Hossein Badrnezhad
Hossein Badrnezhad

Reputation: 502

Failed to apply plugin 'dagger.hilt.android.plugin'

When I'm using hilt in my project, I got into this problem:

A problem occurred evaluating project ':app'.
    > Failed to apply plugin 'dagger.hilt.android.plugin'.
        > Could not create plugin of type 'HiltGradlePlugin'.
            > Could not generate a decorated class for type HiltGradlePlugin.
                > com/android/build/gradle/BaseExtension
 Unable to load class 'com.android.build.gradle.BaseExtension'.
 This is an unexpected error. Please file a bug containing the idea.log file.

The main issue is : Failed to apply plugin 'dagger.hilt.android.plugin'.

This is my build.gradle (Module) file :

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

android {
    compileSdk 30

    defaultConfig {
        applicationId "net.holosen.hiltdaggerapp"
        minSdk 21
        targetSdk 30
        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.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


    kapt "com.google.dagger:hilt-android-compiler:2.38.1"

    // Dagger Core
    implementation "com.google.dagger:dagger:2.38.1"
    kapt "com.google.dagger:dagger-compiler:2.38.1"

// Dagger Android
    api 'com.google.dagger:dagger-android:2.38.1'
    api 'com.google.dagger:dagger-android-support:2.37'
    kapt 'com.google.dagger:dagger-android-processor:2.38.1'

// Dagger - Hilt
    implementation 'com.google.dagger:hilt-android:2.38.1'
    kapt 'com.google.dagger:hilt-compiler:2.38.1'

    // For instrumentation tests
    androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
    kaptAndroidTest 'com.google.dagger:hilt-compiler:2.38.1'

    // For local unit tests
    testImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
    kaptTest 'com.google.dagger:hilt-compiler:2.38.1'
}
kapt {
    correctErrorTypes true
}

and my build.gradle (Project) file :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

and this is the settings.gradle file :

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    plugins {
        id 'com.android.application' version '7.1.0-alpha12'
        id 'com.android.library' version '7.1.0-alpha12'
        id 'org.jetbrains.kotlin.android' version '1.5.31'
        id 'dagger.hilt.android.plugin' version '2.38.1'
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "HiltDaggerApp"
include ':app'

My reference is Gradle Build Setup from 'https://dagger.dev/hilt/gradle-setup.html'.

gradle-wrapper.properties file :

#Sun Sep 26 12:17:22 IRST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

Upvotes: 5

Views: 3311

Answers (1)

Hasan Kucuk
Hasan Kucuk

Reputation: 2779

This is the solution I found when I faced the same problem, can you try?

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

Upvotes: 1

Related Questions