生成签名APK时Kotlin版本不兼容问题的安全解决方案
问题分析
你遇到的核心问题是Kotlin版本配置冲突:项目依赖(如core-ktx:1.10.0)是用Kotlin 1.8编译的,但当前构建环境被错误地指定为期望1.6版本。冲突根源在于你同时混用了两种Gradle插件配置方式(项目级plugins块 + 模块级buildScript块),导致Kotlin版本不统一。
安全解决方案
步骤1:统一插件配置方式,移除模块级冗余配置
模块级build.gradle中的buildScript块是多余的,因为项目级已经通过plugins方式统一管理插件版本,混用会导致版本冲突。直接删除整个buildScript块。
步骤2:确保Kotlin版本全局一致
项目级和模块级的Kotlin版本必须和依赖要求(1.8+)、IDE插件版本匹配。建议使用正式版而非RC版,避免兼容性问题。
修改后的项目级build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.3.1' apply false id 'com.android.library' version '7.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.8.20' apply false } // 可选:提取版本变量,方便后续统一修改 ext { kotlin_version = '1.8.20' }
修改后的模块级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' } android { namespace 'com.apptest.testpoc' compileSdk 33 defaultConfig { applicationId "com.apptest.testpoc" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } buildFeatures{ viewBinding true } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.10.0' implementation 'androidx.appcompat:appcompat-resources:1.6.1' implementation 'com.google.android.material:material:1.8.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'io.agora.rtc:full-sdk:4.0.1' }
步骤3:同步项目并清理缓存
- 执行
File -> Sync Project with Gradle Files同步配置 - 清理IDE缓存:
File -> Invalidate Caches... -> Invalidate and Restart - 确保IDE的Kotlin插件版本升级到1.8.20正式版(和gradle配置一致)
关键说明
androidx.core:core-ktx:1.10.0及以上版本依赖Kotlin 1.8+,不能降级到1.6,必须保持构建版本和依赖要求匹配。- 避免同时使用
buildScript和plugins两种插件配置方式,这是Gradle构建中版本冲突的常见诱因。
内容的提问来源于stack exchange,提问作者Shashank
相关产品推荐
相关产品推荐

