Hilt报错:不支持的元数据版本,寻求修复方案
Android Hilt构建错误:Unsupported metadata version 修复方案
错误信息
Task :app:kaptDebugKotlin FAILED Values of variant API AnnotationProcessorOptions.arguments are queried and may return non final values, this is unsupported error: [Hilt] Unsupported metadata version. Check that your Kotlin version is >= 1.0: java.lang.IllegalStateException: Unsupported metadata version. Check that your Kotlin version is >= 1.0 at dagger.internal.codegen.kotlin.KotlinMetadata.metadataOf(KotlinMetadata.java:206) at dagger.internal.codegen.kotlin.KotlinMetadata.from(KotlinMetadata.java:187) ...(完整堆栈略) [Hilt] Processing did not complete. See error above for details. Execution failed for task ':app:kaptDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction > java.lang.reflect.InvocationTargetException (no error message)
问题根源
- Hilt版本不一致:根
build.gradle定义的Hilt版本为2.47,但模块build.gradle又重复定义了hilt_version = '2.38.1',导致插件与依赖版本不匹配 - 兼容性不匹配:Kotlin 1.8.10的元数据格式不被旧版Hilt(2.38.1)支持,Hilt 2.44及以上版本才适配Kotlin 1.8系列
修复步骤
- 统一Hilt版本:移除模块
build.gradle中重复定义的hilt_version变量,直接引用根gradle中定义的版本 - 保证版本兼容:保留根gradle中的
hilt_version = '2.47',该版本完全支持Kotlin 1.8.10 - 清理缓存重构:清除Android Studio缓存,执行清理构建命令确保修改生效
修改后的配置文件
根目录 build.gradle
buildscript { ext { compose_ui_version = '1.4.3' kotlin_version = '1.8.10' hilt_version = '2.47' } dependencies { //classpath 'com.google.gms:google-services:4.3.15' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version" classpath 'com.android.tools.build:gradle:7.4.1' } } // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.1' apply false id 'com.android.library' version '7.4.1' apply false id 'org.jetbrains.kotlin.android' version '1.8.10' apply false }
模块目录 build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlin-kapt' //id 'com.google.gms.google-services' id 'dagger.hilt.android.plugin' } android { namespace 'com.wladimirbr.readerbook' compileSdk 33 defaultConfig { applicationId "com.wladimirbr.readerbook" minSdk 28 targetSdk 33 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 '1.4.3' } packagingOptions { resources { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1' implementation 'androidx.activity:activity-compose:1.3.1' implementation "androidx.compose.ui:ui:$compose_ui_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version" implementation 'androidx.compose.material:material:1.2.0' 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_ui_version" debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version" debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version" implementation 'com.google.firebase:firebase-auth-ktx:22.0.0' implementation 'com.google.firebase:firebase-firestore-ktx:23.0.3' implementation 'com.google.android.gms:play-services-auth:20.4.1' implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.0" implementation "androidx.lifecycle:lifecycle-runtime-compose:2.6.0" implementation "androidx.navigation:navigation-compose:2.5.3" implementation "io.coil-kt:coil-compose:2.2.2" implementation "com.google.dagger:hilt-android:$hilt_version" implementation 'androidx.hilt:hilt-work:1.0.0' // para work manager kapt "com.google.dagger:hilt-android-compiler:$hilt_version" kapt "androidx.hilt:hilt-compiler:1.0.0" implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha03" implementation "androidx.compose.material:material-icons-extended:$compose_ui_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2' implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.5.2" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0" implementation("io.coil-kt:coil-compose:1.4.0") // Retrofit implementation 'com.squareup.retrofit2:retrofit:2.9.0' // OkHttp implementation("com.squareup.okhttp3:okhttp:5.0.0-alpha.2") // JSON Converter implementation 'com.squareup.retrofit2:converter-gson:2.9.0' } configurations.all { resolutionStrategy { force 'org.jetbrains.kotlin:kotlin-stdlib:1.8.10' } }
最后操作
修改完成后执行以下步骤:
- 打开Android Studio,点击
File -> Invalidate Caches... -> Invalidate and Restart - 执行终端命令:
./gradlew clean build
内容的提问来源于stack exchange,提问作者wladyband
相关产品推荐
相关产品推荐

