Gradle 6.1.1与AGP 4.0.1项目构建失败求助
解决Gradle 6.1.1 + AGP 4.0.1项目构建失败问题
问题背景
项目基于Gradle 6.1.1、Android Gradle Plugin 4.0.1开发,两年前可正常构建,现在同步时出现证书验证失败的构建错误,核心表现为无法从JCenter拉取gradle-bintray-plugin插件。
错误原因
- JCenter已停止官方维护,原有SSL证书失效,导致HTTPS请求无法通过安全验证;
- 项目依赖的
gradle-bintray-plugin和第三方android-maven-gradle-plugin均已废弃,且仅能从JCenter获取。
解决方案
1. 修正根目录build.gradle配置
移除所有jcenter()仓库引用,删除废弃插件依赖:
修改后的根目录build.gradle代码:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() maven { url 'https://maven.google.com' } } dependencies { classpath "com.android.tools.build:gradle:4.0.1" // 移除废弃的第三方Maven插件,改用AGP自带的maven-publish // classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // 移除废弃的Bintray插件,本地构建无需保留;若需发布可改用JFrog Artifactory插件 // classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() maven { url 'https://www.jitpack.io' } maven { url 'https://maven.google.com' } } } task clean(type: Delete) { delete rootProject.buildDir }
2. 替换模块中的Maven插件(若有使用)
如果模块build.gradle中应用了com.github.dcendents.android-maven插件,替换为AGP自带的maven-publish插件,按需配置发布任务:
修改后的模块build.gradle相关配置:
apply plugin: 'com.android.application' apply plugin: 'maven-publish' // 替换原第三方Maven插件 android { compileSdkVersion 29 defaultConfig { applicationId "com.example.electricalcalculator" minSdkVersion 18 targetSdkVersion 29 versionCode 18 versionName "2.3.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildToolsVersion '29.0.2' } // 若需将产物发布到本地Maven仓库,添加以下配置(可选) afterEvaluate { publishing { publications { release(MavenPublication) { from components.release groupId 'com.example' // 替换为你的项目groupId artifactId 'electricalcalculator' // 替换为你的模块artifactId version '2.3.0' // 替换为你的版本号 } } } }
3. 检查其他依赖来源
如果项目中还有其他依赖是从JCenter拉取的,需要找到这些依赖的替代仓库(如MavenCentral、Google或JitPack),替换仓库引用后重新同步项目。
内容的提问来源于stack exchange,提问作者Sedghian
相关产品推荐
相关产品推荐

