期望@HiltAndroidApp 有一个值。您是否忘记应用 Gradle 插件?
Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
我有 Google 这个问题,但结果对我不起作用。
详情如下
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"]
}
谁有想法?谢谢!
我今天早上刚遇到这个问题。您的 build.gradle 中是否有向 annotationProcessOptions 添加参数的内容?例如:
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":
"$projectDir/schemas".toString()]
}
}
}
}
如果是这样,请尝试将“arguments =”更改为“arguments +=”,因为仅使用 equals 会覆盖之前设置的任何内容。
为了备份@SteveC 的回答,当使用 Kotlin 时 Gradle DSL 有点不同
我们不能使用 +=
或 arguments = mapOf()
。正如官方 Dagger-Hilt documentation here & the github issue here 中关于文档所述
见下图解释:
arguments = mapOf()
将用 this.arguments.clear()
调用 setArguments
,因此将覆盖之前的参数(在本例中为 Hilt
)
解决方法:
javaCompileOptions {
annotationProcessorOptions {
arguments(
mapOf(
"dagger.gradle.incremental" to "true",
"room.incremental" to "true"
)
)
}
}
将 arguments()
包装为函数而不是调用 setter,它也会保留之前的 arguments
。
只是不要忘记将 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'
}
编辑:看起来 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
解决方案 1:降级 kotlin
如果您正在使用 kotlin-gradle-plugin:1.5.20
(在您的项目级别 build.gradle
中),将其降级到 1.5.10
应该可以解决问题。
下个版本可能会修复这个问题,然后你就升级到新版本。
解决方案 2:禁用 Gradle 工作人员 API
将此行添加到您的 gradle.properties
文件中:
kapt.use.worker.api=false
它将禁用 gradle worker API。
它对我有用,但正如 documentation:
中所说
Using the worker API lets Gradle run independent annotation processing tasks from a single project in parallel, which in some cases significantly decreases the execution time.
因此,如果禁用它,您的构建速度可能会变慢。
除了 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
我有 Google 这个问题,但结果对我不起作用。
详情如下
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"]
}
谁有想法?谢谢!
我今天早上刚遇到这个问题。您的 build.gradle 中是否有向 annotationProcessOptions 添加参数的内容?例如:
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":
"$projectDir/schemas".toString()]
}
}
}
}
如果是这样,请尝试将“arguments =”更改为“arguments +=”,因为仅使用 equals 会覆盖之前设置的任何内容。
为了备份@SteveC 的回答,当使用 Kotlin 时 Gradle DSL 有点不同
我们不能使用 +=
或 arguments = mapOf()
。正如官方 Dagger-Hilt documentation here & the github issue here 中关于文档所述
见下图解释:
arguments = mapOf()
将用this.arguments.clear()
调用setArguments
,因此将覆盖之前的参数(在本例中为Hilt
)
解决方法:
javaCompileOptions {
annotationProcessorOptions {
arguments(
mapOf(
"dagger.gradle.incremental" to "true",
"room.incremental" to "true"
)
)
}
}
将 arguments()
包装为函数而不是调用 setter,它也会保留之前的 arguments
。
只是不要忘记将 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'
}
编辑:看起来 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
解决方案 1:降级 kotlin
如果您正在使用 kotlin-gradle-plugin:1.5.20
(在您的项目级别 build.gradle
中),将其降级到 1.5.10
应该可以解决问题。
下个版本可能会修复这个问题,然后你就升级到新版本。
解决方案 2:禁用 Gradle 工作人员 API
将此行添加到您的 gradle.properties
文件中:
kapt.use.worker.api=false
它将禁用 gradle worker API。 它对我有用,但正如 documentation:
中所说Using the worker API lets Gradle run independent annotation processing tasks from a single project in parallel, which in some cases significantly decreases the execution time.
因此,如果禁用它,您的构建速度可能会变慢。
除了 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