Gradle 插件无法解析与代理配置导致的构建失败排查
在使用 Gradle 构建项目时,若遇到插件无法下载或解析的情况,错误信息通常指向插件仓库访问失败。以下是一个典型的异常堆栈:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'org.gradle.kotlin.kotlin-dsl', version: '2.3.3'] was not found in any of the following sources:
Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
Included Builds (None of the included builds contain this plugin)
Plugin Repositories (could not resolve plugin artifact 'org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:2.3.3')
Searched in the following repositories:
Gradle Central Plugin Repository表面看是插件在仓库中不存在,但实际问题往往与网络代理配置有关。
代理配置残留引发的构建故障
Gradle 支持通过 gradle.properties 文件配置网络代理。当代理服务未运行时,残留的代理设置会导致所有网络请求被重定向到无效的本地端口,从而引发连接拒绝错误。
检查 Gradle 配置目录下的 gradle.properties 文件(路径通常为 ~/.gradle/gradle.properties 或项目根目录下的同名文件),可能会发现如下配置:
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=1080
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=1080解决方案
根据实际需求选择以下任一方式处理:
- 移除代理配置:若当前网络环境无需代理,直接删除上述四行配置后重新构建。
- 更新代理参数:若需使用代理,将端口修改为实际运行的代理服务端口,或更换为有效的代理主机地址。
- 使用环境变量替代:通过
HTTP_PROXY和HTTPS_PROXY环境变量动态控制,避免硬编码在配置文件中。
Flutter 项目中的类似场景
在 Flutter 项目的 Android 构建过程中,同样可能因代理配置失效导致 Kotlin 相关工件下载失败:
FAILURE: Build failed with an exception.
* Where:
Settings file '.../android/settings.gradle.kts' line: 20
* What went wrong:
Error resolving plugin [id: 'dev.flutter.flutter-plugin-loader', version: '1.0.0']
> A problem occurred configuring project ':gradle'.
> Could not resolve all artifacts for configuration 'classpath'.
> Could not download kotlin-gradle-plugin-2.0.21-gradle85.jar (org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21)
> Could not get resource 'https://plugins.gradle.org/m2/...'
> Got socket exception during request. It might be caused by SSL misconfiguration
> Connect to 127.0.0.1:1080 [/127.0.0.1] failed: Connection refused: getsockopt错误信息中明确显示连接 127.0.0.1:1080 被拒绝,这与前述代理配置完全吻合。清理代理设置后,Gradle 将直接连接默认的插件仓库,构建即可恢复正常。