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;

@Inject
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

Inject

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 {
@Inject
public Brush(){
}
}

public class CanvasPaper {
@Inject
public CanvasPaper(){
}
}

public class Paint {
@Inject
public Paint(){
}
}

Module

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:

@Module
public class PictureModule {
@Provides
public Brush provideBrush() {
return new Brush();
}

@Provides
public CanvasPaper provideCanvas() {
return new CanvasPaper();
}

@Provides
public Paint providePaint() {
return new Paint();
}

}

Provides

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

@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.

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:

@Singleton
@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.