Search code examples
androidkotlingradledependency-injectiondagger-hilt

Dagger-hilt error while compiling project


Getting error below while using dagger-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

Please note that I already followed some topics from stackoverflow and other documentation Hilt Unsupported metadata version in Kotlin

Unsupported metadata version. Check that your Kotlin version is >= 1.0: java.lang.IllegalStateException

https://github.com/google/dagger/issues/2379

Using below app gradle configuration

 compileSdk 32

    defaultConfig {
        applicationId "com.test.plantdemo"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"
}

Android plugin

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

App level dependency

   dependencies {
implementation "androidx.work:work-runtime-ktx:2.5.0"
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    implementation 'androidx.hilt:hilt-work:1.0.0'
    kapt "com.google.dagger:hilt-android-compiler:2.35.1"
    kapt "com.google.dagger:hilt-compiler:2.35.1"
    implementation "com.google.dagger:hilt-android:2.35.1"
}

Top level dependency I used

 dependencies {
        // other plugins...
        //classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40'
    }

Top level gradle Plugin

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
    id 'com.google.dagger.hilt.android' version '2.41' apply false
}

Error window shows below error


[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)


step I followed after adding library

Step1: Android application class

@HiltAndroidApp
class PlantApplication: Application() {
}

Step2: Module calss

@Module
@InstallIn(SingletonComponent::class)
object MainModule {
}

Step3: View Model

@HiltViewModel
class PlantListBaseViewModel @Inject constructor(): ViewModel()  {
}

Step4: fragment

@AndroidEntryPoint
class PlantListBaseFragment : Fragment() {
}

Solution

  • I recommend using the latest stable version of Dagger. Mine is working with these versions:

    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
    
    implementation "com.google.dagger:dagger-android-support:2.42"
    implementation "com.google.dagger:hilt-android:2.42"
    
    

    I hope it'll work

    Update:

    If you are curious about the reason, here:

    Looks like Dagger needs to update the kotlin-metadata-jvm library to 0.4.0 which supports reading metadata from Kotlin 1.7, current version is 0.3.0, you might be able to work around the issue by forcing an update on the transitive dep, likely depending on it directly, something like this:

    dependencies {
      //Not a processor, but forces Dagger to use newer metadata lib
      kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.4.2"
    }
    

    you can follow that here: https://github.com/google/dagger/issues/3383