Dagger2 in Android (Part 1)
I believe one of the most important things in Android development (and other branches of programming) is DI. what is it? well, you can read this article to find out!
DI
DI means “Dependency Injection”, consider a Class that has some values and the class depends on these values to work fine (for example a “Car” that have 4 “Wheals”, these wheels are elements for that class and the car won’t work fine without them), these values are called “Dependencies” and somehow we must set some value for them. one of the ways is initializing them in the constructor, probably you did something like that before, right? well, that’s some kind of Dependency Injection but obviously not a proper way (maybe in the future we have to change the constructor and we use this constructor some places in our codes, do you get the issue? now we have to go back and refine all of those! A REAL PAIN !!! instead of doing that we make a component and that will inject these values into our class.
Dagger2
Now we reach the Android part ! what is Dagger2? Dagger was a library for java and it was made for implementing DI, Google forks this library and make Dagger2, in Android development we use this library for DI, there are two ways for using Dagger2, Java way or Android way, Dagger2 would support both java apps and Android apps, you can use java ways in Android development but when we have a specific way for Android we don’t do it in java way!
Big Picture
As you can see in the picture we have some classes that have some dependencies, we make a component for injecting these dependencies to our classes, this component provides these dependencies by some modules. that’s the architecture of Dagger2, now it’s time for some code!
Component
A component is an interface, take a look at this code :
this is a component, as you can see there is an inject functions and duty of this function is to inject a value into a class, how ? easy like this :
but what happens right now? how it will inject Sth? well, when you build your codes dagger starts to generate some classes and these classes handle this operation, really easy, right? the code I just show you above is not complete and not in Android mode (it’s Java mode) so now we are going to learn how to implement Dagger2 in Android.
Dagger 2 In Android
The first parameter we’ll need entire the code is App class, normally you extend it from Application but if we want to use Dagger we have to make some changes, take a look at this class :
you must extend it from DaggerApplication, we must give an instance of BaseApplication to a component we called it AppComponent, but as you can see in code you have DaggerAppComponent, not AppComponent, what is that ? in Dagger2 when you create a ComponentSomeThing and build your project, Dagger generate some code (i explained it already) and a class with name “Dagger + ComponentSomeThing” will be generated for us, this is one the classes that handle DI for us. in the BaseApplication we give an instance of it to this component like the code above and then we can inject BaseApplication in places we need, here is AppComponent :
AppComponent will extend (actually implement cause it’s an interface) from AndroidInjector and we give a generic type to it, BaseApplication. this interface has a Builder, we want to override this builder and push our BaseApplication into it, the term “@Component.Builder” will do this for us, with “fun application(application: Application): Builder” we give this component an instance of BaseApplication and with build method we will build this component, @BindsInstance is a term for creating an instance of Application while the constructor of AppCompoentn is executing, if you take a look to BaseApplication class you can see “application” and “build” methods. okay ! so far so good, we now make our top-level component. now it’s the time for make a module for this component, take a look at this class :
we provide a Sth class here , now we can use @Inject term when we want Sth in our Activity or … . this module’s owner is AppComponent so we must change our AppComponent to this :
what we should provide by AppModule ? in Dagger2 we can have other components too but AppComponent and AppModule will provide some dependency we’ve need the entire app, something like Retrofit or SharedPreferences.
another thing you should know is when we have a dependency and it depends on another dependency that we already provide, we can write this :
dagger automatically provide Sth for us when we want to provide AnotherThing, it’s like magic!
now we want to inject some dependency to Activity, first of all, you must extend your activity from DaggerAppCompatActivity, now we need to make a component for our activity, take a look at this picture :
some dependencies are for Activity1 and some are Activity2, if a dependency is used in both of them we provide it on AppComponent, you can see “Sub Component” here, a sub-component is a component which is a subset of App Component, it can access all the dependencies App Component provides . to create an Activity Sub Component first we create our activity :
as you see we inject Sth into this activity, now it’s time to define this Activity as a sub Component :
@ContributesAndroidInjector is a term for creating a sub-component in dagger, it will generate a sub-component automatically, as you can see this sub-component can have its modules (Activity1Modeul) and it’s defined in a module (ActivityBuilderModule) which is a module for App Component so we change our AppComponent to this :
we extend activity from DaggerAppCompatActivity and define it as a sub-component of AppComponent and rest of the works handles by dagger, now we can easily access our dependencies like this :
this was my first article about Dagger2, I will write more articles about Dagger2 and I’ll explain Fragment injection, combine Dagger with MVVM and … in next article.
I create a simple project of these content (branch: step1) :
contact me: shobeyri.ali@gmail.com