使用Gradle Wrapper构建项目时找不到com.android.tools.build:gradle:3.0.1
解决Gradle Wrapper构建时找不到com.android.tools.build:gradle:3.0.1的问题
我最近碰到了个特别头疼的问题:用Gradle Wrapper执行./gradlew命令构建Android项目时直接失败,报错找不到com.android.tools.build:gradle:3.0.1依赖,但在Android Studio里点击构建却完全正常。试了好几种办法都没解决,最后终于找到原因了,分享给同样踩坑的朋友。
报错信息
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ':app'. > Could not resolve all files for configuration ':app:classpath'. > Could not find com.android.tools.build:gradle:3.0.1. Searched in the following locations: https://maven.fabric.io/public/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom https://maven.fabric.io/public/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.jar https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.pom https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.1/gradle-3.0.1.jar Required by: project :app * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. * Get more help at https://help.gradle.org
我试过的无效方法
- 确认Gradle Wrapper版本是4.1(和Android Studio里使用的版本一致)
- 在根目录
build.gradle中添加了google()仓库 - 尝试设置
android.enableAapt2=false
我的初始配置文件
根目录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.0.1' // 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 }
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
最终解决方法
原来我被官方文档的描述误导了!文档说“要将此类库添加到构建中,请在顶层build.gradle文件中包含Google的Maven仓库”,但实际上还需要在app模块的build.gradle文件中也添加buildscript的仓库配置:
修改app/build.gradle,添加以下内容:
... buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' } } ...
添加之后再执行./gradlew,构建就正常通过了!
内容的提问来源于stack exchange,提问作者FGarcia
相关产品推荐
相关产品推荐

