1

Why is the subcomponents = attribute set on a module of a component and not on the component directly? This doesn't seem very intuitive to me so I guess there must be a reason.

@Component(modules = ExampleModule.class)
public interface AppComponent 

@Module(subcomponents = ActivityComponent.class)
public abstract class ExampleModule
Florian Walther
  • 6,237
  • 5
  • 46
  • 104

1 Answers1

2

In a sense, it makes more sense for subcomponents to be on modules: They're private implementation details that are not necessarily exposed publicly, and ones that are exposed can still be listed as builder methods on the component directly. For comparison's sake, you also cannot define @Provides or @Binds methods directly on components; those also affect the implementation details of the component and are not necessarily visible publicly.

However, your point stands; as of April 29, 2019, this is an open and triaged issue (#1463), filed by Google contributor David P. Baker.

We see a pattern of people creating otherwise empty modules just to add subcomponents to components:

[...]

Propose adding subcomponents to dagger.Component, dagger.Subcomponent, dagger.producers.ProductionComponent, and dagger.producers.ProductionSubcomponent.

This would simplify these cases conceptually and reduce boilerplate because there is no need for an empty module.

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • Thank you for the clarification. Is it common to create an empty module just to declare the subcomponent? Or would you generally use an existing module. – Florian Walther Apr 30 '19 at 13:29
  • 1
    @FlorianWalther Entirely your choice, and you can also use [`@Module(includes={...})`](https://stackoverflow.com/q/43769365/1426891) for this. I haven't often seen it on otherwise-empty modules, but that's usually because I've seen each `@Component` get its own adjacent `@Module` for utility bindings and so there's already a convenient file created. I wouldn't hesitate to put it on an otherwise-empty module if nothing else belonged there. – Jeff Bowman Apr 30 '19 at 22:20