Android Hilt won't work with plugins added

Viewed 1313

I have questions about android hilt.
I have added hilt plugin.

//build.gradle(:project)
buildscript {
   ext.hilt_version = '2.37'
   dependencies {
      ...
      classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
   }
}
//build.gradle(:app)
plugins {
   ...
   id 'kotlin-kapt'
   id 'dagger.hilt.android.plugin'
}

dependencies {
   implementation "com.google.dagger:hilt-android:$hilt_version"
   kapt "com.google.dagger:hilt-compiler:$hilt_version"
}
//MyApplication.kt
@HiltAndroidApp
class MyApplication : Application() {...}

When I build the project, I get the error message saying
"Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?"
Do you have any idea?

3 Answers

I am also having same problem in my new projects. This error had gone after lowering kotlin version to 1.5.10. I think hilt has compatibility issues with latest kotlin plugin.

As mentioned by others, Kotlin 1.5.20 has a bug in Kapt which causes this issue.

It is fixed in 1.5.21. Just increase the version and you're good.

There is a bug in 1.5.20 To avoid this bug there is a workaround. in build.gradle (app) file paste this code below. generally, in the hilt Gradle plugin, these options are set automatically. in 1.5.20 it is not. if you set it manually it will resolve the issue.

kapt {
javacOptions {
    option("-Adagger.fastInit=ENABLED")
    option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}}
Related