'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!



Solution 1:[1]

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 2:[2]

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.

Solution 3:[3]

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'
}

Solution 4:[4]

Adding to sitatech's answer, I've also encountered this issue using kotlin-grade-plugin-1.5.20. The new 1.5.21 patch solved it for me.

Kotlin Grade Plugin v1.5.21 release notes: https://github.com/JetBrains/kotlin/releases/tag/v1.5.21

Issue in Jetbrains issue tracker: https://youtrack.jetbrains.com/issue/KT-47416

Solution 5:[5]

To backup @SteveC answer, when using Kotlin Gradle DSL is a bit different

We can't use either += or arguments = mapOf(). As stated in the official Dagger-Hilt documentation here & the github issue here regarding the docs as well

See below image for explanations:

arguments = mapOf()

  1. arguments = mapOf() will call setArguments with this.arguments.clear(), thus will overwrite previous argument (in this case Hilt)

Workaround approach:

        javaCompileOptions {
            annotationProcessorOptions {
                arguments(
                    mapOf(
                        "dagger.gradle.incremental" to "true",
                        "room.incremental" to "true"
                    )
                )
            }
        }

Wrapping the arguments() as a functions instead of calling setter, it'll retain the previous arguments as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Ryan M
Solution 4 Karl Jamoralin
Solution 5 mochadwi