1

I am using Dagger version 2.24

About this module declaration with subcomponent:

@Module(subcomponents = MySubComponent.class)
abstract class MyModule {
  ...
}

I understand (if I understand wrongly already here, please point out) this code means that classes under MyModule belongs to MySubComponent.

Some tutorial says,

this code can make Dagger automatically detects whether the subcomponent is requested.

What does it mean? Does it mean at compile time Dagger can decide whether this subcomponent is requested by scanning codebase to see if any objects in this module is dependent by any code, if no then there wouldn't be MySubComponent (and it contained objects) be generated at compile time? Otherwise Dagger would then generate MySubComponent(& objects belongs to it)?

Do I understand correctly or do I miss something?

Leem
  • 17,220
  • 36
  • 109
  • 159

1 Answers1

0

Does it mean at compile time Dagger can decide whether this subcomponent is requested by scanning codebase to see if any objects in this module is dependent by any code, if no then there wouldn't be MySubComponent (and it contained objects) be generated at compile time? Otherwise Dagger would then generate MySubComponent(& objects belongs to it)?

I think you mixed up the code generation and object instantiated. The code is always generated (i.e. MySubComponent is always generated by Dagger), but weather is it instantiated is based on if it is needed by the Object that is created by MyModule.

If MyModule is providing an Object that required by the MySubComponent's builder, only when that Object is created, then MySubComponent's builder is instantiated as shown in the example from https://dagger.dev/subcomponents.html

@Singleton
class RequestRouter {
  @Inject RequestRouter(
      Provider<RequestComponent.Builder> requestComponentProvider) {}

  void dataReceived(Data data) {
    RequestComponent requestComponent =
        requestComponentProvider.get()
            .data(data)
            .build();
    requestComponent.requestHandler()
        .writeResponse(200, "hello, world");
  }
}

Upon the creation of the RequestRouter, the Provider<RequestComponent.Builder> requestComponentProvider is given. And only upon the calling of dataReceived function, the Subcomponent Builder (i.e. RequestComponent's builder) is created, that then create the Subcomponent (i.e. RequestComponent).

By having @Module(subcomponents = MySubComponent.class) allow lazy creation of it's subcomponent, hence it is stating the below

this code can make Dagger automatically detects whether the subcomponent is requested.

The statement above is for the purpose of contrasting with another approach of instantiating MySubComponent as stated below

The other approach is, force the subcomponent (or it's builder) to be always instantiated regardless, that's by creating it through the ParentComponent.

@Component
interface MyParentComponent {
    MySubComponent getSubComponent();
}

You could refer to https://medium.com/@elye.project/dagger-2-subcomponent-through-module-or-component-comparison-92486767a861 article for more details between the two.

Hope this clarifies.

Elye
  • 53,639
  • 54
  • 212
  • 474