Android项目编译报错:无法解析com.android.tools.build:gradle依赖
解决Cannot resolve external dependency com.android.tools.build:gradle的问题
我来帮你排查这个问题,从你提供的配置和报错信息来看,核心问题出在模块级build.gradle里多余的buildscript块,另外还有一些小的配置优化点可以一并处理:
问题根源
你的模块级build.gradle末尾有这么一段代码:
buildscript { dependencies { classpath 'com.android.tools.build:gradle' } }
这段代码的问题在于:
- Android Gradle Plugin(也就是
com.android.tools.build:gradle)的classpath声明应该只放在顶级build.gradle的buildscript块中,模块里不需要重复声明 - 这个块里没有定义任何仓库(repositories),Gradle不知道去哪里下载这个依赖,所以抛出了
no repositories are defined的错误 - 你没有指定依赖的版本号,Gradle会尝试拉取最新版本,但顶级build.gradle里已经指定了3.5.0,这里重复声明完全没必要
修复步骤
1. 删除模块级build.gradle里的多余buildscript块
直接删掉模块文件末尾的那段buildscript代码,只保留模块本身的配置内容。
2. 优化顶级build.gradle的仓库配置
你的顶级build.gradle里的maven仓库https://dl.google.com/dl/android/maven2其实已经被google()仓库包含了,可以删掉这个冗余的maven块,简化后的顶级build.gradle如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
3. 清理模块级build.gradle的冗余依赖和版本问题
你的模块依赖里有重复声明(比如多次implementation 'androidx.appcompat:appcompat:1.1.0'),还有buildToolsVersion和compileSdkVersion不匹配的问题,建议优化后的模块build.gradle如下:
apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion '29.0.3' // 升级到和compileSdk匹配的版本 defaultConfig { applicationId "com.flixarts.ar.englishnow" minSdkVersion 14 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" // 换成androidx的测试runner multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) // AndroidX核心依赖 implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.recyclerview:recyclerview:1.0.0' implementation 'com.google.android.material:material:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' // 换成稳定版,去掉-rc01 // 统一用AndroidX的multidex implementation 'androidx.multidex:multidex:2.0.1' // 第三方依赖 implementation 'com.google.android.gms:play-services-ads:18.2.0' // 测试依赖 testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' // 换成androidx的测试runner androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' // 升级到稳定版 }
这里做了几个关键优化:
- 升级buildToolsVersion到和compileSdkVersion 29匹配的29.0.3
- 替换旧的support库测试runner为AndroidX版本
- 用AndroidX的multidex替代旧的
com.android.support:multidex - 移除重复的依赖声明
- 把带
-rc01的预览版依赖换成稳定版,避免潜在问题
4. 同步项目并清理缓存
做完以上修改后,点击Android Studio右上角的Sync Project with Gradle Files按钮,等待同步完成。如果还是有问题,可以尝试File > Invalidate Caches / Restart清理缓存再重启。
为什么更新Android Studio会触发这个问题?
新版Android Studio对Gradle配置的校验更严格,旧版本可能允许这种冗余的buildscript块不报错,但新版本会直接抛出找不到仓库的错误,这就是你更新后出现问题的原因。
内容的提问来源于stack exchange,提问作者Mauro Stancato
相关产品推荐
相关产品推荐

