Android Studio Gradle构建失败 求无VPN永久解决方案
解决Android Gradle构建依赖下载失败问题
问题描述
导入多数现有Android项目时频繁遇到Gradle构建失败,因Google对伊朗用户限制相关服务,需借助VPN才能下载依赖库,但VPN连接困难且速度极慢,现咨询:
- 是否可以手动下载所有依赖库到指定文件夹,并配置到项目中?
- 是否可像Visual Studio的NuGet一样,更换Android Studio的依赖仓库地址来获取库?
报错信息
Could not resolve all artifacts for configuration ':classpath'. Could not find gradle-4.2.2.jar (com.android.tools.build:gradle:4.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/4.2.2/gradle-4.2.2.jar Could not find builder-4.2.2.jar (com.android.tools.build:builder:4.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/build/builder/4.2.2/builder-4.2.2.jar Could not find aaptcompiler-4.2.2.jar (com.android.tools.build:aaptcompiler:4.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/build/aaptcompiler/4.2.2/aaptcompiler-4.2.2.jar Could not find bundletool-1.1.0.jar (com.android.tools.build:bundletool:1.1.0). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/build/bundletool/1.1.0/bundletool-1.1.0.jar Could not find sdk-common-27.2.2.jar (com.android.tools:sdk-common:27.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/sdk-common/27.2.2/sdk-common-27.2.2.jar Could not find sdklib-27.2.2.jar (com.android.tools:sdklib:27.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/sdklib/27.2.2/sdklib-27.2.2.jar Could not find common-27.2.2.jar (com.android.tools:common:27.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/common/27.2.2/common-27.2.2.jar Could not find protos-27.2.2.jar (com.android.tools.analytics-library:protos:27.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/analytics-library/protos/27.2.2/protos-27.2.2.jar Could not find core-proto-0.0.8-alpha01.jar (com.google.testing.platform:core-proto:0.0.8-alpha01). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/google/testing/platform/core-proto/0.0.8-alpha01/core-proto-0.0.8-alpha01.jar Could not find zipflinger-4.2.2.jar (com.android:zipflinger:4.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/zipflinger/4.2.2/zipflinger-4.2.2.jar Could not find apksig-4.2.2.jar (com.android.tools.build:apksig:4.2.2). Searched in the following locations: https://dl.google.com/dl/android/maven2/com/android/tools/build/apksig/4.2.2/apksig-4.2.2.jar
项目build.gradle配置
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.2.2" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon maven { url "https://jitpack.io" } } } task clean(type: Delete) { delete rootProject.buildDir }
解决方案
1. 手动下载依赖并配置到项目
可以手动下载依赖库并配置本地仓库,步骤如下:
- 步骤1:下载依赖包
根据报错信息里列出的依赖文件路径,手动下载对应的jar包。每个缺失依赖都会标注其查找路径,按照该路径即可获取对应的文件。 - 步骤2:创建本地仓库目录
在项目根目录下新建一个文件夹(比如命名为local-repo),然后按照Maven标准目录结构存放jar包。例如依赖com.android.tools.build:gradle:4.2.2,需要在local-repo下创建com/android/tools/build/gradle/4.2.2的层级目录,将下载好的jar包放入该目录。如果能找到对应依赖的pom文件,也一并放入;若找不到,可以手动创建一个简单的pom.xml文件,声明依赖的基本groupId、artifactId和version信息。 - 步骤3:配置项目使用本地仓库
修改项目根目录的build.gradle,在buildscript和allprojects的repositories块中添加本地仓库的引用:
配置完成后,Gradle会优先从本地仓库查找所需依赖。buildscript { repositories { maven { url uri('./local-repo') } // 引用本地仓库 google() mavenCentral() jcenter() } // 其他依赖配置不变 } allprojects { repositories { maven { url uri('./local-repo') } // 引用本地仓库 google() mavenCentral() jcenter() maven { url "https://jitpack.io" } } }
2. 更换依赖仓库地址
可以替换Google仓库为国内Maven镜像仓库,步骤如下:
- 替换默认仓库为镜像仓库
修改项目根目录的build.gradle,将google()、mavenCentral()、jcenter()替换为国内主流的Maven镜像仓库地址(可自行搜索国内可靠的Android相关Maven镜像)。示例配置如下:
注意:选择镜像仓库时优先选同步及时、稳定的,若部分依赖仍无法找到,可结合手动下载依赖的方式补充。buildscript { repositories { maven { url '国内Google镜像仓库地址' } maven { url '国内公共Maven镜像仓库地址' } maven { url '国内jcenter镜像仓库地址' } } dependencies { classpath "com.android.tools.build:gradle:4.2.2" } } allprojects { repositories { maven { url '国内Google镜像仓库地址' } maven { url '国内公共Maven镜像仓库地址' } maven { url '国内jcenter镜像仓库地址' } maven { url "https://jitpack.io" } } }
内容的提问来源于stack exchange,提问作者Mohamad Hasan Salmaaniyaan
相关产品推荐
相关产品推荐

