'Dagger-Hilt Did you forget to apply the Gradle Plugin? But I applied the plugin
Hello I'm trying to use the Dagger Hilt library In my project but when I'm trying to build it, it fails with this message
public final class MyApplication extends android. app.Application {
^
Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)
See https://dagger.dev/hilt/gradle-setup.html
[Hilt] Processing did not complete. See the error above for details.
And then a java file called MyApplication.java opens with the below content:
package com.example.hilt;
import android.app.Application;
import dagger.hilt.android.HiltAndroidApp;
import kotlin.reflect.KClass;
@kotlin.Metadata(mv = {1, 5, 1}, k = 1, d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0007\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002\u00a8\u0006\u0003"}, d2 = {"Lcom/example/hilt/MyApplication;", "Landroid/app/Application;", "()V", "app_debug"})
@dagger.hilt.android.HiltAndroidApp
public final class MyApplication extends android.app.Application {
public MyApplication() {
super();
}
}
this is my project build.gradle
file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.20"
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.37'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And this is my app module build.gradle
file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.hilt"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.37"
kapt "com.google.dagger:hilt-android-compiler:2.37"
}
kapt {
correctErrorTypes true
}
as you see I did everything correctly and it still gives me the error. I tested my code on another computer and it worked! I even removed android studio, Gradle, and SDK manager from my computer and reinstall them but it still gives me the error. How can I fix it?
Solution 1:[1]
I finally found the solution. Here you can see an issue related to support for Kotlin's plugin 1.5.20 and as it explains this is the bug of Kotlin's plugin, not dagger hilt's!
The solution is simple, you have to add this code to your build.gradle app module:
kapt {
javacOptions {
option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}
}
Edit:
Just update the Kotlin plugin to the latest version and change ext.kotlin_version
to the latest version in project-level build.gradle. The current version is 1.5.21
.
Solution 2:[2]
Just change the version of this below classpath in your project level gradle to the latest version -
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:VERSION_TO_CHANGE"
Solution 3:[3]
you did not add annotationProcessor check following link https://dagger.dev/hilt/gradle-setup.html
add like this in your app.gradle
implementation 'com.google.dagger:hilt-android:2.37'
annotationProcessor 'com.google.dagger:hilt-compiler:2.37'
Solution 4:[4]
I have the same problem on fresh project. And problem was in wrong gradle path.
I specified in local.properties
this:
org.gradle.java.home=/Users/cmx/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/203.7935034/Android Studio.app/Contents/jre/Contents/Home
And it helps me.
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 | Prateek Sharma |
Solution 3 | Jack Philips |
Solution 4 | Pavel Shorokhov |