DI using dagger-2 android

Sachin Kumar
MindOrks
Published in
3 min readSep 30, 2018

DI has now become the most commonly used word among Android developers. DI stands for Dependency Injection, it’s a design pattern in which a class (dependent) receives the dependencies of other classes in form of an object from another piece of code instead of constructing them internally which simplifies testing and improves decoupling.

Dagger-2 is a dependency injection framework. It uses code generation and is based on annotations. The generated code is easy to read and debug.

It uses the following annotations :

  • @Module and @Provides: define classes and methods which provide dependencies.
  • @Inject: request dependencies. Can be used on a constructor, a field, or a method.
  • @Component: enable selected modules and used for performing dependency injection.

Dagger-2 uses generated code to access the fields and not reflection. Therefore it is not allowed to use private fields for field injection.

Let’s start with the sample project in order to understand DI using Dagger-2 in depth.

Step 1: Add the following dependencies in the build.gradle file (app).

Here, we are using daggerversion: 2.17

Step 2: Add the following files in the package of our project:

AppModule.java :

This file contains the dependency provider which gonna provide the dependencies to the dependent. That’s why we have specified “@Module” above the class name “AppModule”. This class provides appcontext, sharedpreference and resources references to the dependent.

AppComponent.java :

It contains the injection methods specifying the type of dependents which require dependency. Here, in our project, we have two dependents “MainActivity” and “MyApplication” which require dependencies of appcontext, sharedpreference and resources, etc. And yes don’t forget to specify the annotation “@Component” with modules/providers and modules/providers can be multiple.

MyApplication.java :

Extending “Application” class is needed to initialize the “AppComponent’s” reference using “DaggerAppComponents’s” build() method. For now, we have only one module/provider. inject() method is used to inject the dependency to “MyApplication” class.

MainActivity.java :

In this file, we can see the use of “@inject” annotation to get the dependencies without using any constructor or setter method which makes this class decoupled and testable using mockito, etc. Here, we have used the “AppComponent” class’s reference for passing the reference of “MainActivity” using inject method declared in “AppComponent”.

Hence, now we can conclude the following main benefits of DI using Dagger-2 :

  1. Reusable code: As we have configured the dependency externally which increases the reusability of “AppComponent”.
  2. Testable code: Objects/references can now be easily mocked because objects creation will be handled by Dagger-2 which makes the code easily testable.
  3. Readable code: DI has moved the dependencies to the interface of components. This makes it easier to see what dependencies the component has, making the code more readable.

If you have found this article useful to get the basic understanding of DI using Dagger-2, please hit the clap button.

Thanks in advance :)

--

--

Sachin Kumar
MindOrks

Senior Java Backend Dev | Expertise in Java Microservices, Spring Boot Framework & Android apps development.