3

I'm using Dagger across several Gradle modules in an android project. I have a lib and app module. Inside lib module I have two classes PrivateThing and ExposedThing. ExposedThing depends on PrivateThing.

I have a dagger @Module to provide these two things:

@Module
public class LibModule {
    @Provides
    ExposedThing provideExposedThing(PrivateThing privateThing) {
        return new ExposedThing(privateThing);
    }

    @Provides
    PrivateThing providePrivateThing() {
        return new PrivateThing();
    }
}

In the app module, I have a single class SomeUiElement which depends on ExposedThing and a separate module:

@Module
public class ApplicationModule {
    @Provides
    SomeUiElement provideSomeUiElement(ExposedThing exposedThing) {
        return new SomeUiElement(exposedThing);
    }
}

And a component to bring everything together:

@Singleton
@Component(modules = {
        ApplicationModule.class,
        LibModule.class
})
public interface ApplicationComponent {
    void inject(SomeActivity activity);

}

Now I want to enforce that nothing in the app module can depend on PrivateThing. I think I'm asking something similar to this question. One solution was to use component dependency. However the documentation recommends subcomponents see "Subcomponents for encapsulation".

Which is the preferred way to do this? Won't either method mean that lib module supplies it's own Component? I thought this was not a best practice, that libraries should only supply a Module.

Jonathan
  • 20,053
  • 6
  • 63
  • 70
tir38
  • 9,810
  • 10
  • 64
  • 107
  • Just being curious, either component dependency and sub-component can't prevent exposing `PrivateThing` to the application component, doesn't it? – jaychang0917 Sep 06 '18 at 02:02

0 Answers0