62

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!

Cyrus
  • 8,995
  • 9
  • 31
  • 58

7 Answers7

119

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.

SteveC
  • 1,594
  • 2
  • 14
  • 14
  • This work for me, because i dont add ``` annotationProcessorOptions { arguments += ["room.schemaLocation": "$projectDir/schemas".toString()] }``` to my module build.gradle file. Documents don't give any prompt that above codes should be added to build.gradle if using ```Room```. – Cyrus Jul 14 '20 at 08:46
  • for Kotlin DLS it could be : `arguments = arguments + mapOf("room.incremental" to "true")` – i30mb1 Aug 06 '20 at 16:21
  • 1
    one can also use this syntax `arguments["room.schemaLocation"] = ... arguments["room.incremental"] = true` to add as many arguments as they want, but with the above answer, this is most prolly not needed – Ace Nov 17 '20 at 15:55
  • Thank you, it does say this in the [docs](https://dagger.dev/hilt/gradle-setup) but I skimmed it completely – Daniel Wilson Sep 27 '22 at 09:13
  • Nice! Just ran into this now and I don't think I ever would have guess this was the issue. Thanks! – DataGraham May 11 '23 at 23:42
65

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

Sergio
  • 2,346
  • 2
  • 24
  • 28
32

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.

sitatech
  • 1,311
  • 11
  • 17
13

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'
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Osman Yalın
  • 620
  • 7
  • 7
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

Karl Jamoralin
  • 1,240
  • 1
  • 14
  • 27
3

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.

mochadwi
  • 1,190
  • 9
  • 32
  • 87
0

in my case, multi module in presentaion layer, I just removed :

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

in defaultConfig block

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44