185

I was tried to run my code in Kotlin 1.5.10 With plugin as

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

and dependencies as below

dependencies {
    ...
    //Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.33-beta"
    kapt "com.google.dagger:hilt-android-compiler:2.33-beta"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0-beta01"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha01'

    implementation 'com.android.support:palette-v7:28.0.0'
}

When I migrate to kotlin_version = "1.5.10", it just errors out stating

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:186) at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1133) ...

Can anyone help me? I spent a lot of time on it, your answer will help me a lot

4
  • Try to update all hilt libraries to the latest versions Commented May 28, 2021 at 19:23
  • 1
    I was tried, but it has the same issue Commented May 29, 2021 at 4:17
  • 1
    Update your Hilt version to the latest (2.36 at the moment) Commented Jun 1, 2021 at 6:18
  • 1
    it's not working for me Commented Jun 1, 2021 at 9:19

28 Answers 28

306

Go to https://dagger.dev/hilt/gradle-setup check Hilt currently version

Update: For now, you can use the newest version.

  • Kotlin:1.9.0 - Hilt:2.48
  • Kotlin:2.0.0 - Hilt:2.51.1

For Gradle version catalogs: You can use either ksp or kapt; for now, I use ksp.

Project-level build.gradle

plugins {
    /.../
    alias(libs.plugins.compose.compiler) apply false
    alias(libs.plugins.ksp) apply false
    alias(libs.plugins.dagger.hilt) apply false
}

App-level build.gradle

plugins {
    /.../
    alias(libs.plugins.compose.compiler)
    alias(libs.plugins.ksp)
    alias(libs.plugins.dagger.hilt)
}

libs.versions.toml

[libraries]
/.../
hilt = { group = 'com.google.dagger', name = 'hilt-android', version = '2.51.1'}
androidx-hilt = { group = 'androidx.hilt', name = 'hilt-navigation-compose', version = '1.2.0'}
hilt-android-compiler = { group = 'com.google.dagger', name = 'hilt-compiler', version = '2.51.1' }

[plugins]
/.../
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version = "2.0.0-1.0.23" }
dagger-hilt = { id = "com.google.dagger.hilt.android", version = "2.51.1" }
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for the answer,upgrading the hilt library is a nightmare at times.
Also these versions work for me: kotlin:1.6.0 , hilt:2.40.5
This worked for me, I used the answer provided by @NazaninNasab since it has more up to date versions. cheers
I would add: Visit the Gradle Build Setup page and verify EVERYTHING is up to date in your build.gradle files. Android Studio with it's suggestions helped me update only a part, but the Hilt Gradle Plugin was not, so I ended up having new and old version numbers that were causing the error.
Man, android development sucks. You spend more than 50% of the time solving dependencies and compatibility problems than actual work.
|
89

Here how i solved the error:

If you are using Kotlin 1.9.0 then use hilt 2.48

Make these Updates in Hilt version:

in Project-level build.gradle

    plugins{
id("com.google.dagger.hilt.android") version "2.48" apply false
}  

in App-level build.gradle

 plugins{
 id ("kotlin-kapt")
    id("com.google.dagger.hilt.android")
}

dependencies{
     implementation("com.google.dagger:hilt-android:2.48")
        kapt("com.google.dagger:hilt-android-compiler:2.48")
    }

Comments

37

If anyone still facing this issue related to not matching version of kotlin and hilt here is the latest version of hilt vs kotlin that is working for me

project level:

plugins {
    id("com.android.application") version "8.1.2" apply false
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("com.google.dagger.hilt.android") version "2.50" apply false
}

app level:

plugins {
    ...
    id("kotlin-android")
    id("kotlin-kapt")
    id("com.google.dagger.hilt.android")
}

kotlinOptions {
        jvmTarget = "17"
    }

dependencies {

    implementation("androidx.core:core-ktx:1.12.0")

    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.2")

    implementation("com.google.dagger:hilt-android:2.50")
    kapt("com.google.dagger:hilt-android-compiler:2.50")
    kapt("androidx.hilt:hilt-compiler:1.0.0")
    implementation("androidx.hilt:hilt-navigation-fragment:1.0.0")

}

I wasted around 30 hr just to figure out proper latest version and compatibility where I was stuck with the same reported error. hope this will help someone!

ps- update version from 2.48.1 to 2.50

Comments

21

I got the same error. I changed two gradle files and It worked for me.

Project Gradle

plugins {
   
    //    dependencies for dagger hilt
    id 'com.google.dagger.hilt.android' version '2.42' apply false

}

Module Gradle

dependencies {

    implementation 'com.google.dagger:hilt-android:2.42'
    kapt 'com.google.dagger:hilt-compiler:2.42'
    implementation("androidx.hilt:hilt-navigation-fragment:1.0.0")

}

1 Comment

The error is related to compatibility issues between hilt and kotlin. Updating Hilt might work but you will also have to look at Kotlin compatibility whether it needs to be upgraded or not.
19

If you use kotlin version 1.8.0 then

in Project-level build.gradle change hilt version

id 'com.google.dagger.hilt.android' version '2.44' apply false

In App-level build.gradle

implementation "com.google.dagger:hilt-android:2.44"
kapt "com.google.dagger:hilt-compiler:2.44"

Comments

10

Updated: Kotlin DSL

Using dagger version 2.48 with ksp also resolved this issue. Using Java version 17.

build.gradle.kts (project)

plugins {
    ...

    id("com.google.devtools.ksp") version "1.9.10-1.0.13" apply false
    id("com.google.dagger.hilt.android") version "2.48" apply false
}

build.gradle.kts (app)

plugins {
   ...   

   id("com.google.dagger.hilt.android")
   id("com.google.devtools.ksp")
}

...

dependencies {
   ...

   implementation("com.google.dagger:hilt-android:2.48")
   ksp("com.google.dagger:hilt-compiler:2.48")
}

Old Solution

I resolved the error by updating the dependency version to the latest version and adding this additional dependency. I am using java version 17.

kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0"

build.gradle (Project Level)

plugins {
   ...

   id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
   id 'com.google.dagger.hilt.android' version '2.48' apply false
}

build.gradle (App Level)

plugins {
   ...   
   
   id 'kotlin-kapt'
   id 'com.google.dagger.hilt.android'
}

...

dependencies {
   ...

   implementation "com.google.dagger:hilt-android:2.48"
   kapt "com.google.dagger:hilt-compiler:2.48"
   kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0"
}

kapt {
    correctErrorTypes true
}

Comments

6

If any solution solved your problem. Go to https://dagger.dev/hilt/gradle-setup, in Using Hilt with Kotlin section, copy the version mentioned in dependencies and update your build.gradle accordingly

3 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This is the Right way of resolving the error on latest SDK
I would suggest this but only to improve the answer to verify the versions used in gradle as a complement to stackoverflow.com/a/68319048/1084764
6

Adding this line to build.gradle dependencies helped me:

kapt("org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.3.0")

https://youtrack.jetbrains.com/issue/KT-45885

Comments

6

Thanks for the answer , i had to do a slight tweak in order to work for me because i'm using Arctic Fox, hopefully this answer will help as well

Build.gradle (project)

buildscript {
    ext {
        compose_version = '1.0.0'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.0-alpha05'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.37"
    }
}

Build.gradle (app)

//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.34-beta"
kapt "com.google.dagger:hilt-android-compiler:2.34-beta"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'

Comments

4

For Kotlin 1.7.10, you just need to make the hilt versions 2.42.

1 Comment

What if I am not using hilt in my android app?
4

in Project-level build.gradle change hilt version

id 'com.google.dagger.hilt.android' version '2.44' apply false

in App-Level build.gradle

implementation "com.google.dagger:hilt-android:2.44"
kapt "com.google.dagger:hilt-compiler:2.44"

Comments

2

I got same here. I was using dagger:hilt-android:2.33-beta with Kotlin 1.5.10.Please try this

Project gradle

implementation "com.google.dagger:hilt-android:2.33-beta"

Module gradle

plugins {
    ...
    id 'dagger.hilt.android.plugin'
}
dependencies {
    ...
    //dagger-hilt
    implementation "com.google.dagger:hilt-android:2.35.1"
    kapt "com.google.dagger:hilt-android-compiler:2.35.1"
}

Comments

2

You can use

kotlin_version = "1.8.10"
hilt_version = "2.45"

That are the newest versions

Comments

2

check the hilt_version_one is same to hilt_version_two

I get the problem , when my two version is different .

id 'com.google.dagger.hilt.android' version "$hilt_version_one" apply false
implementation "com.google.dagger:hilt-android:$hilt_version_two"
kapt "com.google.dagger:hilt-compiler:$hilt_version_two"

Comments

2

I also got the similar error due incompatible versions of Kotlin and Hilt,

Following combination worked for me:

 kotlin_version =  '2.0.0'
 hilt_version = '2.51.1'

Comments

1

general solution - in AS build console click link at bottom - build with -stacktrace param and find which annotation processor (KAPT) is causing error - then try to update dependency - if you are lucky new version should be available and supporting your gradle version

message in build output you should lookin for

Try:
Run with --stacktrace option to get the stack trace. Run with --info or 
--debug option to get more log output. Run with --scan to get full insights.

1 Comment

That's the correct answer. It's for dependency/library version error and debug option shows the actual reason.
1

I have face this issue twice,

So two possible solution for this problem:

Case 1: If you are using new version of Android such as 1.8 or 1.9

Simply update the version of dagger.hilt into the new one 2.48

Classpath:

plugins {
     id "com.google.dagger.hilt.android" version "2.42" apply false
}

and dependency :

 implementation "com.google.dagger:hilt-android:2.48"
    kapt "com.google.dagger:hilt-compiler:2.48"

Case 2: If you are using previous version of Android such as 1.7 or less than you have to make sure that the classpath and dependency version match, in my case the error was because of different version.

Mismatch:

Classpath:

plugins {
    id "com.google.dagger.hilt.android" version "2.42" apply false
}

and dependency :

 implementation "com.google.dagger:hilt-android:2.48"
    kapt "com.google.dagger:hilt-compiler:2.48"

Correct:

Classpath:

plugins {
    id "com.google.dagger.hilt.android" version "2.42" apply false
}

and dependency :

 implementation "com.google.dagger:hilt-android:2.42"
   kapt "com.google.dagger:hilt-compiler:2.42"

Comments

1

My solution: if you use dagger 2 and hilt in one project, you should keep versions the same

hiltVersion = '2.48.1'
daggerVersion = '2.48.1'

Comments

1

So i am getting the same issue and after a lot of time spend i got to knew it's only version issue. If you are using kotlin version

id("org.jetbrains.kotlin.android") version "1.9.0" apply false

You can find it in your project level gardle.

The hilt version should be in project level gradle

    id("com.google.dagger.hilt.android") version "2.48" apply false

and in app level gradle it should be

 implementation("com.google.dagger:hilt-android:2.48")
 kapt("com.google.dagger:hilt-android-compiler:2.48")

Comments

1

I fixed it by updating my Hilt version

id 'com.google.dagger.hilt.android' version '2.48' apply false
implementation "com.google.dagger:hilt-android:2.48"
kapt "com.google.dagger:hilt-android-compiler:2.48"

I use Gradle version 8.10.2 and Kotlin 1.9.24

Comments

0
Firstly check helt dependency versions with below url

https://dagger.dev/hilt/gradle-setup

Check you kotlin versions

Kotlin version 1.6.0

Project level gradle
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.40"

App level gradle
def hilt_version="2.40"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"


Kotlin version 1.7.0

Project level gradle
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.42"

App level gradle
def hilt_version="2.42"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

Comments

0

I have an issue when upgrading kotlin-gradle-plugin:1.7.x for fulfill requirement of compose. I revert them to org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21 and kotlinCompilerExtensionVersion value

composeOptions {
    kotlinCompilerExtensionVersion compose_version
}

kotlinCompilerExtensionVersion compose_version

Comments

0

In my case, the problem was caused by different versions specified in the dependencies:

  • 2.40 in classpath 'com.google.dagger:hilt-android-gradle-plugin'
  • 2.43.2 in implementation 'com.google.dagger:hilt-android'

Comments

0

In my case it turned out that the Hilt version in the implementation and the classpath were different, making them the same solved the issue. A good practice should be using a version variable.

They were like this: In the build.gradle file of the app module

implementation "com.google.dagger:hilt-android:2.39.1" In the build.gradle file of the project

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.1'

Comments

0

in project level gradle add -

    buildscript {
    dependencies {
    classpath "com.google.dagger:hilt-android-gradle-plugin:2.48.1"
    }
}

Comments

0

Solution for Kotlin Gradle: (for Groovy just use same versions)

This problem is arised basically when your Kotlin version is incompatible with the Kotlin version used in Hilt. So it's not a big deal. Just adjust the versions and that's it 👏👏👏

In your root level Gradle file match the following versions:

plugins {
    id("com.android.application") version "8.2.0" apply false
    id("org.jetbrains.kotlin.android") version "1.9.23" apply false
    id("com.google.dagger.hilt.android") version "2.51.1" apply false
    id("com.google.devtools.ksp") version "1.9.23-1.0.20" apply false
}

and in your app level Gradle file match the following versions for all of those dependencies.

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    kotlin("kapt")
    id("com.google.devtools.ksp")
    id("com.google.dagger.hilt.android")
}

dependencies {
    // Hilt
    implementation("com.google.dagger:hilt-android:2.51.1")
    kapt("com.google.dagger:hilt-android-compiler:2.51.1")
    implementation("androidx.hilt:hilt-navigation-compose:1.2.0")
}

I also mentioned ksp versions along with Hilt and Kotlin because someone may need to use also use ksp in their project which also related to the Kotlin version.

Comments

0

solved by updating gradle url distribution.

Comments

0

Even I have this error, I suggest a way put the exact version they are giving the documentation.

For kotlin 1.9.22:

plugins {
  ...
  id("com.google.dagger.hilt.android") version "2.51.1" apply false
}

//in app.gradle

plugins {
  id("kotlin-kapt")
  id("com.google.dagger.hilt.android")
}

android {
  ...
}

dependencies {
  implementation("com.google.dagger:hilt-android:2.51.1")
  kapt("com.google.dagger:hilt-android-compiler:2.51.1")
}

// Allow references to generated code
// I think this line is the GameChanger. I didn't see this in above response. suggestion and explanation are welcome..

kapt {
  correctErrorTypes = true
}

Hence the Error will be solved

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.