63

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.

enter image description here

enter image description here

enter image description here

enter image description here

Feroz Khan
  • 2,396
  • 5
  • 20
  • 37

10 Answers10

59

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

Boken
  • 4,825
  • 10
  • 32
  • 42
Taki
  • 3,290
  • 1
  • 16
  • 41
  • 2
    I added the dependency that you told with `androidProcessors` as I'm using Java but after that I got Gradle DSL method not found: androidProcessors(). So replacing the `androidProcessors` with `annotationProcessor` worked for me. Thanks. – Feroz Khan Jul 13 '20 at 19:32
  • my bad yeah i was supposed to say annotationProcessor , sorry for that and im glad that it worked for you mate , good luck – Taki Jul 13 '20 at 19:34
  • 1
    Is Dagger core and Dagger Android really required while implementing Hilt? – artenson.art98 Dec 27 '21 at 08:24
  • is classpath needed? not mentioned in official docs – htafoya Jan 05 '23 at 01:03
22

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
}
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
  • Additionally, For defining common dependencies like we used to do with global extensions, we just need to define them in gradle.properties and they could easily be shared between project and module. – Kashif K. May 30 '22 at 19:20
  • Really, but the documentation states to add classpath :S – David Jan 14 '23 at 20:29
20

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"
A-run
  • 470
  • 4
  • 13
  • 2
    Yup, this solve my error. The docs at https://developer.android.com/training/dependency-injection/hilt-android?authuser=1#setup , didn't say add "com.google.dagger:hilt-android-compiler:$hilt_version" dependency. – QuartZ Jul 12 '21 at 09:40
12

Keep as it is,

apply plugin: 'kotlin-kapt' or

plugins {
    id 'kotlin-kapt'
}

This will fix your issue

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Anand M Joseph
  • 787
  • 7
  • 7
10

build.gradle (project)

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

build.gradle (:app)

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"
}
Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
4

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.

KE Keronei
  • 323
  • 2
  • 8
3

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")
Void
  • 969
  • 1
  • 11
  • 19
2

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)

htafoya
  • 18,261
  • 11
  • 80
  • 104
1

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!

JeckOnly
  • 347
  • 2
  • 10
1

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

Mahdi Zareei
  • 1,299
  • 11
  • 18