0

Hello I am new to Dagger2. Goal. Take my Networking DI and MVP DI. MVP as in the presenter for an an activity that extends base activity. I want to combine all this into one super module and place this into my base activity. Over time add more presenters.

I do not want 30+ inject statements in my baseActivity. I am following this example but it is too simple compared to what I am trying to do.

I think the issue is with injecting the API at the base activity. For some reason Dagger is looking for Api in my MVP class.. So that would be a dependency graph issue?

Having spent more time on this.. The issue stems from Mvp's interface of baseActivity or any sub activity that extends baseActivity. That means when it goes to inject, it sees the @inject Api call, and cannot find it. It will work if I add Api to this module, but thats upside down of what I want. I want Component / Module for Application level items. I then want a component / module that has all my different MVP component in one module.. It's like Dagger starts looking for dependencies in the leaf of a tree and getting upset when it doesn't see whats in the root. I need it to go the other way. Be satisfied that I injected the dependency in the Root activity.

Base Activity...

@inject
public ApiClient mClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mManager = new SharedPreferencesManager(this);
    DaggerInjector.get().inject(this);
}

DaggerInjector

public class DaggerInjector {
private static AppComponent appComponent = DaggerAppComponent.builder().appModule(new AppModule()).build();

public static AppComponent get() {
    return appComponent;
}
}



@Component(modules = {AppModule.class,  ApiModule.class, MvpModule.class})
@Singleton
public interface AppComponent {

    void inject(BaseActivity activity);


}

Api

@Singleton
@Component(modules = {ApiModule.class})
public interface ApiComponent {
    void inject( BaseActivity activity);
}



@Module
public class ApiModule {

    @Provides
    @Singleton
    public ApiClient getApiClient(){
        return new ApiClient();
    }
}

Mvp

@Singleton
@Component(modules = {MvpModule.class})
public interface MvpComponent {
    void inject(BaseActivity activity);
}

@Module
public class MvpModule {

    @Provides
    @Singleton
    public MvpPresenter getMvpPresenter(){ return new MvpPresenter();}
}



Error:(16, 10) error: ApiClient cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. This type supports members injection but cannot be implicitly provided.
ApiClient is injected at
...BaseActivity.ApiClient
...BaseActivity is injected at
MvpComponent.inject(activity)
4

1 回答 1

0

我发现了我的问题。我需要使用一个子组件。

@Singleton
@Subcomponent(modules = {MvpModule.class})
public interface MvpComponent {
    void inject(BaseActivity activity);
}

@Module
public class MvpModule {

    @Provides
    @Singleton
    public MvpPresenter getMvpPresenter(){ return new MvpPresenter();}
}

Dagger-我们是否应该为每个活动/片段创建每个组件和模块

Dagger2 活动范围,我需要多少个模块/组件?

于 2017-02-16T21:47:45.387 回答