1

您好,我正在尝试在我的项目中使用 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 管理器并重新安装它们,但它仍然给我错误。我该如何解决?

4

3 回答 3

12

我终于找到了解决方案。在这里您可以看到与支持 Kotlin 插件 1.5.20 相关的问题,因为它解释了这是 Kotlin 插件的错误,而不是匕首的错误!

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

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

编辑:

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

于 2021-07-05T10:32:10.127 回答
1

只需将项目级别 gradle 中类路径下方的版本更改为最新版本 -

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:VERSION_TO_CHANGE"
于 2022-02-12T15:41:36.833 回答
0

您没有添加 annotationProcessor 检查以下链接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'
于 2021-07-05T09:59:36.363 回答