android - 应用插件失败 'dagger.hilt.android.plugin'

标签 android gradle android-gradle-plugin dagger dagger-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
 Unable to load class 'com.android.build.gradle.BaseExtension'.
 This is an unexpected error. Please file a bug containing the idea.log file.
主要问题是:无法应用插件“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
}
和我的 build.gradle (项目)文件:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}
这是 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'
我的引用是 Gradle build设置 来自“https://dagger.dev/hilt/gradle-setup.html”。
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

最佳答案

这是我遇到同样问题时找到的解决方案,你可以试试吗?
我添加了 解决策略 settings.gradle 如下。

 pluginManagement {
        repositories {...}
        plugins { ...}
     resolutionStrategy {
            eachPlugin {
                if( requested.id.id == 'dagger.hilt.android.plugin') {
                    useModule("com.google.dagger:hilt-android-gradle-plugin:2.39.1")
                }
            }
        }
    }
然后,当我将如下所示的 hilt 插件添加到模块级别 build.gradle 文件,它已正确更新。
plugins{
...
id 'dagger.hilt.android.plugin'
}

关于android - 应用插件失败 'dagger.hilt.android.plugin',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69338548/

相关文章:

javascript - 调用nodejs文件的onclick函数

android - 控制 Activity

android - 未使用导航组件将 fragment 添加到后台堆栈

java - 当我尝试运行我的Android应用程序时,Gradle失败,原因为空白

gradle - 哪个 Maven BOM 确定 Gradle 5 中的依赖项版本?

actionbarsherlock - Android Studio 项目无法解析 ActionBarSherlock 导入

java - 编译运行时出错[NoClassDefFoundError]

java - Gradle 无法从本地 Artifact 下载外部依赖项,应从 mavenCentral 下载外部依赖项

android - 引用同一个项目中的一个类,不同的模块

Android jitpack 不会从我的仓库构建