Dagger: IllegalArgumentException: No injector factory bound for Class

两盒软妹~` 提交于 2019-11-29 05:24:27
azizbekian

I believe you have forgot to put @ContributesAndroidInjector annotation:


    @Module
    public abstract class ActivityModule {
        @ContributesAndroidInjector
        abstract ProductListActivity contributeProductListActivity();
        @ContributesAndroidInjector
        abstract ProductDetailsActivity contributeProductDetailsActivity();
    }

And include ViewModelModule within AppModule:


    @Module(includes = ViewModelModule.class)
    class AppModule {
        ...
    }


See this code that you have wrote:

@Provides
@Singleton
ProductListRepository provideProductListRepository(ProductListRepository repository) {
    return repository;
}

What do you expect to happen? You are telling dagger "hey, dagger, whenever I ask you to provide me ProductListRepository then create(return) that object using ProductListRepository". That's not gonna work out.

Most possibly what you intended was "hey, dagger, whenever I ask you to provide me an implementation of ProductListRepository then create(return) that object using ProductListRepositoryImpl":

@Provides
@Singleton
ProductListRepository provideProductListRepository(ProductListRepositoryImpl repository) {
    return repository;
}

Which may be substituted with following:

@Binds
@Singleton
abstract ProductListRepository provideProductListRepository(ProductListRepositoryImpl repository);
DivineChaos

Using the @contrubutesAndroidInjector requires an additional annotationProcessor.

Add this line to your gradle build file:

annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!