The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android-compiler dependency was found

Viewed 33911

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

7 Answers

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

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"

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
}

Keep as it is,

apply plugin: 'kotlin-kapt' or

plugins {
    id 'kotlin-kapt'
}

This will fix your issue

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

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.

O 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")
Related