问题现象
一般新创建的Android Studio项目,在创建完成后会自动使用gradle进行编译构建,下载需要的依赖包等;而国内因为访问google,mavenCentral很慢,就会出现下载依赖包时下载失败,如:
Caused by: java.io.EOFException: SSL peer shut down incorrectly
Cause 2: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0.
Caused by: org.gradle.api.resources.ResourceException: Could not get resource 'https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.6.0/kotlin-gradle-plugin-1.6.0.pom'.
Caused by: org.gradle.internal.resource.transport.http.HttpRequestException: Could not HEAD 'https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.6.0/kotlin-gradle-plugin-1.6.0.pom'.
Caused by: org.gradle.internal.resource.transport.http.HttpRequestException: The server may not support the client's requested TLS protocol versions: (TLSv1.2, TLSv1.3). You may need to configure the client to allow other protocols to be used. See: https://docs.gradle.org/7.0.2/userguide/build_environment.html#gradle_system_properties
Caused by: javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake
原因分析
从错误信息来看,是说repo.maven.apache.org 仓库站点不支持客户端的TLSv1.2或者TLSv1.3,需要客户端配置支持站点支持的其他协议。 然而repo.maven.apache.org 是MavenCentral官方镜像仓库,怎么可能不支持现在更推荐的TLSv1.2呢,Gradle最新版本已经不再支持相对不安全的TLSv1.1了。去myssl.cn输入repo.maven.apache.org 检查https支持情况,果然是支持TLSv1.2的。 那么问题就可能出现在网络通信上,既然国外的MavenCentral访问有问题,那换成国内的源试试呢? 现在比较推荐的是阿里云的maven仓库,在项目的build.gradle中配置阿里仓库地址:maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' } ,点击【Sync Project With Gradle Files】执行后成功了
解决方案
项目级别
如上面所说,直接在项目根目录的build.gradle文件中,buildscript.repositories节点下添加下面内容即可:
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
全局设置
如果希望对本地所有项目一起生效,在%userprofile%/.gradle 文件夹下,创建名为init.gradle 的文件,保存如下内容:
allprojects {
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
}
}
|