I create dagger2
Conponent
and Subcomponent
Main Component:
@Singleton
@Component(modules = {PresentersModule.class, RepositoriesModule.class, UtilsModule.class, AppModule.class, RoomModule.class})
public interface AppComponent {
SettingsComponent plusSettingsComponent(NetworkModule networkModule);
...
My subcomponent:
@Upgradable
@Subcomponent(modules = {NetworkModule.class})
public interface SettingsComponent {
RestApiFactory getRestApiFactory();
}
I want set RestApiFactory
from subcomponent
like parameter in main component
@Singleton
@Provides
TransactionsRepository provideTransactionsRepository(RestApiFactory restApiFactory) {
return new TransactionsRepositoryImpl(restApiFactory);
}
but RestApiFactory
contains in Subcomponent
Now I make this- in Application class I have method:
public SettingsComponent plusSettingsComponent() {
if (settingsComponent == null) {
settingsComponent = appComponent.plusSettingsComponent(new NetworkModule());
}
return settingsComponent;
}
And call it in repository:
return MyApplication.me().plusSettingsComponent().getRestApiFactory()
.getTransactionsService()
.getTransactions(transactionRequest, page);
if I try set RestApiFactory
like parameter - I get error:
I cahge method to this:
@Singleton
@Provides
TransactionsRepository provideTransactionsRepository(RestApiFactory restApiFactory) {
return new TransactionsRepositoryImpl(restApiFactory);
}
And change this:
private RestApiFactory restApiFactory;
@Inject
public TransactionsRepositoryImpl(RestApiFactory restApiFactory) {
this.restApiFactory = restApiFactory;
}
@Override
public Observable<List<TransactionItem>> loadTransactions(TransactionRequest transactionRequest, int page) {
return restApiFactory.getTransactionsService()
.getTransactions(transactionRequest, page);
}
Error
Error:(30, 27) error: retrofit2.Retrofit cannot be provided without an @Inject constructor or from an @Provides-annotated method.
full error
Error:(30, 27) error: retrofit2.Retrofit cannot be provided without an @Inject constructor or from an @Provides-annotated method.
retrofit2.Retrofit is injected at
my.network.api.RestApiFactory.<init>(retrofit)
my.network.api.RestApiFactory is injected at
my.dagger.RepositoriesModule.provideTransactionsRepository(restApiFactory)
my.repositories.TransactionsRepository is injected at
my.dagger.PresentersModule.provideTransactionsPresenter(…, transactionsRepository)
my.presenters.TransactionsPresenter is provided at
my.dagger.AppComponent.getTransactionsPresenter()
A binding with matching key exists in component: my.dagger.SettingsComponent