Dagger-Hilt 你是否忘记应用Gradle插件?但我已经应用了插件。

3

你好,我正在尝试在项目中使用Dagger Hilt库,但是当我尝试构建它时,会出现以下错误信息:

public final class MyApplication extends android. app.Application {
             ^
  Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)
  See https://dagger.dev/hilt/gradle-setup.html
  [Hilt] Processing did not complete. See the error above for details.

然后一个名为MyApplication.java的Java文件打开,其内容如下:

package com.example.hilt;

import android.app.Application;
import dagger.hilt.android.HiltAndroidApp;
import kotlin.reflect.KClass;

@kotlin.Metadata(mv = {1, 5, 1}, k = 1, d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0007\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002\u00a8\u0006\u0003"}, d2 = {"Lcom/example/hilt/MyApplication;", "Landroid/app/Application;", "()V", "app_debug"})
@dagger.hilt.android.HiltAndroidApp
public final class MyApplication extends android.app.Application {
    
    public MyApplication() {
        super();
    }
}

这是我的项目 build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.5.20"
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

这是我的应用程序模块build.gradle文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'


android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.hilt"
        minSdkVersion 21
        targetSdkVersion 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 = JavaVersion.VERSION_1_8.toString()
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    //Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.37"
    kapt "com.google.dagger:hilt-android-compiler:2.37"
}

kapt {
    correctErrorTypes true
}

就像你所看到的,我做了一切正确的事情,但它仍然显示错误。我在另一台电脑上测试了我的代码,它可以正常工作!我甚至从我的电脑中删除了Android Studio、Gradle和SDK Manager并重新安装了它们,但它仍然显示错误。 我该如何解决这个问题?

4个回答

19

我终于找到解决方案了。在这里,您可以看到一个与Kotlin插件1.5.20相关的问题,并且正如它所解释的那样,这是Kotlin插件的Bug,而不是Dagger Hilt的!

解决方案很简单,您需要将此代码添加到您的build.gradle应用程序模块中:

kapt {
    javacOptions {
        option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
    }
}

编辑:

刚刚将Kotlin插件更新到最新版本,并在项目级别的build.gradle中将ext.kotlin_version更改为最新版本。当前版本是1.5.21


这不是解决方案。我期望Kotlin插件开发人员修复这个问题。 - Dmytro Marchuk
更新 Kotlin 版本后问题得到解决。 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21" - Tobi Oyelekan

1

你没有添加注解处理器,请查看以下链接https://dagger.dev/hilt/gradle-setup.html

在你的app.gradle中添加如下内容

implementation 'com.google.dagger:hilt-android:2.37'
annotationProcessor 'com.google.dagger:hilt-compiler:2.37'

1
我正在使用Kotlin,我不需要注解处理器。 - ali noori
如果你检查Android Studio,他们正在引用你的示例,这可能会解决你的问题。你已经应用了我的答案吗? - Jack Philips
请查看此答案:https://dev59.com/Q1IG5IYBdhLWcg3wzEvT - Jack Philips
是的,我几乎检查了所有与此类错误相关的主题,但没有一个能解决这个错误。 - ali noori
使缓存失效,重新启动,也许会有帮助。 - Jack Philips
我找到了解决方案,它是 Kotlin 版本 1.5.20 的 bug! - ali noori

1

我在新项目中遇到了同样的问题。问题出在gradle路径设置错误。

我在local.properties中指定了以下内容:

org.gradle.java.home=/Users/cmx/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/203.7935034/Android Studio.app/Contents/jre/Contents/Home

它对我很有帮助。


1
只需将项目级别gradle中的下面classpath的版本更改为最新版本即可 -
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:VERSION_TO_CHANGE"

1
谢谢!在正确答案的编辑部分,我也说了这个。 - ali noori

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接