Dagger2。如果没有@ Provide-annotated方法,则无法提供我的@Module对象

时间:2016-08-10 11:43:57

标签: android dependency-injection dagger-2

但事实上确实如此。我为这个问题搜索了其他问题,没有人解决我的问题。错误状态

  如果没有@ Provide-annotated方法,则无法提供<。> network.Repository。   network.Repository是注入的   module.LoginModule.provideLoginPresenter(储存库)

我用

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

我的代码

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);
    }
}

1 个答案:

答案 0 :(得分:1)

应该是

dependencies = {ApplicationComponent.class},

而不是

dependencies = {ApplicationModule.class},

ApplicationComponent应该有

 Repository repository();

而不是

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