I'm getting the error "The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android-compiler
dependency was found." while building the project.
Here's how I'm adding hilt to my project.
I'm getting the error "The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android-compiler
dependency was found." while building the project.
Here's how I'm adding hilt to my project.
I guess you re missing this dependency
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
*First add the plugin
apply plugin: 'dagger.hilt.android.plugin'
*Secondly add the dependencies
// Dagger Core
implementation "com.google.dagger:dagger:2.37"
kapt "com.google.dagger:dagger-compiler:2.37"
// Dagger Android
api 'com.google.dagger:dagger-android:2.37'
api 'com.google.dagger:dagger-android-support:2.37'
kapt 'com.google.dagger:dagger-android-processor:2.37'
// Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.37"
kapt "com.google.dagger:hilt-android-compiler:2.37"
*Thirdly add the classpath
classpath "com.google.dagger:hilt-android-gradle-plugin:2.28-alpha"
PS : if you re using java replace kapt with annotationProcessor
My issue was that I used incorrect dependency syntax when tried adding Hilt dependency to the root build.gradle
in the project using new Gradle syntax id 'pluginId' version 'pluginVersion»' [apply false]
Instead of
buildscript {
...
dependencies {
....
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.41'
}
}
I used
plugins {
...
id 'com.google.dagger.hilt.android' version '2.41' apply false
}
In case you are using this compiler dependency:
"com.google.dagger:hilt-compiler:$hilt_version"
Just change it to:
"com.google.dagger:hilt-android-compiler:$hilt_version"
Keep as it is,
apply plugin: 'kotlin-kapt'
or
plugins {
id 'kotlin-kapt'
}
This will fix your issue
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
No matter how much we apply the plugin, if the following dependencies are not applied, the error is obtained
plugins {
...
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
}
In some scenarios I have encountered, I have a running project in one of my workstations, but when I checkout in another machine, it brings this issue.
It's because the hilt android plugin is applied, but fetching the dependencies has not yet reached the section to download the compiler dependency.
A simple fix is to comment out the id(BuildPlugins.hiltPlugin)
if you are using plugins{}
or apply plugin BuildPlugins.hiltPlugin
, try to sync again, then once you have a successful build(with hilt issues - but the required dependency has been downloaded), you can un-comment the line and sync.
Note:BuildPlugins in this case is an object that holds my plugins definitions.
I solved mine by adding the following dependencies
// Dagger & Hilt
implementation("com.google.dagger:hilt-android:2.40")
kapt("com.google.dagger:hilt-android-compiler:2.40")
implementation("androidx.hilt:hilt-common:1.0.0")
kapt("androidx.hilt:hilt-compiler:1.0.0")
implementation("androidx.hilt:hilt-navigation-fragment:1.0.0")
implementation("androidx.hilt:hilt-work:1.0.0")
be sure to use the following plugin in the app module:
id("dagger.hilt.android.plugin")
instead of
id("com.google.dagger.hilt.android")
which is wrongufully specified in the official docs (as 2023)
everybody! I met this question today, if you have added all the right plugins and dependencies, and you still get an error, ok——it is a bug in Hilt gradle plugin, not your fault, just comment your implementation of Hilt and rebuild your project and uncomment your implementation after build successfully!
maybe you added this:
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
but your forgot to add the dependencies of hilt into your module