ROOM构建报错:元数据二进制版本1.7.1与预期1.5.1不匹配
解决Room构建错误:The binary version of its metadata is 1.7.1, expected version is 1.5.1
问题原因
这个错误是Kotlin插件版本与Room编译器依赖的Kotlin版本不匹配导致的。你使用的Room 2.5.1基于Kotlin 1.7+版本构建,但项目当前的Kotlin Android插件版本为1.5.x,两者元数据版本无法兼容。
解决方案
1. 升级项目根目录的Kotlin插件版本
打开项目根目录下的build.gradle(或build.gradle.kts),将Kotlin插件版本升级到与Room 2.5.1兼容的版本(推荐1.8.20或更高):
plugins { id 'com.android.application' version '7.4.2' apply false id 'org.jetbrains.kotlin.android' version '1.8.20' apply false }
2. 同步模块级build.gradle的Kotlin配置
在你的模块级build.gradle中,确保Kotlin编译配置与插件版本对齐:
android { // ...保留你原有其他配置 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } }
3. 重新同步并构建项目
修改完成后,点击Android Studio的「Sync Project with Gradle Files」按钮,同步完成后重新构建项目即可。
你的原模块级build.gradle参考(补充必要配置后)
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' } android { compileSdk 33 defaultConfig { applicationId "com.your.package.name" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary true } } 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.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' 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" implementation 'androidx.core:core-splashscreen:1.0.0' def room_version = "2.5.1" implementation "androidx.room:room-runtime:$room_version" kapt "androidx.room:room-compiler:$room_version" }
内容的提问来源于stack exchange,提问作者xMaster
相关产品推荐
相关产品推荐

