Taman Neupane
4 min readFeb 27, 2017

This is my first article. So if i have done any mistakes please let me know.

Dependency Injection(Dagger 2)

Generally nowadays there are some libraries without which a simple network talking android application cannot be created. Some of them are

1. Retrofit 2
2. OkHttp3
3. Picasso
4. Gson

As an android developer you all know what they are and how does they work. Now i am going some what deeper.

Retrofit internally uses Okhttp3 for creating the network call. So simply we can say that retrofit depends on Okhttp3. And if you wanna cache the response from the server okhttp cannot cache the response.So we need File to cache the network response.And after getting response and cacheing it we need to serialize and deserialize the response which means we cast the response to a class and use that class where ever it is required. This work is done by Gson. So now we can just create dependency like : Retrofit depends on Okhttp3 and Gson for working.

So combine these to one module and call it Network Module.

-Network Module
1. Okhttp
2. Gson
3. Retrofit
4. File(cache)

Now another most important library is picasso. As we are using okhttp3 why not use it with picasso as downloader. So lets use okhttp3 with picasso too. Form this we can see the direct relation between picasso and okhttp3. So this will be our another module. Lets call it picasso module.

-picasso module
1. Picasso
2. Okhttp3

Now i think you can clearly see the dependency among these library. So to manage this dependency we need dagger2.

Not like other tutorial i will explain dagger2 more simply not will long theory but with practical explanation. Hope you will get it how i got about the dagger.

Before stepping to Dagger2.You need to know about certain terminology listed below.I will discuss terminology along with problem and solution.

Problem:
As dagger means dependency injection which means create object somewhere else and inject it to an activity or fragment and use it. So now what will provide object for injection and where does it come from?

Solution:
It comes from component. A component is simply interface that provides object for injection to our activity or fragment.

1. Component (@Component)
In simple word it is just interface with some of unimplemented function with the returntype of the object we want and it is bridge between injection(i will discuss about it later) and module(@module). With the help of this component we can inject the dependency to our activity.so now for our case a component is simply interface that provides retrofit and picasso object and we can inject that two object to our activity and use its object directly.

Problem:
Now as i said the object will be inject with the help of component and it is simply interface. And a interface consist of unimplemented function which has the returntype of the object we want. So now the problem is where does the creation of the object takes place?

Solution:
The creation of the object takes place in the module.

2. Module (@Module)
Module is simply public class with annotation @Module.As i said picasso and retrofit object is provided by component but where does it come from? Because our component is interface which contains only unimplemented functions returning Retrofit and picasso object.Module provides that to the component.

To make more clear i will explan working of the Dagger.

Dagger first look into the component (interface) and its unimplemented function and return type of unimplemented functions. Now if we havent made module then dagger shows error. But if we have made module and specified this module in our component then dagger will go searching for that module for the returntype of unimplemented functions. Now the things come into play. The object require by component is created in module in a function.

For our case the retrofit and picasso object is created inside the module and it is provided or injected to activity with the help of component.

Problem:
Every thing happens in run time. Now you are thinking that how dagger search for the required object inside module. The module consist of various functions how dagger knows that?

Solution:
For this dagger has another type of annotation called provide. Dagger will look in to the functions contains @Provide annotation.

3. Provide(@Provide)
As i discussed about module and it contains function that creates object that we require i.e. Retrofit and picasso. When dagger comes for searching from component to module we need to say to dagger that the object you are looking for can be found in the function so we need to write @Provide in that functions.

Problem:
Now the problem is that we have Activity,Fragment in the android. They both have different lifecycle. Some object are required through out the application while some are required with in activity or fragment. How to maintain this using dagger?

Solution: This is maintained by dagger using Scopes.

4. Scope(@Scope)
This helps to maintain reusability of the object.

Problem: As we created module for picasso we know it requires context.where does it comes from?

Solution: for this dagger doesnt have any annotation. But its called external dependency injection. We need to inject context to the picasso module or any other module. For this simply constructor will do our work. We need to create constructor of that module and make context as an argument of that module which we will inject from our application class which you can see form code that i will add later.

Problem: suppose we have two types of context. One application context and activity context. Dagger cannot specify that either the context is application or activity. So how does dagger knows which one is it?

Solution: for this problem dagger has something called qualifier(@Qualifier).

5. Qualifier(@Qualifier)
This help dagger to distinguish between two similar types of object like application context or activity context. It is simply an interface containing @qualifier annotation.