murt murt - 4 months ago 16
Android Question

Dagger2. My @Module Object cannot be provided without an @Provides-annotated method

But in fact it is. I'm searched others questions for this issue and noone fix my problem. Error state


network.Repository cannot be provided without an @Provides-annotated method.
network.Repository is injected at
module.LoginModule.provideLoginPresenter(repository)


I use

apt 'com.google.dagger:dagger-compiler:2.5'
compile 'com.google.dagger:dagger:2.5'


My code

ApplicationComponent

@PerApplication
@Component(modules = {ApplicationModule.class, RestModule.class, WebSocketModule.class})
public interface ApplicationComponent {

MyApplication myApplication();
@PerApplication Repository repository(Retrofit retrofit); // testet without arg in constructor as well
WebSocket webSocket();
}


LoginComponent

@PerLogin
@Component(dependencies = {ApplicationModule.class},
modules = {LoginModule.class})
public interface LoginComponent {

void inject(LoginView loginView);

LoginPresenter getLoginPresenter();

}


RestModule

@Module
public class RestModule {


@Provides
@PerApplication
Repository provideRepository(Retrofit retrofit){
return new RetrofitRestRepository(retrofit);
}

[...]
@Provides
@PerApplication
Retrofit provideRetrofit(...){
[...]
return retrofit;
}
}


LoginModule

@Module
public class LoginModule {

@Provides
@PerLogin
public LoginPresenter provideLoginPresenter(Repository repository){
return new LoginPresenter(repository);
}
}

Answer

Should be

dependencies = {ApplicationComponent.class},

instead of

dependencies = {ApplicationModule.class},

And ApplicationComponent should have

 Repository repository();

instead of

 @PerApplication Repository repository(Retrofit retrofit); // testet without arg in constructor as well