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
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);
Using the @contrubutesAndroidInjector requires an additional annotationProcessor.
Add this line to your gradle build file:
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'