Dependency injection in android using dagger2 Part-1

Abhishek Dewangan
4 min readSep 11, 2017

--

What is Dependency Injection :

Dependency Injection is build on the concept on Inversion of Control , which says that class should get dependency from outside , means no class should instantiate another class in place of that get it from some configuration file.

History

There are other Java dependency injection frameworks, many of them suffered limitations in relying on XML(like Spring), required validating dependency issues at run-time, or incurred performance penalties during startup.

Dagger 1 uses Reflection to resolves its dependencies at run-time. This leads to a performance hit, as well as increased difficulty in resolving any bugs or issues (as we know, resolving run time bugs is much harder compared to compile time bugs). Dagger 2 on the other hand, relies heavily on Annotations to define and resolve dependencies. It is easier to traverse dependencies and also catch any potential issues during compile stage.

What it solves :

The most important advantage is that it increases the possibility of reusing the class and to be able to test them independent of other classes (your code will become loosely coupled ).

your code will be more clean and readable because all unnecessary code will be abstracted from you .

you don’t need to write boiler plate code.

Provides Custom Scope so that you can persist data for particular life cycle like (Activity, Fragment, Service).

Now we understand brief about what is dependency injection what problem it can solve … now we will move into how we can use this concept in android using Dagger2.

Dagger2 gradle configuraltion

build.gradle(app)

final DAGGER_VERSION = '2.1'
compile "com.google.dagger:dagger:$DAGGER_VERSION"
annotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION"

Types of Injections you can do via Dagger2

  1. Constructor Injection : we can Inject constructor if Dagger have definition of all the constructor params .
  2. Field Injection : We can inject member variables and variable must not be private.
  3. Method Parameters : Injecting method parameters

How Dependency Injection work in Dagger2 ?

Dependency Provider provides dependency to Dependency Consumer through Connection.

Dependency Provider : classes annotated with @Module is responsible for providing objects which can be injected, This kind of class defines method annotated with @Provides is responsible for providing dependent objects.

Dependency Consumer : the consumer will use @Inject annotation to Inject dependent object.

Connection : Interface annotated with @Component acts as a bridge between Provider and Consumer , and provide relevant dependent object to consumer.

Now we know overview of dagger 2 now let’s discuss brief about few Dagger2 Annotation :

@Module : If you annotated your class with this annotation then this class responsibility is to provide all the dependent object, so whenever you Consumer Inject any object then dagger compiler first check with in Module classes that Object is getting provided or not if not then it will give you compile time error. below is the snippet of Module class.

@Component : This annotation work as a bridge between @Module and @Inject .. to make component we need to create interface annotated with @Component and provide all the function declaration and Dagger will create Concrete class(Start with prefix Dagger then your component interface name).

@Scope : Main Advantage of Dagger 2 is that you can define your scope in which your dependencies will persist , like static in java persist through out the application same like that you can define custom scope which will persist for particular life cycle like Activity, Fragment, Service.

for creating a scope you have to annotate your interface with @Scope….below is the snippet for ActivityScope

@Qualifier : Suppose you have an object which is in multiple scopes than to Identify which scope object you want to use you will use @Qualifier ( you can also use @Named(“name”) annotation in place of @Qualifier ).

The best example of that will be Context Object because Activity Context and Application Context both are different and suppose In Activity there is an adapter which requires Context then we need to provide Activity Context not Application Context, so we can use @Qualifier and get the desired context.

In next post I will demonstrate Dagger2 Implementation with Example …. stay tuned for next post.

Happy Coding.

--

--