引起:org.gradle.API.InvalidUserCodeException:使用依赖性目录需要激活匹配要素预览

gajydyqb  于 2023-05-07  发布在  其他
关注(0)|答案(2)|浏览(718)

当我尝试在Gradle 7.0中使用目录功能时https://docs.gradle.org/7.0.2/userguide/platforms.html#sub:central-declaration-of-dependencies
如下所示,转换为setting.gradle

dependencyResolutionManagement {
    // Some other codes

    versionCatalogs {
        libs {
            alias('androidx-core').to('androidx.core:core-ktx:1.6.0')
            alias('androidx-appcompat').to('androidx.appcompat:appcompat:1.3.1')
            alias('androidx-constrainlayout').to('androidx.constraintlayout:constraintlayout:2.1.0')
            alias('android-material').to('com.google.android.material:material:1.4.0')
        }
    }
}

当我执行gradle同步时,它错误地指出

Caused by: org.gradle.api.InvalidUserCodeException: Using dependency catalogs requires the activation of the matching feature preview.

我检查了https://docs.gradle.org/7.0.2/userguide/platforms.html#sub:central-declaration-of-dependencies,发现它指出

Central declaration of dependencies is an incubating feature. It requires the activation of the VERSION_CATALOGS feature preview.

在https://docs.gradle.org/7.0.2/userguide/feature_lifecycle.html#feature_preview网站上

The feature preview API allows certain incubating features to be activated by adding enableFeaturePreview('FEATURE') in your settings file. Individual preview features will be announced in release notes.

我尝试在settings.gradle中添加enableFeaturePreview('FEATURE'),但似乎不起作用。我应该把enableFeaturePreview('FEATURE')放在哪里?

htrmnn0y

htrmnn0y1#

看起来我需要将FEATURE替换为实际功能的名称。即

enableFeaturePreview("VERSION_CATALOGS")

我只是把它放在settings.gradle文件的第一行,这就可以了。

sqougxex

sqougxex2#

在我的例子中,当我在settings.gradle (project-name)文件的最后一行中添加enableFeaturePreview("VERSION_CATALOGS")行时,它可以工作。
文件是

settings.gradle(项目名称)

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "project-name"
include ':app'
enableFeaturePreview("VERSION_CATALOGS")

build.gradle(项目名称)

plugins {
    alias(libs.plugins.android.application) apply(false)
    alias(libs.plugins.android.library) apply(false)
    alias(libs.plugins.kotlin.android) apply(false)
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

相关问题