Android Dagger2 ComponentProcessor 无法处理

Android Dagger2 ComponentProcessor was unable to process

我正在尝试使用 Dagger2 制作多模块项目。你可以看到我的代码following the link。在 main 分支中,所有匕首 类 都在 presentation 模块中的工作解决方案。

现在我正在尝试为 DI 根创建单独的 app 模块。您可以看到最近的尝试 in the develop branch。它不工作。我想在 app 模块中创建我的根 ApplicationComponent 组件,并从其他模块添加 PresentationComponent (子)组件。每次我尝试一些东西,最终我都会得到关注:

dagger.internal.codegen.ComponentProcessor was unable to process 'ru.ircover.schultetables.app.ApplicationComponent' because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code

这很奇怪,因为与 main 分支相比,我没有更改任何依赖项。唯一改变的是 - 新 link 从根组件 ApplicationComponent 到子组件 PresentationComponent。据我了解,没有办法将循环依赖显示为 app 模块,并且其内容在 presentation 模块中不可见。我遵循了我在 google 中遇到的所有教程,但其中 none 对我有所帮助。

我已经尝试过的:

  1. 添加到子组件的根组件 getter。
  2. 添加到子组件的根组件 BuilderFactory
  3. 添加到根组件特殊模块 subcomponent link.
  4. 将子组件的 inject 方法移动到父组件。

现在我卡住了。似乎问题出在组件或模块连接的某个地方,但我无法找到它发生的位置和方式。我需要帮助。

您需要添加 dagger.android 支持库,因为您的 moxy.MvpAppCompatFragment 是 built on Android Support Fragments。将此添加到您的演示文稿 gradle 文件中:

implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"

来自您的开发分支的 presentation/build.gradle:

implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"

来自Gradle setup

If you're using classes in dagger.android you'll also want to include:

implementation 'com.google.dagger:dagger-android:2.x'
implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'

我已经通过这些更改成功构建了您的项目:

  1. 左边是一个演示文稿,右边是一个应用程序,如您所见,我已经删除了 dagger-android,并将 moxy deps 设为 API(因为您的应用程序是一个主模块,它必须看到所有的 deps 来构建一个图表),或者你可以只添加 moxy deps 到应用程序
  2. 之后,将不会有任何匕首警告,但您需要修复一些文件才能完成构建。顺便说一句,将某些东西传递给模块的构造函数是一种糟糕且已弃用的做法。正如您在 PresentationModule 中看到的那样,我删除了构造函数并删除了 provideContext。我将一个 Context 绑定移动到 ApplicationComponent - 这是做这件事的好地方。
  3. 而且我还从 PresentationComponent.Factory.create
  4. 中删除了构造函数参数

基于Google documentation你可以这样做:

1- 添加@HiltViewModel 注解到 class

2- 将构造函数上的@ViewModelInject 注解替换为@Inject。

3- 从 SavedStateHandle 构造函数参数中删除 @Assisted(如果存在)

4- 从 build.gradle 文件中删除旧的 androidx.hilt:hilt-lifecycle-viewmodel 依赖项