Example of declaring subcomponent in module annotation @Module(subcomponents = ...)

Viewed 128

What is the use case of declaring subcomponent in dagger module annotation?. e.g.

@Module(subcomponents = ChildComponent.class)
public class ModuleB 

I have read the documentation, but couldn't find any example of usage.

Any {@link Subcomponent}- or {@code @ProductionSubcomponent}-annotated classes which should be
   * children of the component in which this module is installed. A subcomponent may be listed in
   * more than one module in a component.
1 Answers

You can specify Subcomponent classes that should be dependent on Component that your module is child of.

I have component A with module ModuleA and component B. If you specify subcomponents = B.class inside ModuleA, then dagger will not compile giving

error: A doesn't have a @Subcomponent.Builder, which is required when used with @Module.subcomponents
@Module(subcomponents = {B.class})
^

unless component B is subcomponent of A.

Related