SingletonComponent 缺少父声明

SingletonComponent is missing a parent declaration

我刚刚将我的 android Hilt-Dagger 依赖项更新为 1.0.0-alpha03
我认为 ApplicationComponentdeprecated
并替换为 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)

提前致谢。

尝试将您的刀柄-android-编译器更新为

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

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

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

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

当 Android 工作室建议将刀柄从 2.28 升级到 2.31 时,我就遇到了这种情况。但它没有建议使用 kapt。确保两者都是 2.31.

就我而言,为@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 替换为最新版本。