导致:org.gradle.api.InvalidUserCodeException:使用依赖项目录需要激活匹配的功能预览

Ely*_*lye 11 gradle

当我尝试使用 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')
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Caused by: org.gradle.api.InvalidUserCodeException: Using dependency catalogs requires the activation of the matching feature preview.
Run Code Online (Sandbox Code Playgroud)

我检查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.
Run Code Online (Sandbox Code Playgroud)

在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.
Run Code Online (Sandbox Code Playgroud)

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

Ely*_*lye 27

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

enableFeaturePreview("VERSION_CATALOGS")
Run Code Online (Sandbox Code Playgroud)

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