51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Android编译报错:Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'

问题描述 {#问题描述}

Android项目编译时报错,提示如下信息:
Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'

解决方案 {#解决方案}

这个问题其实是因为使用的Android Studio中的Gradle版本较高引起的:

当Gradle版本在7.0以下的时候,工程根目录下的build.gradle文件中的配置如下所示:

|------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:4.0.2' } } allprojects { repositories { google() jcenter() } } |

当Gradle版本在7.0以上的时候,工程根目录下的build.gradle文件中的allprojects > repositories配置需要迁移到项目级settings.gradle文件中:

|-----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 | dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } |

操作方法 {#操作方法}

  1. settings.gradle中的dependencyResolutionManagement中添加镜像源。
  2. 删除build.gradle(project)中的原有镜像源。

后记 {#后记}

当我们按照以上的说明在dependencyResolutionManagement->repositories中增加maven配置后,这个问题就可以顺利解决了!

注意:只要你使用的Gradle版本在7.0以上,当你接入相关SDK时都需要做以上的配置迁移工作!


赞(4)
未经允许不得转载:工具盒子 » Android编译报错:Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'