Dagger on Android

Dagger, is a Dependency Injection library that was developed by Square and is currently being developed by Google. Dependency Injection is the process of passing dependency to other objects or framework (dependency injector). Dependency injection makes testing easier. The injection can be done through the constructor.

To use the Dagger library in your Android project, you must first add the following dependencies.

implementation 'com.google.dagger:dagger:2.16'
annotationProcessor 'com.google.dagger:dagger-compiler:2.16'

First, I will explain the annotations used in Dagger.

Suppose we have a structure like this.

public class Picture {

private Brush brush;
private CanvasPaper paper;
private Paint paint;

public Picture(Brush brush, CanvasPaper paper, Paint paint) {
this.brush = brush;
this.paper = paper;
this.paint = paint;

// getters and setters

When we want to use the Picture class, we first have to create all the objects.

Dagger uses the standard JSR-330 annotations in many places, one beingInject.

Next, we’ll implement the code to perform the injection. More specifically, we’ll create:

  • a module, which is a class that provides or builds the objects’ dependencies, and
  • a component, which is an interface used to generate the injector


So how do we call the instance (object) of the classes we want to use? For this we will use Inject annotation.

We use Inject annotation to invoke instances of classes that are automatically generated by Dagger 2, which we specify with the annotation provide in our module.

public class Brush {
public Brush(){

public class CanvasPaper {
public CanvasPaper(){

public class Paint {
public Paint(){


This tells the Dagger container that this class is a module. The Injects parameter specifies the class (s) whose dependencies are to be injected with this module.

To create a module,we need to annotate the class with the @Moduleannotation. This annotation indicates that the class can make dependencies available to the container:

public class PictureModule {
public Brush provideBrush() {
return new Brush();

public CanvasPaper provideCanvas() {
return new CanvasPaper();

public Paint providePaint() {
return new Paint();



The classes we want to use are specified in this annotation. Using this annotation, Dagger 2 produces the class for us when we need it. We need to add the @Provides annotation on methods that construct our dependencies.


@Singleton (and any other scope annotation) makes your class a single instance in your dependencies graph (it means that this instance will be “singleton” as long as Component object exists).

In short — everytime you’re injecting @Singleton annotated class (with @Inject annotation) it will be the same instance as long as you inject it from the same Component.


In the class that has the @Component annotation, we define which classes take dependencies.
This is the class that will generate Picture instances, injecting dependencies provided by the PictureModule.
Simply put, we need a method signature that returns a Picture and we need to mark the class with the @Component annotation:

@Component(modules = PictureModule.class)
public interface PictureComponent {
Picture buildPicture();

Notice how we passed our module class as an argument to the @Component annotation. If we didn’t do that, Dagger wouldn’t know how to build the picture’s dependencies.
These are the basic components of Dagger.