Kotlin版本不兼容报错:集成Room数据库后编译失败
Kotlin版本不兼容与标准库冲突问题解决(Room+Hilt+Compose项目)
问题背景
开发Kotlin词典应用(采用Room存储单词)时,编译持续报错:模块元数据二进制版本1.7.1与预期1.5.1不兼容,类路径存在多版本Kotlin标准库冲突,手动修改应用Kotlin版本后问题仍未解决。
错误日志
Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath e: C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.7.0/51736992f422993a1e741051bdf3c12801bc1ca1/kotlin-stdlib-common-1.7.0.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1. e: C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.7.0/a5f42c684ad9003160ef0d0f693ecf0ba7b13549/kotlin-stdlib-1.7.0.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1. e: C:/Users/enest/.gradle/caches/transforms-3/721eb3f536dfebc7923ca73bc7386d87/transformed/room-runtime-2.4.3-api.jar!/META-INF/room-runtime_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1. e: C:/Users/enest/.gradle/caches/transforms-3/fc43dbac19dd2a5bd7a61cadd61fc30e/transformed/room-ktx-2.4.3-api.jar!/META-INF/room-ktx_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1. > Task :app:kaptDebugKotlin C:\Users\enest\AndroidStudioProjects\DictionaryApp\app\build\tmp\kapt3\stubs\debug\com\enestigli\dictionaryapp\data\locale\OwnDicDatabase.java:7: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false. public abstract class OwnDicDatabase extends androidx.room.RoomDatabase { ^ warning: The following options were not recognized by any processor: '[dagger.hilt.android.internal.projectType, kapt.kotlin.generated]' > Task :app:compileDebugKotlin FAILED w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath: C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.6.0/baf82c475e9372c25407f3d132439e4aa803b8b8/kotlin-stdlib-jdk8-1.6.0.jar (version 1.6) C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.6.0/da6bdc87391322974a43ccc00a25536ae74dad51/kotlin-stdlib-jdk7-1.6.0.jar (version 1.6) C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.7.0/a5f42c684ad9003160ef0d0f693ecf0ba7b13549/kotlin-stdlib-1.7.0.jar (version 1.7) C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.7.0/51736992f422993a1e741051bdf3c12801bc1ca1/kotlin-stdlib-common-1.7.0.jar (version 1.7) w: Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath e: Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors e: C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.7.0/51736992f422993a1e741051bdf3c12801bc1ca1/kotlin-stdlib-common-1.7.0.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1. e: C:/Users/enest/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.7.0/a5f42c684ad9003160ef0d0f693ecf0ba7b13549/kotlin-stdlib-1.7.0.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1. e: C:/Users/enest/.gradle/caches/transforms-3/721eb3f536dfebc7923ca73bc7386d87/transformed/room-runtime-2.4.3-api.jar!/META-INF/room-runtime_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1.
相关配置
项目级build.gradle
buildscript { ext { compose_version = '1.1.0-alpha02' } }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.2.2' apply false id 'com.android.library' version '7.2.2' apply false id 'org.jetbrains.kotlin.android' version '1.5.21' apply false //hilt id("com.google.dagger.hilt.android") version "2.44" apply false id 'org.jetbrains.kotlin.kapt' version "1.5.21" apply false } task clean(type: Delete) { delete rootProject.buildDir }
模块级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' id 'dagger.hilt.android.plugin' } android { compileSdk 32 defaultConfig { applicationId "com.enestigli.dictionaryapp" minSdk 21 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary 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 = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion compose_version } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.3.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version" // Retrofit implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation "com.squareup.okhttp3:okhttp:4.9.0" implementation "com.squareup.okhttp3:logging-interceptor:4.9.0" //Dagger - Hilt implementation "com.google.dagger:hilt-android:2.38.1" kapt "com.google.dagger:hilt-android-compiler:2.38.1" //implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03" kapt "androidx.hilt:hilt-compiler:1.0.0" implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03' // Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1' // Coroutine Lifecycle Scopes implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1" // Room implementation "androidx.room:room-runtime:2.4.3" kapt "androidx.room:room-compiler:2.4.3" // Kotlin Extensions and Coroutines support for Room implementation "androidx.room:room-ktx:2.4.3" //Material theme3 implementation "androidx.compose.material3:material3:1.0.0-alpha12" implementation "androidx.compose.material3:material3-window-size-class:1.0.0-alpha12" //skrape{it} implementation 'org.jsoup:jsoup:1.12.1' //compose implementation "androidx.compose.runtime:runtime-livedata:$compose_version" }
解决步骤
1. 统一Kotlin版本到匹配Room的版本
Room 2.4.3是基于Kotlin 1.7编译的,当前项目用的1.5.21版本太低,直接升级到1.7.20(与Room 2.4.3兼容):
修改项目级build.gradle中的Kotlin插件版本:
plugins { id 'com.android.application' version '7.2.2' apply false id 'com.android.library' version '7.2.2' apply false // 升级为1.7.20 id 'org.jetbrains.kotlin.android' version '1.7.20' apply false id("com.google.dagger.hilt.android") version "2.44" apply false // kapt版本与Kotlin版本保持一致 id 'org.jetbrains.kotlin.kapt' version "1.7.20" apply false }
2. 强制统一所有Kotlin标准库版本
在模块级build.gradle的dependencies块末尾添加版本强制规则,避免依赖间接引入不同版本的标准库:
configurations.all { resolutionStrategy { force 'org.jetbrains.kotlin:kotlin-stdlib:1.7.20' force 'org.jetbrains.kotlin:kotlin-stdlib-common:1.7.20' force 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20' force 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20' } }
3. 同步依赖并清理缓存
- 点击Android Studio右上角的「Sync Project with Gradle Files」同步配置
- 执行「File -> Invalidate Caches... -> Invalidate and Restart」清理缓存后重启AS
4. 处理Room Schema警告(可选)
日志里的Room Schema警告,可二选一解决:
- 方案一:在模块级build.gradle的
defaultConfig中添加Schema导出路径:
defaultConfig { // ...其他配置 javaCompileOptions { annotationProcessorOptions { arguments += ["room.schemaLocation": "$projectDir/schemas".toString()] } } }
- 方案二:在你的
OwnDicDatabase类上添加注解关闭导出:
@Database(entities = [YourEntity::class], version = 1, exportSchema = false) abstract class OwnDicDatabase : RoomDatabase() { // ... }
5. 统一Hilt版本(可选)
当前Hilt依赖版本2.38.1与插件版本2.44不一致,建议统一为2.44,避免潜在冲突:
// Dagger - Hilt implementation "com.google.dagger:hilt-android:2.44" kapt "com.google.dagger:hilt-android-compiler:2.44" kapt "androidx.hilt:hilt-compiler:1.0.0" implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
内容的提问来源于stack exchange,提问作者NewPartizal
相关产品推荐
相关产品推荐

