52

When I am using Hilt in android with Room I got this kinda error.

The full log is here:

home/someone/Desktop/Calculator/app/build/tmp/kapt3/stubs/debug/com/hamidjonhamidov/calculator/MyApplication.java:7: error: [Hilt]
public class MyApplication extends android.app.Application {
       ^
  Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
  [Hilt] Processing did not complete. See error above for details./home/someone/Desktop/Calculator/app/build/tmp/kapt3/stubs/debug/com/hamidjonhamidov/calculator/ui/main/MainActivity.java:7: error: [Hilt]

Anyone knows solution for this?

Khamidjon Khamidov
  • 6,783
  • 6
  • 31
  • 62

11 Answers11

73

I had this issue after upgrading Kotlin to 1.5.20.
Adding kapt.use.worker.api=false in gradle.properties worked for me the problem
Checkout dagger issue Support for Kotlin 1.5.20

greybeard
  • 2,249
  • 8
  • 30
  • 66
Mr Wil
  • 741
  • 6
  • 5
57

Fortunately, there is simple solution. In build.gradle in database scheme, we should use arguments += instead of arguments = .

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

Or/And in buld.gradle You should apply plugin like: apply plugin 'dagger.hilt.android.plugin'

This solved the problem)

Khamidjon Khamidov
  • 6,783
  • 6
  • 31
  • 62
34

UPDATE

Upgrade Hilt to v28.1.0 and Kotlin to v1.5.21 should fix this issue

OLD ANSWER

If you are on kotlin 1.5.20, answer of Mr-wil will decrease build speed as said in official doc:

To improve the speed of builds that use kapt, you can enable the Gradle worker API for kapt tasks. 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.

Instead, set:

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")
    }
}

source

Ali Zarei
  • 3,523
  • 5
  • 33
  • 44
24

This generic error message can also appear in many circumstances. As a more generic check, ensure that your module's build.gradle file, ensure that you has:

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

at the top.

goldy1992
  • 893
  • 11
  • 17
18

I had the same problem and it seems like there is a problem in kotlin-kapt plugin. If you guys have tried out all the above answers and didn't get resolved, please try the below code in your build.gradle(module-level) outside the dependencies{} block

kapt {
    javacOptions {
        option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
    }
}
Vijay
  • 575
  • 4
  • 14
17

This was due to a bug in Kotlin 1.5.20. It is fixed in Kotlin 1.5.21.

So all you need to do is to upgrade to the latest version of Kotlin.

Ehsan Khaveh
  • 1,444
  • 17
  • 21
8

in the build.gradle of your Android Gradle modules apply the plugin:

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

android {
  // ...
}

see detail here

raditya gumay
  • 2,951
  • 3
  • 17
  • 24
4

I'm still facing the same issue in 2022

I have solved the problem by adding

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'

to build.gradle project and adding

id 'dagger.hilt.android.plugin'

to plugins in build.gradle app

Mohamed Salama
  • 167
  • 1
  • 13
3

In my case it solved by declaring plugin:

plugins {
    id("dagger.hilt.android.plugin")
}
Mohsents
  • 691
  • 11
  • 9
3

Check if all these are added.In my case I had missed adding one of the below line.

In project level build.gradle

buildscript {
dependencies {
    classpath "com.google.dagger:hilt-android-gradle-plugin:2.44"
  }
}

In app level build.gradle

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}

dependencies {
implementation "com.google.dagger:hilt-android:2.44"
implementation "androidx.hilt:hilt-common:1.0.0"
kapt "com.google.dagger:hilt-android-compiler:2.44"
}
Tarun Anchala
  • 2,232
  • 16
  • 15
0

-In app level build.gradle---add the following in newer versions

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}
  • In project level build.gradle

     buildscript {
         ext.kotlin_version = "1.7.21"
         repositories {
             google()
             mavenCentral()
         }
         dependencies {
             classpath "com.android.tools.build:gradle:4.2.2"
             classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
             classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
    
             // NOTE: Do not place your application dependencies here; they belong
             // in the individual module build.gradle files
         }
    

    }

Tushar
  • 3,527
  • 9
  • 27
  • 49