预期 @HiltAndroidApp 具有值。您是否忘记应用 Gradle 插件?

Cyr*_*rus 36 android dagger-2 dagger-hilt

我有谷歌这个问题,但结果对我不起作用。

详情如下。

    public final class App extends com.zhixin.wedeep.common.BaseApplication implements androidx.lifecycle.LifecycleOwner {
                 ^
     // Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?

Run Code Online (Sandbox Code Playgroud)

应用程序代码。

@HiltAndroidApp
class App : BaseApplication(), LifecycleOwner {

    @Inject
    lateinit var service: EventService


    private val mLifecycleRegistry = LifecycleRegistry(this)

}


Run Code Online (Sandbox Code Playgroud)

此模块 gradle 文件。

apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'dagger.hilt.android.plugin'

dependencies {
    implementation rootProject.ext.dependencies["hilt-android"]
    implementation rootProject.ext.dependencies["hilt-lifecycle-viewmodel"]
    kapt rootProject.ext.kapt["hilt-compiler"]
    kapt rootProject.ext.kapt["hilt-android-compiler"]
}
Run Code Online (Sandbox Code Playgroud)

谁有想法?谢谢!

Ste*_*veC 72

我今天早上刚遇到这个问题。您的 build.gradle 中是否有任何内容向 annotationProcessOptions 添加参数?例如:

  android {
        ...
        defaultConfig {
            ...
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = ["room.schemaLocation":
                                 "$projectDir/schemas".toString()]
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果是这样,请尝试从“arguments =" 更改为“arguments +=",因为仅使用 equals 会覆盖之前设置的任何内容。


Ser*_*gio 44

编辑:看起来 kotlin gradle 插件 1.5.21 解决了这个问题,而无需使用下面的 javacOptions。更新 Kotlin 并重试!

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
Run Code Online (Sandbox Code Playgroud)

如果您没有使用 Room 并且仍然收到错误,请将其放在 build.gradle 的 android 块中:

kapt {
    javacOptions {
        // These options are normally set automatically via the Hilt Gradle plugin, but we
        // set them manually to workaround a bug in the Kotlin 1.5.20
        option("-Adagger.fastInit=ENABLED")
        option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 kotlin 1.5.20 上的 kapt 错误:https : //github.com/google/dagger/issues/2684


sit*_*ech 25

解决方案 1:降级 kotlin

如果您正在使用kotlin-gradle-plugin:1.5.20(在您的项目级别build.gradle),将其降级到1.5.10应该可以解决问题。该问题可能会在下一个版本中修复,然后您将升级到新版本。

解决方案 2:禁用 Gradle worker API

将此行添加到您的gradle.properties文件中:

kapt.use.worker.api=false

它将禁用gradle worker API。它对我有用,但正如文档中所说:

使用 worker API 可以让 Gradle 并行运行来自单个项目的独立注释处理任务,这在某些情况下会显着减少执行时间。

因此,通过禁用它,您的构建可能会减慢。


小智 8

只是不要忘记将Hilt 类路径依赖项添加到您的项目级 gradle 文件中:

classpath "com.google.dagger:hilt-android-gradle-plugin:$versions.daggerHiltCoreVersion"
Run Code Online (Sandbox Code Playgroud)

定义特定的版本号而不是上面的$versions.daggerHiltCoreVersion

并将插件添加到您的应用程序级别 gradle:

apply plugin : 'dagger.hilt.android.plugin'

对于以后的 Gradle 版本,添加插件如下

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