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

7 浏览
0 Comments

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

你好,我正在尝试在我的项目中使用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
}

这是我的app模块的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,并重新安装它们,但仍然出现错误。

我该如何解决这个问题?

0
0 Comments

问题:Dagger-Hilt Did you forget to apply the Gradle Plugin? But I applied the plugin

原因:可能是由于未添加annotationProcessor导致的。

解决方法:在app.gradle中添加以下代码:

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

另外,如果使用Kotlin,则不需要添加annotationProcessor。

此外,还可以尝试查看Android Studio中的示例,可能能解决问题。还可以参考stackoverflow上的答案:

[expected hiltandroidapp to have a value did you forget to apply the gradle plu](https://stackoverflow.com/questions/62887817)

如果以上方法都不能解决问题,可以尝试清除缓存并重启Android Studio。

最后,作者发现问题的原因是Kotlin版本1.5.20的bug。

0
0 Comments

问题原因:Kotlin插件1.5.20存在一个bug,导致Dagger Hilt无法正常工作。

解决方法1:在app模块的build.gradle文件中添加以下代码:

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

解决方法2:将Kotlin插件更新到最新版本,并在项目级别的build.gradle文件中将ext.kotlin_version更改为最新版本(当前版本为1.5.21)。

解决方法3:更新Kotlin版本,将classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"添加到build.gradle文件中。

0
0 Comments

Dagger-Hilt是一个用于Android应用程序的依赖注入框架。然而,有时候在使用Dagger-Hilt时,可能会遇到一个错误信息:Dagger-Hilt Did you forget to apply the Gradle Plugin? But I applied the plugin。

这个错误的原因是可能在项目级别的gradle文件中,Dagger-Hilt的版本与当前项目中使用的版本不兼容。为了解决这个问题,我们只需要将gradle文件中的Dagger-Hilt版本更改为最新的版本即可。

具体的解决方法如下:在项目级别的gradle文件中找到以下代码:

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:VERSION_TO_CHANGE"

将`VERSION_TO_CHANGE`替换为最新的Dagger-Hilt版本。然后重新构建项目,问题就会得到解决。

希望以上内容能帮助到你。感谢!

0