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

AndroidDagger 2Dagger Hilt

Android Problem Overview


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!

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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 3 - Android

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 4 - Android

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 5 - Android

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 6 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCyrusView Question on Stackoverflow
Solution 1 - AndroidSteveCView Answer on Stackoverflow
Solution 2 - AndroidSergioView Answer on Stackoverflow
Solution 3 - AndroidsitatechView Answer on Stackoverflow
Solution 4 - AndroidOsman YalınView Answer on Stackoverflow
Solution 5 - AndroidKarl JamoralinView Answer on Stackoverflow
Solution 6 - AndroidmochadwiView Answer on Stackoverflow