【发布时间】: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如果我认为这会导致问题。