59

I get the following warning when I want to use @AndroidEntryPoint which is a property of hilt in my project.

 Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)

When I try to add id 'dagger.hilt.android.plugin' to the module level build.gradle file of my project, I get the following error.

org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'dagger.hilt.android.plugin'] was not found in any of the following sources:

I tried to add it to the build.gradle file at the Module level of the project as follows. They all give an error.

enter image description here

I tried to add it as a classpath to the project level build.gradle file, in this case I still get an error.

enter image description here

When I created the default project, a settings.gradle structure was created as follows. This is my first time using this build. My version of Android Studio Android Studio - Bumblebee | 2021.1.1 Canary 13

enter image description here

Hasan Kucuk
  • 2,433
  • 6
  • 19
  • 41

14 Answers14

122

I am late for the answer. I was also facing the same problem in Android Studio Bumblebee because of the new Gradle syntax for adding dependencies at the project level.

For adding Dagger Hilt in project-level you can use the following syntax:

id 'com.google.dagger.hilt.android' version '2.41' apply false

At the time of writing this, the latest version is 2.41. It is in mavenCentral repository.

Smit Bhanvadia
  • 1,329
  • 2
  • 8
  • 5
43

After a long struggle, I solved the problem as follows;

I added a resolutionStrategy to settings.gradle as below.

 pluginManagement {
        repositories {...}
        plugins { ...}
     resolutionStrategy {
            eachPlugin {
                if( requested.id.id == 'dagger.hilt.android.plugin') {
                    useModule("com.google.dagger:hilt-android-gradle-plugin:2.39.1")
                }
            }
        }
    }

Then, when I added the hilt plugin as below to the module level build.gradle file, it was updated correctly.

plugins{
...
id 'dagger.hilt.android.plugin'
}
Hasan Kucuk
  • 2,433
  • 6
  • 19
  • 41
  • 1
    But why? I have not added it and am still able to use Hilt. This error only comes (in my case) when I convert root `build.gradle` to `build.gradle.kts`. Just trying a bunch of random stuff and pasting here with one random way which coincidentally worked will not help in wrong run – AndroidEngineX Jan 02 '22 at 10:34
  • @AtulGupta in my case the last version of gradle was making definitions in settings.gradle. Since classpath cannot be given in settings.gradle like build.gradle at project level, I solved it with this method. – Hasan Kucuk Jan 02 '22 at 11:17
  • Yup agree...That this particular string sequence definitely solved your issue but it clearly doesn't explain the root cause – AndroidEngineX Jan 02 '22 at 12:57
  • 1
    I have added my [answer](https://stackoverflow.com/a/70556278/5028085) as well which explains why this config is required – AndroidEngineX Jan 02 '22 at 13:17
21

For adding dagger hilt to your project. Follow these steps

Add hilt dependencies to your module's build.gradle. I assume you are using Kotlin, otherwise you have to use annotationProcessor insted of kapt plugin.

dependencies {
  //..
  implementation 'com.google.dagger:hilt-android:2.39.1'
  kapt 'com.google.dagger:hilt-compiler:2.39.1'
  //..
   }

Add hilt gradle plugin to project's build.gradle.

dependencies {
    //..
    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.39.1'
  }

Apply kotlin-kapt and hilt plugins to module build.gradle

plugins {
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}
Praveen
  • 3,186
  • 2
  • 8
  • 23
13

plugin{} block in the root build.gradle is used to define the Gradle plugins that can be applied to root build.gradle and all(or some) the Gradle sub-projects.

The one caveat of using plugin block is that it only resolves plugin that is present in the Gradle plugin portal(see doc) or custom Maven and Ivy plugin repositories must contain plugin marker artefacts in addition to the artefacts which actually implement the plugin(see doc). In the case of the Android Gradle plugin and Hilt plugin, they have not published those plugins to Gradle plugin portal and they have also not published their Plugin Marker Artifacts

Due to the above missing Plugin Marker Artifacts you need to manually resolve the plugin using Plugin Resolution Rules at settings.gradle by adding the below code(this is specific to Hilt Gradle plugin for other you have to check different against requested.id.id)

pluginManagement {
    repositories {
        // Your repo from where Gradle will search for Gradle plugins
    }
    plugins {
        // ...
    }
    resolutionStrategy {
        eachPlugin {
            if(requested.id.id == 'dagger.hilt.android.plugin') {
                    useModule("com.google.dagger:hilt-android-gradle-plugin:${requested.version}")
            }
        }
    }
}

AndroidEngineX
  • 975
  • 1
  • 9
  • 22
11

I faced the same problem, I solved the problem as follows:

First, add the com.google.dagger.hilt.android plugin to your project's root build.gradle file:

plugins {
    id 'com.android.application' version '7.2.0' apply false
    id 'com.android.library' version '7.2.0' apply false
    id 'com.google.dagger.hilt.android' version '2.42' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.0' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Then, apply the Gradle plugin and add these dependencies in your app/build.gradle file:

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

android {
    ....................
    // Enable java 8
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    
    implementation 'com.google.dagger:hilt-android:2.38.1'
    kapt 'com.google.dagger:hilt-android-compiler:2.38.1'
  
}
Samad Talukder
  • 982
  • 11
  • 21
7

Just you need to add this to project's root build.gradle file.

buildscript {
  repositories {
    // other repositories...
    mavenCentral()
  }
  dependencies {
    // other plugins...
    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40'
  }
}
  • 1
    This is the correct solution no need for all the stuff you are told by AndroidEngineX the docs are here https://dagger.dev/hilt/gradle-setup.html – D76X Jan 12 '22 at 07:39
  • @D76X This is an older way to do it. In the latest Android studio, you have to do it using the way AndroidEngineX and other have suggested. – Chirag Savsani Feb 04 '22 at 12:33
4

use this configration in gradle root

         plugins {
         id 'com.android.application' version '7.2.0' apply false
         id 'com.android.library' version '7.2.0' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
        id 'com.google.dagger.hilt.android' version '2.42' apply false
         }

       task clean(type: Delete) {
        delete rootProject.buildDir
         }

and in 2nd gradle file

   plugins {    id 'com.android.application'    id 
   'org.jetbrains.kotlin.android'    id 'kotlin-kapt'    id 
   'com.google.dagger.hilt.android' }
   android {    compileSdk 32
   defaultConfig {
   applicationId "com.example.hilttest"
   minSdk 21
   targetSdk 32
   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 = '1.8'    } }
   dependencies {
   implementation 'androidx.core:core-ktx:1.7.0'    implementation 
   'androidx.appcompat:appcompat:1.4.1'    implementation 
     `enter code here`'com.google.android.material:material:1.6.0'    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'    testImplementation 'junit:junit:4.13.2'    androidTestImplementation 'androidx.test.ext:junit:1.1.3'    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'    implementation 'com.google.dagger:hilt-android:2.42'    kapt 'com.google.dagger:hilt-compiler:2.42'
   // For instrumentation tests    androidTestImplementation  'com.google.dagger:hilt-android-testing:2.42'    kaptAndroidTest 'com.google.dagger:hilt-compiler:2.42'
   // For local unit tests    testImplementation 'com.google.dagger:hilt-android-testing:2.42'    kaptTest 'com.google.dagger:hilt-compiler:2.42' } kapt {    correctErrorTypes = true }
Mohammad Muddasir
  • 947
  • 10
  • 21
1

First, add the hilt-android-gradle-plugin plugin to your project's root build.gradle  file:

buildscript {
    ...
    dependencies {
        def hilt_version = "2.39.1"
        classpath 'com.android.tools.build:gradle:7.0.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}

Then, apply the Gradle plugin and add these dependencies in your app/build.gradle file:

plugins {
  id 'kotlin-kapt' // for annotation processing
  id 'dagger.hilt.android.plugin'
}

android {
    ...
}

dependencies {
    def hilt_version = "2.39.1"
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-compiler:$hilt_version"
}

 
kapt {
    correctErrorTypes true
}

enable Java 8 in your project, add the following to the app/build.gradle

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toSttring()
}
  • You forgot to mention that this solution is almost copy-paste from the official documentation: https://developer.android.com/training/dependency-injection/hilt-android – Boken Jan 13 '23 at 13:24
0

All I need to add was this class path:

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.39.1'
mktowett
  • 11
  • 3
0

if u use gradle kotlin DSL

you must change kapt to ksp https://developer.android.com/build/migrate-to-ksp

0

I also faced the same issue, I solved it by adding com.google.dagger.hilt.android to my root build.gradle()

plugins {
    id 'com.android.application' version '8.0.2' apply false
    id 'com.android.library' version '8.0.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
    id 'com.google.dagger.hilt.android' version '2.46.1' apply false
} 

Then I applied this plug and added hilt dependencies in my app/build.gradle(Module:app)

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


 dependencies{
    ----------------------------------------
      // dagger -hilt
        implementation "com.google.dagger:hilt-android:2.46.1"
        kapt "com.google.dagger:hilt-compiler:2.46.1"
        implementation "androidx.activity:activity-ktx:1.7.2"
    }
0

in build.gradle project level, add this line id 'com.google.dagger.hilt.android' version '2.46.1' apply false

plugins {
    id 'com.android.application' version '8.0.2' apply false
    id 'com.android.library' version '8.0.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
    id 'com.google.dagger.hilt.android' version '2.46.1' apply false
}

Then in build.gradle app level, add this line id 'com.google.dagger.hilt.android'

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'
}
Rezky
  • 41
  • 8
0

If you are using Kotlin and Android Studio Bumblebee 2023.1 or letter version then add the hilt-android-gradle-plugin plugin to your project's root build.gradle file:

plugins {
  ...
  id("com.google.dagger.hilt.android") version "2.44" apply false
}
0

If you, like me, came across this because you were trying to add the dagger plugin into a precompiled convention plugin, none of the other answers will solve the problem. The solution is easy to gloss over, but note the part of gradle's documentation on convention plugins stating:

external plugins need to be added as implementation dependencies before they can be applied in a precompiled script plugin

So, assuming a directory structure of:

├─build-conventions
│ ├─build.gradle.kts
│ ├─settings.gradle.kts
│ ├─src
│ │ ├─main
│ │ │ └─kotlin
│ │ │   ├─myproject.hilt-conventions.gradle.kts
│ │ │   └─[...]
├─app
│ └─[...]
├─build.gradle
├─settings.gradle

Rather than adding the hilt plugin as a versioned plugin in the root build.gradle.kts as you would do to get hilt to function in the :app module, you will instead need to add the following to build-conventions/build.gradle.kts:

// build-conventions/build.gradle.kts

dependencies {
    implementation("com.google.dagger:hilt-android-gradle-plugin:2.47")
}

Then you will be free to apply the hilt plugin as usual to your convention plugin:

// build-conventions/src/main/kotlin/myproject.hilt-conventions.gradle.kts

plugins {
    id("kotlin-kapt")
    id("dagger.hilt.android.plugin")
}
omiwrench
  • 90
  • 1
  • 9