Ralph Bergmann
Ralph Bergmann

Reputation: 3076

Dagger 2 component dependencies

Is it possible to inject something into a module?

I have 2 base modules / components:

@Component(modules = {OkHttpModule.class})
public interface OkHttpComponent extends AppComponent {

    OkHttpClient provideOkHttpClient();
}

@Module
public class OkHttpModule {

    @Provides
    OkHttpClient provideOkHttpClient() {

        return mHttpClient;
    }
}
@Component(modules = {GsonModule.class})
public interface GsonComponent extends AppComponent {

    Gson provideGson();
}

@Module
public class GsonModule {

    @Provides
    Gson provideGson() {

        return mGson;
    }
}

With this modules / components I want to create a 3rd module:

@Component(dependencies = {OkHttpComponent.class, GsonComponent.class}, 
           modules = {RetrofitModule.class})
public interface RetrofitComponent extends AppComponent {

    API provideAPI();
}

@Module
public class RetrofitModule {

    @Inject OkHttpClient mHttpClient;
    @Inject Gson         mGson;

    @Provides
    VeentsMeAPI provideVeentsMeAPI() {

        return mVeentsMeAPI;
    }
}

But mHttpClient and mGson are not injected. Is it possible to inject something into a module? And if yes how?

I create the components like this:

okHttpComponent = DaggerOkHttpComponent.builder()
        .okHttpModule(new OkHttpModule(this))
        .build();

gsonComponent = DaggerGsonComponent.builder()
        .gsonModule(new GsonModule())
        .build();

retrofitComponent = DaggerRetrofitComponent.builder()
        .retrofitModule(new RetrofitModule())
        .okHttpComponent(okHttpComponent)
        .gsonComponent(gsonComponent)
        .build();

Upvotes: 12

Views: 9865

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81578

In my opinion, there isn't really a point to injecting into a module. All modules are declared as complete=false, library=true as per the Dagger1 definition of modules, meaning you don't need to specify any real magic as long as you include the modules that are not contained within the component you are using, or make your component dependencies link to each other in such a way that each dependency you can inject is specified only in one component.

You will get the dependencies from other modules in constructor parameters. You need to include the module on the includes list, or you need to have the module specified in your component within the same scope.

The right way to do it is this:

@Module
public class OkHttpModule {
    @Provides
    @ApplicationScope
    public OkHttpClient okHttpClient() {
        return new OkHttpClient();
    }
}

@Module
public class GsonModule {
    @Provides
    @ApplicationScope 
    public Gson gson() {
        return new Gson();
    }
}

@Module(includes={GsonModule.class, OkHttpModule.class}) //look closely, this is the magic
public class RetrofitModule {
    @Provides
    @ApplicationScope
    public VeentsMeAPI veentsMeAPI(Gson gson, OkHttpClient okHttpClient) { //dependencies!
         return RestAdapter.Builder()
            .setClient(new Client(okHttpClient))
            .setConverter(new GsonConverter(gson))
            .create(VeentsMeAPI.class);
    }
}

And then it's

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationScope {}

And maybe even

@ApplicationScope
public class Something {
    private final VeentsMeAPI veentsMeApi;

    @Inject
    public Something(VeentsMeAAPI veentsMeApi) {
        this.veentsMeApi = veentsMeApi;
    }
}

Then

@ApplicationScope
@Component(modules={RetrofitModule.class}) //contains all other modules/dependencies
public interface AppComponent {
    OkHttpClient okHttpClient();
    Gson gson();
    VeentsMeAPI veentsMeAPI();
    Something something();

    void inject(SomeActivity someActivity);
}

and

Then you'd get the generated stuff and you'd have to build it together like so

AppComponent appComponent = DaggerAppComponent.create();

//use appComponent.inject(someActivity); with `void inject(Stuff someActivity);` methods defined in AppComponent

Component dependencies are the same as subcomponents, so those are for subscoping, and not for inheriting dependencies from the same scope. Dependencies of the same scope should be in the same component, just different modules.

Upvotes: 23

Related Questions