【问题标题】:Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?预期 @HiltAndroidApp 具有值。您是否忘记应用 Gradle 插件?
【发布时间】:2020-11-03 08:31:15
【问题描述】:

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

详情如下。

    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?

应用代码。

@HiltAndroidApp
class App : BaseApplication(), LifecycleOwner {

    @Inject
    lateinit var service: EventService


    private val mLifecycleRegistry = LifecycleRegistry(this)

}


这个模块的 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"]
}

谁有想法?谢谢!

【问题讨论】:

  • Kotlin 升级到 1.5.20 后也会出现此问题 (github.com/google/dagger/issues/2684) 在gradle.properties 中添加kapt.use.worker.api=false 即可解决问题。
  • 由 Kotlin 1.5.21 修复
  • kotlin 升级到1.5.21后不修复

标签: android dagger-2 dagger-hilt


【解决方案1】:

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

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

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

【讨论】:

  • 这对我有用,因为我不添加 ``` annotationProcessorOptions { arguments += ["room.schemaLocation": "$projectDir/schemas".toString()] }``` 到我的模块build.gradle 文件。如果使用Room,文档没有提示上述代码应该添加到build.gradle中。
  • 对于 Kotlin DLS 可能是:arguments = arguments + mapOf("room.incremental" to "true")
  • 人们也可以使用这种语法arguments["room.schemaLocation"] = ... arguments["room.incremental"] = true 来添加任意数量的参数,但是对于上述答案,这几乎不需要
  • 非常感谢!
【解决方案2】:

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

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

如果您没有使用 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")
    }
}

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

【讨论】:

  • 谢谢。它解决了我的问题。
【解决方案3】:

解决方案 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。 它对我有用,但正如documentation 中所说:

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

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

【讨论】:

  • 降级 kotlin 是我的解决方案,谢谢
  • Solution2 对我有用 :)
  • 是的,解决方案 2 也适用于我。
【解决方案4】:

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

classpath "com.google.dagger:hilt-android-gradle-plugin:$versions.daggerHiltCoreVersion"

定义具体的版本号,而不是上面的$versions.daggerHiltCoreVersion

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

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

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

plugins {
    id 'dagger.hilt.android.plugin'
}

【讨论】:

    【解决方案5】:

    除了 sitatech 的回答之外,我还使用kotlin-grade-plugin-1.5.20 遇到了这个问题。新的1.5.21 补丁为我解决了这个问题。

    Kotlin Grade Plugin v1.5.21 发行说明:https://github.com/JetBrains/kotlin/releases/tag/v1.5.21

    Jetbrains 问题跟踪器中的问题:https://youtrack.jetbrains.com/issue/KT-47416

    【讨论】:

      【解决方案6】:

      要备份@SteveC 的答案,使用 Kotlin Gradle DSL 时有点不同

      我们不能使用+=arguments = mapOf()。正如官方 Dagger-Hilt documentation heregithub issue here 关于文档所述

      解释见下图:

      1. arguments = mapOf() 将使用 this.arguments.clear() 调用 setArguments,因此将覆盖先前的参数(在这种情况下为 Hilt

      解决方法:

              javaCompileOptions {
                  annotationProcessorOptions {
                      arguments(
                          mapOf(
                              "dagger.gradle.incremental" to "true",
                              "room.incremental" to "true"
                          )
                      )
                  }
              }
      

      arguments() 包装为函数而不是调用setter,它也会保留以前的arguments

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-24
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多