Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?

I have Google this problem, but the results are not work for me.

The detail as following.

    public final class App extends com.zhixin.wedeep.common.BaseApplication implements androidx.lifecycle.LifecycleOwner {
                 ^
     // Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?

The App code.

@HiltAndroidApp
class App : BaseApplication(), LifecycleOwner {

    @Inject
    lateinit var service: EventService


    private val mLifecycleRegistry = LifecycleRegistry(this)

}


This module gradle file.

apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'dagger.hilt.android.plugin'

dependencies {
    implementation rootProject.ext.dependencies["hilt-android"]
    implementation rootProject.ext.dependencies["hilt-lifecycle-viewmodel"]
    kapt rootProject.ext.kapt["hilt-compiler"]
    kapt rootProject.ext.kapt["hilt-android-compiler"]
}

Who has ideas? Thanks!


I just hit this problem this morning. Do you have anything in your build.gradle that adds arguments to the annotationProcessOptions? For example:

  android {
        ...
        defaultConfig {
            ...
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = ["room.schemaLocation":
                                 "$projectDir/schemas".toString()]
                }
            }
        }
    }

If so, try changing from "arguments =" to "arguments +=", as just using equals overwrites anything set previously.


EDIT: Looks like kotlin gradle plugin 1.5.21 solves the problem without using the bellow javacOptions. UPDATE Kotlin and try again!

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"

If you are not using Room and still get the error put this in the android block of build.gradle:

kapt {
    javacOptions {
        // These options are normally set automatically via the Hilt Gradle plugin, but we
        // set them manually to workaround a bug in the Kotlin 1.5.20
        option("-Adagger.fastInit=ENABLED")
        option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
    }
}

It's a kapt bug on kotlin 1.5.20: https://github.com/google/dagger/issues/2684


SOLUTION 1 : Downgrade kotlin

If you are using kotlin-gradle-plugin:1.5.20 (in your project level build.gradle), downgrading it to 1.5.10 should fix the issue. The issue will probably be fixed in the next versions, then you will upgrade to the new version.

SOLUTION 2 : Disable Gradle worker API

Add this line to your gradle.properties file:

kapt.use.worker.api=false

It will disable the gradle worker API. It works for me, but as said in the documentation:

Using the worker API lets Gradle run independent annotation processing tasks from a single project in parallel, which in some cases significantly decreases the execution time.

So by disabling it, your build may be slowed down.


Just don't forget to add Hilt classpath dependency to your project level gradle file:

classpath "com.google.dagger:hilt-android-gradle-plugin:$versions.daggerHiltCoreVersion"

Define the specific version number instead of $versions.daggerHiltCoreVersion above.

And add plugin to your app level gradle:

apply plugin : 'dagger.hilt.android.plugin'

For later Gradle versions, add the plugin as follows

plugins {
    id 'dagger.hilt.android.plugin'
}