Gradle - 评估根项目时出现问题 (Android)

Gradle - A problem occurred evaluating root project (Android)

你好,我正在尝试添加这个存储库

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

但不知道为什么会出现此错误(以前从未发生过)

A problem occurred evaluating root project 'Dog Howl'.
> Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'

完整代码

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.4"
        classpath 'com.google.gms:google-services:4.3.10'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

在您的项目中settings.gradle添加您想要的存储库

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
   repositories {
   google()
   mavenCentral()
   jcenter()
   maven { url "https://maven.xyz.com" }
}

}

好的,感谢这个 它解决了我的问题

settings.gradle 中我替换了这个

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

有了这个

repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

它解决了问题