问题
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
回答1:
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
todagger.Component
,dagger.Subcomponent
,dagger.producers.ProductionComponent
, anddagger.producers.ProductionSubcomponent
.This would simplify these cases conceptually and reduce boilerplate because there is no need for an empty module.
来源:https://stackoverflow.com/questions/55908839/why-are-dagger-subcomponents-declared-in-a-module-and-not-in-the-parent-componen