I've started to setup Dagger 2 and faced a strange issue that looks like a bug to me.
I have 1 main component and 1 subcomponent which I 'plus' in parent component.
I use:
compile "com.google.dagger:dagger:2.4"
apt "com.google.dagger:dagger-compiler:2.4"
annotationProcessor "com.google.dagger:dagger-compiler:2.4"
Application component is prety easy. It just 'plus' the subcomponent:
@Singleton @Component(modules = { AristocratsAppModule.class })
public interface AristocratsAppComponent {
StreamComponent plus(StreamModule module);
}
Application module also quite basic. It provides application level dependancies:
@Module public class AristocratsAppModule {
private static AristocratsApp app;
public AristocratsAppModule(AristocratsApp app) {
AristocratsAppModule.app = app;
}
@Provides @Singleton AristocratsApp providesApplication() {
return app;
}
@Provides @Singleton GsonXml providesGsonXml() {
return ...
}
@Provides @Singleton @Named("aristocrats") Retrofit providesAristocratsRetrofit() {
return ...
}
@Provides @Singleton @Named("last_fm") Retrofit providesLastFmRetrofit() {
return ...
}
}
Subcomponent has two StreamServices (Retrofit services) and simple inject:
@PerController @Subcomponent(modules = { StreamModule.class }) public interface StreamComponent {
void inject(StreamsController streamsController);
AristocratsStreamService providesAristocratsStreamService();
LastFmStreamService providesLastFmStreamService();
}
Module of subcomponent provides presenter injection and two different injections of Retrofit services:
@Module public class StreamModule {
private StreamsController mView;
public StreamModule(StreamsController view) {
mView = view;
}
@Provides @PerController StreamMvp.Presenter providesPresenter(StreamCase streamCase,
StreamModelMapper streamMapper) {
return new StreamPresenter(mView, streamCase, streamMapper);
}
@Provides @PerController AristocratsStreamService providesAristocratsStreamService(
@Named("aristocrats") Retrofit retrofit) {
return retrofit.create(AristocratsStreamService.class);
}
@Provides @PerController LastFmStreamService providesLastFmStreamService(
@Named("last_fm") Retrofit retrofit) {
return retrofit.create(LastFmStreamService.class);
}
}
Application injection that I call in onCreate() of my app class:
mAppComponent = DaggerAristocratsAppComponent.builder()
.aristocratsAppModule(new AristocratsAppModule(this))
.build();
View injection that I call in my controller:
getAppComponent().plus(new StreamModule(this)).inject(this);
Exception that I'm getting during build:
Error:(13, 8) error: [com.qsoft.streams.presentation.di.StreamComponent.inject(com.qsoft.streams.presentation.StreamsController)] com.qsoft.streams.data.network.AristocratsStreamService cannot be provided without an @Provides-annotated method.
com.qsoft.streams.data.network.AristocratsStreamService is injected at
com.qsoft.streams.data.network.StreamApi.<init>(…, streamServiceAristocrats, …)
com.qsoft.streams.data.network.StreamApi is injected at
com.qsoft.streams.data.repository.StreamRepository.<init>(streamApi, …)
com.qsoft.streams.data.repository.StreamRepository is injected at
com.qsoft.streams.domain.interactors.StreamCase.<init>(streamRepository)
com.qsoft.streams.domain.interactors.StreamCase is injected at
com.qsoft.streams.presentation.di.StreamModule.providesPresenter(streamCase, …)
com.qsoft.streams.presentation.StreamMvp.Presenter is injected at
com.qsoft.streams.presentation.StreamsController.mPresenter
com.qsoft.streams.presentation.StreamsController is injected at
com.qsoft.streams.presentation.di.StreamComponent.inject(streamsController)
A binding with matching key exists in component: com.qsoft.streams.presentation.di.StreamComponent
Error complaints about missing @Provides-annotated method but they are inside my child module. Seems that Dagger2 just does not see them. I was thinking that the problem could be in the missing declaration of the AristocratsStreamService in subcomponent, so I even added it there but nothing changed.
This situation looks very strange and I would like to hear some inputs from more experienced Dagger2 developers.
Thank you in advance!