使用Hilt 2.40.1搭配Kotlin 1.9.10时遇不支持的元数据版本错误
解决Hilt「Unsupported metadata version」错误
问题根源
你用的Dagger Hilt 2.40.1版本不兼容Kotlin 1.9.10。Hilt对Kotlin版本有严格匹配要求,旧版Hilt无法识别Kotlin 1.9生成的元数据格式,才会抛出这个错误。
修复步骤
1. 升级Hilt版本
把Hilt版本升到2.48及以上(这个版本开始正式支持Kotlin 1.9.x)。修改顶级build.gradle里的Hilt版本:
buildscript { ext { kotlin_version = '1.9.10' compose_version = '1.5.3' hilt_version = '2.48' // 替换成兼容的新版本 } repositories { google() mavenCentral() } dependencies { def gradle = '8.2.0' classpath "com.android.tools.build:gradle:$gradle" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // 同步升级Hilt插件版本 classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version" def detekt='1.18.1' classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detekt" } }
2. 修正应用级依赖配置
应用级build.gradle里有两处错误需要调整:
hilt-android-compiler必须用kapt引入,不能用implementation- 同步Hilt版本到2.48
修改后的依赖片段:
dependencies { // ... 其他依赖保持不变 def hilt = "2.48" implementation "com.google.dagger:hilt-android:$hilt" kapt "com.google.dagger:hilt-android-compiler:$hilt" // 替换原implementation为kapt implementation "androidx.hilt:hilt-navigation-compose:1.1.0" // ... 其他依赖保持不变 def hilt_lifecycle_view_model = "1.0.0-alpha03" implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_lifecycle_view_model" kapt "androidx.hilt:hilt-compiler:1.1.0" // 这里也改成kapt }
3. 同步并清理构建
- 点击Android Studio顶部的「Sync Project with Gradle Files」按钮同步配置
- 执行
./gradlew clean build命令清理旧构建文件并重新编译
额外优化建议
你的Compose配置里,kotlinCompilerExtensionVersion和Compose版本不一致(当前Compose用1.5.4,扩展版本写的1.5.3),建议改成一致避免潜在问题:
composeOptions { kotlinCompilerExtensionVersion "1.5.4" kotlinCompilerVersion "1.9.10" }
内容的提问来源于stack exchange,提问作者Gagan Batra
相关产品推荐
相关产品推荐

