Android项目编译报错:Kotlin版本不兼容问题求助
解决Kotlin版本不兼容编译错误
错误信息
/home/bansal/.gradle/caches/transforms-2/files-2.1/0bea321a20a76ca878f594ef198fedcf/jetified-core-ktx-1.10.0-alpha02-api.jar!/META-INF/core-ktx_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.
问题原因
- 项目使用Kotlin 1.6.10版本,但依赖的
androidx.core:core-ktx被自动拉取到1.10.0-alpha02版本,该版本基于Kotlin 1.8编译,二者版本不兼容 - 根项目
build.gradle中重复引入了kotlin-gradle-plugin依赖 - 模块
build.gradle同时声明了固定版本和动态版本的core-ktx,动态版本+会优先拉取最新版本,直接引发冲突
解决方案
1. 固定core-ktx依赖版本
删除模块build.gradle中的implementation "androidx.core:core-ktx:+",保留与Kotlin 1.6.10兼容的稳定版本(如1.7.0),避免自动拉取高版本依赖。
2. 清理重复的Kotlin插件依赖
根项目build.gradle中移除重复的classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10",只保留使用变量引用的版本,确保插件版本统一。
3. (可选)升级Kotlin版本适配最新依赖
若需使用最新版core-ktx,需将项目Kotlin版本升级至1.8+,同时升级Android Gradle Plugin(AGP)到7.0+版本(当前AGP 4.0.1仅支持Kotlin 1.5及以下版本)。
修改后的配置代码
根build.gradle
ext.kotlin_version = '1.6.10' repositories { google() jcenter() maven { url 'http://dl.bintray.com/amulyakhare/maven' } } dependencies { classpath "com.android.tools.build:gradle:4.0.1" classpath 'io.realm:realm-gradle-plugin:3.2.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
模块build.gradle
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } dependencies{ implementation 'androidx.core:core-ktx:1.7.0' annotationProcessor 'android.arch.lifecycle:compiler:1.1.1' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'android.arch.lifecycle:viewmodel:1.1.1' implementation 'android.arch.lifecycle:extensions:1.1.1' //Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1' implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
内容的提问来源于stack exchange,提问作者D.J
相关产品推荐
相关产品推荐

