将targetSdkVersion从30升级到31后Android Studio构建报错
解决targetSdkVersion升级到31后kaptDebugKotlin构建失败问题
核心问题根源
- 旧版Android Support库与AndroidX库混合依赖,导致注解处理逻辑冲突
- 使用了过时的Android架构组件版本(Room 1.1.0),其注解处理器不兼容targetSdk31引入的新注解属性规范
- Kotlin及kapt插件版本过低,无法识别targetSdk31新增的
MODULE注解属性
分步解决方案
1. 彻底迁移到AndroidX
移除所有com.android.support开头的依赖,替换为对应的AndroidX库,targetSdk31要求必须使用AndroidX:
com.android.support:appcompat-v7:28.0.0→androidx.appcompat:appcompat:1.4.1com.android.support:support-v4:28.0.0→androidx.legacy:legacy-support-v4:1.0.0com.android.support:design:28.0.0→com.google.android.material:material:1.5.0com.android.support.constraint:constraint-layout:1.1.3→androidx.constraintlayout:constraintlayout:2.1.3com.android.support:multidex:1.0.3→androidx.multidex:multidex:2.0.1
2. 升级Room到AndroidX版本
替换旧的Android架构组件Room为AndroidX版本,版本至少升级到2.4.0+(旧版本注解处理器不兼容targetSdk31):
def room_version = "2.4.3" // 移除旧的android.arch依赖 implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-rxjava2:$room_version" kapt "androidx.room:room-compiler:$room_version"
3. 升级Kotlin及相关插件版本
在项目根目录的build.gradle中指定兼容targetSdk31的Kotlin版本(至少1.5.31),同步升级Android Gradle插件到7.0+:
buildscript { ext.kotlin_version = "1.5.31" dependencies { classpath "com.android.tools.build:gradle:7.0.4" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
4. 清理重复依赖与配置
合并两个dependencies块,移除重复依赖(如重复的appcompat-v7、fileTree),确保配置统一。
修改后的完整app/build.gradle示例
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: "androidx.navigation.safeargs.kotlin" apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 31 defaultConfig { applicationId "com.app.app" minSdkVersion 21 targetSdkVersion 31 versionCode 168 versionName "4.68" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary true multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } dataBinding { enabled = true } } dependencies { def room_version = "2.4.3" def retro_version = "2.9.0" def gson_version = "2.9.0" def nav_version = "2.4.0" def kotlin_version = "1.5.31" implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'com.google.android.gms:play-services-maps:18.0.2' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation "com.google.code.gson:gson:$gson_version" // Room AndroidX版本 implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-rxjava2:$room_version" kapt "androidx.room:room-compiler:$room_version" implementation 'androidx.multidex:multidex:2.0.1' implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" implementation "androidx.navigation:navigation-ui-ktx:$nav_version" // Utils implementation "org.jetbrains.anko:anko-commons:0.10.5" // Retrofit升级到兼容版本 implementation "com.squareup.retrofit2:retrofit:$retro_version" implementation "com.squareup.retrofit2:adapter-rxjava2:$retro_version" implementation "com.squareup.retrofit2:converter-gson:$retro_version" implementation "com.squareup.retrofit2:converter-scalars:$retro_version" implementation "com.squareup.picasso:picasso:2.71828" implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" implementation "androidx.recyclerview:recyclerview:1.2.1" implementation "androidx.recyclerview:recyclerview-selection:1.1.0" implementation 'com.google.firebase:firebase-analytics:19.0.0' implementation 'com.google.firebase:firebase-auth:21.0.1' implementation 'com.google.firebase:firebase-firestore:24.0.1' implementation 'com.google.firebase:firebase-messaging:23.0.0' implementation 'com.google.android.gms:play-services-location:19.0.1' implementation 'me.dm7.barcodescanner:zxing:1.9' implementation 'com.journeyapps:zxing-android-embedded:3.4.0' implementation 'com.vorlonsoft:androidrate:1.0.8' // Lifecycle升级 implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1' }
内容的提问来源于stack exchange,提问作者ank berma
相关产品推荐
相关产品推荐

