Gradle版本与插件更新报错求助:旧Android项目升级问题
老旧Android项目Gradle升级问题求助
我维护一个老旧Android项目,Gradle版本和插件均已严重过时。尝试将二者升级至最新版本时出现大量配置错误,核心问题为无法解析com.android.tools.build:gradle:7.4.1,存在Java版本兼容、插件API版本不匹配等冲突;同时项目出现AppCompatActivity无法解析的报错,但应用可正常运行,且无法使用ConstraintLayout等新特性。
错误日志
A problem occurred configuring root project 'android_calorie_counter-master'. > Could not resolve all files for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:7.4.1. Required by: project : > No matching variant of com.android.tools.build:gradle:7.4.1 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.0.2' but: - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.1 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.2') - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.1 declares a component for use during runtime, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.2') - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.1 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.2') - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.1 declares a component for use during runtime, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.2') * Try: > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Exception is: org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'android_calorie_counter-master'. at org.gradle.configuration.project.LifecycleProjectEvaluator.wrapException(LifecycleProjectEvaluator.java:84) ...
项目相关配置
app模块build.gradle配置
apply plugin: 'com.android.application' android { compileSdkVersion 27 buildToolsVersion "27.0.3" defaultConfig { applicationId "com.vladnamik.developer" minSdkVersion 15 targetSdkVersion 33 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { api fileTree(dir: 'libs', include: ['*.jar']) testImplementation 'junit:junit:4.12' api 'com.android.support:appcompat-v7:27.1.1+' api 'com.android.support:design:27.1.1' }
gradle.properties配置
# Project-wide Gradle settings. # IDE (e.g. Android Studio) users: # Gradle settings configured through the IDE *will override* # any settings specified in this file. # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. # Default value: -Xmx10248m -XX:MaxPermSize=256m # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true
gradle-wrapper.properties配置
#Sat Mar 04 19:19:57 IST 2023 distributionBase=GRADLE_USER_HOME distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME
解决方案
1. 匹配Gradle与Android Gradle插件版本
错误日志显示你用Gradle 8.0.2(插件API版本8.0.2)拉取AGP 7.4.1,二者版本不兼容。AGP 7.4.1需搭配Gradle 7.5:
修改gradle-wrapper.properties的distributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
2. 调整Java版本兼容
AGP 7.0+要求Java 11,需在项目根目录build.gradle添加配置:
buildscript { repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.1' } } allprojects { repositories { google() mavenCentral() } } tasks.withType(JavaCompile) { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 }
同时在gradle.properties指定Java 11路径(或在Android Studio的File > Project Structure > SDK Location设置Embedded JDK 11):
org.gradle.java.home=/path/to/your/java11/jdk
3. 迁移到AndroidX解决API解析与新特性问题
旧Support库无法兼容新特性,需迁移到AndroidX:
- 在
gradle.properties添加:
android.useAndroidX=true android.enableJetifier=true
- 修改app模块
build.gradle依赖:
dependencies { api fileTree(dir: 'libs', include: ['*.jar']) testImplementation 'junit:junit:4.12' api 'androidx.appcompat:appcompat:1.6.1' api 'com.google.android.material:material:1.9.0' // 添加ConstraintLayout依赖 api 'androidx.constraintlayout:constraintlayout:2.1.4' }
4. 更新编译配置
升级compileSdkVersion到33,移除buildToolsVersion(AGP 7.0+自动匹配),添加Java编译选项:
android { compileSdkVersion 33 defaultConfig { applicationId "com.vladnamik.developer" minSdkVersion 15 targetSdkVersion 33 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } }
5. 同步与清理项目
- 点击Android Studio右上角
Sync Project with Gradle Files按钮 - 执行
Build > Clean Project,再执行Build > Rebuild Project
内容的提问来源于stack exchange,提问作者PCAPS
相关产品推荐
相关产品推荐

