Dagger 2 Android : Defeat the Dahaka
We have had many talks and blog posts on Dagger and Dependency Injection till now. I must say that people have done a great job at explaining various concepts of dependency injection, it’s technical details and also case studies. But, in my opinion, Dagger is something which has become a part of our coding lives now. Yet, from time-to-time we still find ourselves lost in the world of dependencies and dealing with “The Dahaka” (a beast of generated code).
Starting to learn about Dagger? Start by creating a @Module
and a @Component
. Add a @Singleton
scope to it.
Dependencies getting out of hand? Create another dependent component or a sub-component.
Creating another component? You should create another scope!! And if this is not enough Create Qualifiers, use LazyInjection
,ProviderInjection
, staticInjection
,AsyncInjection
, MultiBinding
and what not!
Still alive? Now we present you with Dagger Android and I am sure that it will certainly kill you :P
But wait! What exactly is the relation between a Module and a Component?
We don’t know that. Thankfully! Dagger 2 generates all it’s code at compile time and we can actually see for ourselves what’s going on behind the scenes.
We can see that for our every Component, Dagger generates a DaggerComponent
which has a "has-a”
relationship with our Module.
So far so good? Here comes “The Dahaka” (an infinite storm in an ocean of Providers, Factories, Builders, MemberInjectors, DoubleChecks, Lazy, ...
) You can easily get lost in those classes.
Here’s a sound track to play while reading the quote below 😛
“Most people think dependency injection is like a river that flows swift and sure in one direction, but I have seen the face of dependencies and I can tell you they are wrong. Dependency Injection is an ocean in a storm. You may wonder who I am and why I say this; sit down and I will tell you a tale like none that you have ever heard!” — Quote inspired from Prince of Persia

In this series of blog posts, we will aim to Defeat “The Dahaka” (a storm in the ocean of generated classes) or at least try to tame the beast. We will firstly go over the basics of Dagger2 and try to unleash the beast by going into the generated classes and finding out various patterns. We will then go into the details of those patterns and learn how to apply them. We will then find our way towards the technicalities of Dagger Android. Also how to move towards Dagger Android without blindly applying those annotations and also trying not to die in the hands of “The Dahaka” by putting the leash back on the Beast.
After reading these series of articles you should get a better sense of what goes on behind the scenes and how to smartly move towards Dagger Android and still keep your cool :)
Here’s how we are going to tame the Beast :
- “Codes of Time” : Introduction : We already have really great articles about Dagger2 basics. This will be a collection of my favorites
- “Face of the Dahaka” : Definitions : A sneak peek at the generated code and various Dagger terms and patterns. Let’s swim in the ocean of
Factories
MemberInjectors
ProviderInjections
LazyInjections
AsyncInjection
. - “Unleash the Beast” : Relationships : A Tale of Scopes, Components, Subcomponents, Dependent Components and the Builders.
Dagger 2 : Component Relationships & Custom Scopes
- “Defeat the Dahaka” : Battle : Let’s use the tools at hand like
Builders
@Binds
@BindsInstance
Component.Builder
Subcomponent.Builder
DoubleCheck
Multibinding
@ContributesAndroidInjector
… in order to tame the beast.
Dagger 2 : Check — SingleCheck — DoubleCheck … Scopes
Dagger 2 Annotations : @Binds & @ContributesAndroidInjector
- “Befriend the Beast” : Implementation : Dagger 2 Android 😎!
TL;DR
Checkout the Series of articles with a lot less BS : Here’s the links to the articles :
- Dagger 2 : Component.Builder
- Dagger 2 : Check — SingleCheck — DoubleCheck … Scopes
- Dagger 2 : Component Relationships & Custom Scopes
- Dagger 2 Annotations : @Binds & @ContributesAndroidInjector
I also gave a talk on “Dagger 2 Android : Defeat the Dahaka” at Droidcon Berlin, 2017 😃
Checkout the video :
I am myself an Android Developer facing the “Dahaka” every day. I believe in sharing and having some fun while doing it. I like to relate interesting stories with complex topics so that it’s fun for me to write. Kindly ignore the main article and just focus on the series of articles if it’s not so much fun for you to read it 😛. While going through this series of blog posts if you think that something can be done in a better way, Kindly share it so that we can all befriend the beast together.

Stay tuned for the whole series 🙂