【问题标题】:Dagger 2: error: [ComponentProcessor:MiscError] circular dependency with generated codeDagger 2:错误:[ComponentProcessor:MiscError] 循环依赖与生成的代码
【发布时间】:2020-01-28 11:00:13
【问题描述】:

我正在做一个项目,其中旧匕首是在代码库中实现的。今天,我尝试将 dagger 实现优化到 dagger 2.2。如您所知,google 更新了 dagger 库以使该库易于在 android 中实现,有一些帮助类,如 Dagger Activity、Dagger Application 和 Dagger Fragment 类。

我已经更新了库,但我遇到了这样的错误

 error: [ComponentProcessor:MiscError]
 dagger.internal.codegen.ComponentProcessor was unable to process this
 interface because not all of its dependencies could be resolved. Check
 for compilation errors or a circular dependency with generated code.
 public abstract interface NewAppComponent extends
 dagger.android.AndroidInjector<com.mallconnect.tdm.TDMApplication> {

NewAppComponent

@Singleton
@Component(modules = [AndroidSupportInjectionModule::class,
            ActivityBuilderModule::class,
            AppModule::class,
            RoomModule::class,
            RetrofitModule::class,
            MappedInModule::class,
            ViewModelFactoryModule::class
])
interface NewAppComponent : AndroidInjector<TDMApplication> {

    /**
     * Session manager can be access any where in the application
     */
    fun sessionManager(): SessionManager

    @Component.Builder
    interface Builder {

        /**
         * [BindsInstance] annotation is used for, if you want to bind particular object or instance
         * of an object through the time of component construction
         *
         * @param application The application instance
         *
         * @return The Builder
         */
        @BindsInstance
        fun application(application: Application): Builder


        /**
         *
         * @return the AppComponent
         */
        fun build(): NewAppComponent

    }

}

ActivityBuilderModule

@Module
public abstract class ActivityBuilderModule {

    @ContributesAndroidInjector(modules = {MainFragmentBuildersModule.class,
            MainViewModelsModule.class})
    abstract Main contributeMainActivity();

    @ContributesAndroidInjector(modules = {AuthViewModel.class})
    abstract BaseActivity contributeBaseActivity();



}

我查看了一些 StackOverflow 帖子,但没有找到解决方案

Dagger2 Circular Dependency Error

Dagger 2.15: Appcomponent - was unable to process this interface

我们如何追踪错误的根源?

【问题讨论】:

  • 如果不通过all the modules 已包含在component 中,则无法解决该问题!
  • 我添加了 ActivityBuilderModule 所有其他模块都与旧匕首相同
  • 我最好的猜测是AuthViewModel.class 导致了circular dependency。可能是因为它已经被绑定在ViewModelModule 中。尝试从ActivityBuilderModule 中删除AuthViewModel.class 一次并重建
  • 没有。 AuthViewModelModule.class 仅绑定在 ActivityBuilderModule
  • @FazalHussain 你用BaseActivity 扩展你的MainActivity 如果我认为这会导致问题。

标签: android dagger-2


【解决方案1】:

我已经解决了评论中建议的循环依赖问题

如果不查看 component! 中包含的所有模块,则无法解决此问题!

当我错误地通过ActivityBuilderModule 时,我提供的是AuthViewModel 而不是AuthViewModelModule 和内部MainFragmentBuilderModule 我也在证明ViewModel 而不是ViewModelModuleContributeAndroidInjector

【讨论】:

    【解决方案2】:

    对我来说,这是因为,我有一个数据类,其中没有指定顶部的包名,所以,当这个数据类被导入时,它被导入为 import DataClassName 而不是完整的分类名称,因此会导致循环依赖

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-30
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多