在使用 Gradle 构建 Android 项目时,报错:
A problem occurred configuring root project 'ProjectName'.
> Could not resolve all dependencies for configuration ':classpath'.
> Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(http://maven.aliyun.com/nexus/content/groups/public/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols.
错误原因解析
该问题出现在 Gradle 配置中使用了 HTTP 协议的 Maven 仓库地址。自 Gradle 7.0 开始,Gradle 默认禁用了不安全的 HTTP 协议,而要求开发者使用 HTTPS 协议。如果仓库配置如下:
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
Gradle 会抛出此错误,建议开发者切换到 HTTPS 或显式允许 HTTP 协议。
原因详情
-
Gradle 强制安全策略:
- Gradle 7.0+ 默认不允许使用不安全的 HTTP 协议以提高软件安全性。
- 如果项目依赖的第三方 Maven 仓库未配置 HTTPS,会导致构建失败。
-
未显式允许 HTTP 协议:
- Gradle 提供了
allowInsecureProtocol
配置选项,但需要开发者手动启用。
- Gradle 提供了
解决方案
1. 优先更换为 HTTPS 仓库地址
如果使用的仓库支持 HTTPS,则应直接将 HTTP 替换为 HTTPS。例如:
将原来的配置:
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
改为:
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
建议: 大多数主流仓库都支持 HTTPS(如 Maven Central、JCenter 等),直接替换即可解决问题。
2. 显式允许 HTTP 协议
如果仓库仅支持 HTTP(例如某些内部镜像仓库),可以通过 allowInsecureProtocol
参数显式允许使用 HTTP。
修改后的配置如下:
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
allowInsecureProtocol = true
}
完整配置示例
以下是解决问题后的完整 build.gradle
文件:
buildscript {
repositories {
google()
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
allowInsecureProtocol = true
}
maven { url 'https://maven.aliyun.com/repository/public/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:8.7.2'
}
}
3. 检查所有仓库配置
遍历项目中的所有仓库配置(包括 buildscript
和 repositories
部分),确保每一个仓库地址都符合以下规则:
- 优先使用 HTTPS。
- 如果无法使用 HTTPS,显式允许 HTTP:
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' } // 推荐使用 HTTPS
maven {
url 'http://internal.repo.local/maven' // 内部仓库
allowInsecureProtocol = true
}
}
4. 升级 Gradle Wrapper
使用较旧版本的 Gradle Wrapper 也可能导致与仓库安全策略不兼容。建议升级 Gradle Wrapper 到最新版本。修改项目中的 gradle-wrapper.properties
文件:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
执行以下命令完成升级:
./gradlew wrapper --gradle-version 8.2
案例演示
问题代码
在项目中,build.gradle
文件配置如下:
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
}
}
构建时报错:
Using insecure protocols with repositories, without explicit opt-in, is unsupported.
解决代码
根据错误提示,调整为以下配置:
buildscript {
repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
allowInsecureProtocol = true
}
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
}
}
总结
问题原因:
Gradle 7.0+ 默认禁止不安全的 HTTP 协议,要求使用 HTTPS 或显式配置允许 HTTP。
解决方法:
- 优先将仓库地址改为 HTTPS。
- 如果必须使用 HTTP,使用
allowInsecureProtocol
显式允许。