Dagger 2 Subcomponent or Subcomponent Builder?

When to use which?

Oct 27 · 4 min read

There’s two ways to make Subcomponent, link it through the ParentComponent or link it through the ParentModule. This is shown in the article below

To make Subcomponent by linking through the ParentComponent, also has two ways.

They looks pretty similar. When to choose which approach?


  • Access the ChildComponent itself has much lesser boilerplate code, as it doesn’t need the Builder to be coded. Even if one need to instantiate a module, one can do it as below, without need to make a builder for it ✅
  • Access the ChildComponent’s Builder, one need to make the Builder on the SubComponent as shown below ❎

Other than boilerplate, let’s look into the generated code

  • Access the ChildComponent itself generate lesser codes. The code generated is pretty straightforward ✅
  • Access the ChildComponent’s Builder itself generate additional Builder code. This add another layer of code ❎

As noted in this blog, we could have Parent accessing ChildComponent and its object. (not just the ChildComponent accessing the Parent’s Object)

  • Access the ChildComponent doesn’t allow the ParentComponent bound object to access the ChildComponent directly. The below code will fail ❎

It’ll have to workaround with additional provide

  • Access the ChildComponent’s Builder do allow the ParentComponent bound object to access the ChildComponent ✅
  • Access the ChildComponent, we can’t bind an instance that the ChildComponent has ❎
  • Access the ChildComponent’s Builder, one could bind instance to the ChildComponent easily ✅

Shown above, we can bund the data instance to ChildModule through the Builder.

As seen in comparison 2, both access ChildComponent and its Builder also generate a new copy.

So regardless of which approach, a new object is always provided. Hence object is always duplicated.

To ensure the same object from ChildComponent is generated, keep the ChildComponent (either the one directly get from the ParentComponent, or the one that got build from the ChildComponent’s Builder), set custom scope to the ChildComponent, and also set the custom scope to the object that you like it to persist.

Check out the below for more detail on Scope


If a Reverse Dependencies is needed or additional instance need to be bind into the ChildComponent, use ChildComponent Builder instead. Else use Direct ChildComponent access approach instead of accessing its Builder.

Thanks for reading. You could check out my other topics here.

Follow me on medium, Twitter, Facebook or Reddit for little tips and learning on mobile development etc related topics. ~Elye~


Written by


Learning and Sharing Android and iOS Development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade