SingletonComponent 缺少父声明

时间:2021-02-05 00:46:34

标签: android dagger-hilt

我刚刚将我的 android Hilt-Dagger 依赖项更新为 1.0.0-alpha03
我认为 ApplicationComponent弃用
并替换为 SingletonComponent
但是一旦我在我的代码中替换它,它就会显示这个错误 除了更换它,我还需要做其他事情吗?

完全错误

> Task :app:kaptDebugKotlin FAILED
error: [Hilt]
  @DefineComponent dagger.hilt.components.SingletonComponent is missing a parent declaration.
  Please declare the parent, for example: @DefineComponent(parent = ApplicationComponent.class)
  [Hilt] Processing did not complete. See error above for details.error: [Hilt]
  @DefineComponent dagger.hilt.components.SingletonComponent is missing a parent declaration.
  Please declare the parent, for example: @DefineComponent(parent = ApplicationComponent.class)
  [Hilt] Processing did not complete. See error above for details.error: [Hilt]
  @DefineComponent dagger.hilt.components.SingletonComponent is missing a parent declaration.
  Please declare the parent, for example: @DefineComponent(parent = ApplicationComponent.class)
  [Hilt] Processing did not complete. See error above for details.error: [Hilt]
  @DefineComponent dagger.hilt.components.SingletonComponent is missing a parent declaration.
[Hilt]

  Please declare the parent, for example: @DefineComponent(parent = ApplicationComponent.class)
  [Hilt] Processing did not complete. See error above for details.

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

提前致谢。

3 个答案:

答案 0 :(得分:35)

尝试将您的 hilt-android-compiler 更新为

kapt "com.google.dagger:hilt-android-compiler:2.37"

您可以导入 SingleComponent 类,但 kapt 无法正确处理它。

hilt 版本和 kapt 版本匹配很重要。这是我当前的设置。

// HILT
def hilt_version = "2.37"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

答案 1 :(得分:5)

当 Android Studio 建议将 hilt 从 2.28 升级到 2.31 时,这发生在我身上。但它没有建议 kapt。确保两者都是 2.31。

答案 2 :(得分:2)

就我而言,为@mikeBlack 和@user2343647 答案增加价值。确保所有三个都更新为相同的版本号。模块级 build.gradle

implementation "com.google.dagger:hilt-android:2.36"

kapt "com.google.dagger:hilt-android-compiler:2.36"

和项目级 build.gradle

classpath "com.google.dagger:hilt-android-gradle-plugin:2.36"

用最新版本替换 2.36。