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

Viewed 18770

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

9 Answers

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.

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

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

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

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

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

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 }

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()
}

All I need to add was this class path:

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.39.1'
Related