Dagger: IllegalArgumentException: No injector factory bound for Class

后端 未结 2 1985
名媛妹妹
名媛妹妹 2020-12-29 18:06

I am new to Dagger 2. I have 2 Activities, I want to use injected ViewModel for both. Here is my ViewModuleFactory :

@Singleton
public class         


        
相关标签:
2条回答
  • 2020-12-29 18:15

    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);
    
    0 讨论(0)
  • 2020-12-29 18:24

    Using the @contrubutesAndroidInjector requires an additional annotationProcessor.

    Add this line to your gradle build file:

    annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
    
    0 讨论(0)
提交回复
热议问题