无法应用插件“dagger.hilt.android.plugin”

Hos*_*had 5 android gradle dagger android-gradle-plugin dagger-hilt

当我在项目中使用hilt时,我遇到了这个问题:

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
Run Code Online (Sandbox Code Playgroud)
 Unable to load class 'com.android.build.gradle.BaseExtension'.
 This is an unexpected error. Please file a bug containing the idea.log file.
Run Code Online (Sandbox Code Playgroud)

主要问题是:无法应用插件“dagger.hilt.android.plugin”。

这是我的 build.gradle(模块)文件:

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
}
Run Code Online (Sandbox Code Playgroud)

和我的 build.gradle (项目)文件:

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

这是settings.gradle 文件:

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'
Run Code Online (Sandbox Code Playgroud)

我的参考是来自'https://dagger.dev/hilt/gradle-setup.html'的Gradle Build Setup

gradle-wrapper.properties 文件:

#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
Run Code Online (Sandbox Code Playgroud)

Has*_*cuk 1

这是我遇到同样问题时找到的解决方案,你可以尝试一下吗?

我在settings.gradle中添加了一个resolutionStrategy,如下所示。

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

然后,当我将如下所示的 hilt 插件添加到模块级build.gradle文件中时,它已正确更新。

plugins{
...
id 'dagger.hilt.android.plugin'
}
Run Code Online (Sandbox Code Playgroud)