使用Hilt依赖注入时触发kapt编译错误的问题排查
解决Hilt 2.44的kapt编译错误:Unsupported metadata version
问题背景
我是依赖注入(Dependency Injection)新手,正在学习使用Hilt。按照Android官方文档配置了Hilt 2.44依赖,已在Manifest中配置了自定义Application,但运行应用时触发kapt编译错误。
配置与代码
项目级build.gradle.kts
// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { alias(libs.plugins.android.application) apply false alias(libs.plugins.jetbrains.kotlin.android) apply false id("com.google.dagger.hilt.android") version "2.44" apply false }
应用级build.gradle.kts
plugins { alias(libs.plugins.android.application) alias(libs.plugins.jetbrains.kotlin.android) id("kotlin-kapt") id("com.google.dagger.hilt.android") } android { namespace = "com.jihan.dependencyinjection" compileSdk = 34 defaultConfig { applicationId = "com.jihan.dependencyinjection" minSdk = 26 targetSdk = 34 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { isMinifyEnabled = 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" } } dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) implementation(libs.material) implementation(libs.androidx.activity) implementation(libs.androidx.constraintlayout) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) implementation("com.google.dagger:hilt-android:2.44") kapt("com.google.dagger:hilt-android-compiler:2.44") }
应用代码
UserApplication类
package com.jihan.dependencyinjection import android.app.Application import dagger.hilt.android.HiltAndroidApp import javax.inject.Inject @HiltAndroidApp class UserApplication : Application() { @Inject lateinit var userRepository: UserRepository override fun onCreate() { super.onCreate() userRepository.saveUser("jihankhyasjdf","dfhaskjdhfkjsadh") } }
UserRepository类
package com.jihan.dependencyinjection import android.util.Log import javax.inject.Inject class UserRepository @Inject constructor() { fun saveUser(email:String,pass:String){ Log.d("jihankhan","user saved in database") } }
错误信息
Execution failed for task ':app:kaptDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction * Try: > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. * Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin'. ... Caused by: java.lang.IllegalStateException: Unsupported metadata version. Check that your Kotlin version is >= 1.0 ... Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation. BUILD FAILED in 4s 28 actionable tasks: 2 executed, 26 up-to-date
解决方案
这个错误的核心原因是Hilt版本与当前使用的Kotlin版本不兼容。Hilt 2.44对Kotlin版本有特定要求,若你使用的是较新的Kotlin版本(如1.8+),就会触发元数据版本不支持的错误。
解决步骤:
- 升级Hilt到兼容版本:将Hilt版本升级到与你的Kotlin版本匹配的版本。例如,若Kotlin版本是1.8.x,可升级到Hilt 2.48及以上;若为1.9.x,建议使用Hilt 2.50+。
- 修改项目级build.gradle.kts中的Hilt插件版本:
id("com.google.dagger.hilt.android") version "2.50" apply false - 修改应用级build.gradle.kts中的依赖版本:
implementation("com.google.dagger:hilt-android:2.50") kapt("com.google.dagger:hilt-android-compiler:2.50")
- 修改项目级build.gradle.kts中的Hilt插件版本:
- 同步Gradle并清理重建项目:执行
File -> Sync Project with Gradle Files,然后依次执行Build -> Clean Project和Build -> Rebuild Project。
若不确定当前Kotlin版本,可在项目级build.gradle.kts中查看libs.plugins.jetbrains.kotlin.android对应的版本,或在Android Studio的File -> Project Structure -> Modules -> app -> Kotlin Version中查看。
内容的提问来源于stack exchange,提问作者Jihan Khan
相关产品推荐
相关产品推荐

