Introduction to Dagger 2

In my previous post I gave an introduction to inversion of control, if you aren’t sure of this concept yet I recommend reading that post before continuing (you can find it here I have been trying to learn more on this concept and came across this neat tool called Dagger 2, which provides a framework for dependancy injection and is being widely used by the android community.

When I first started with Dagger 2 I had some trouble wrapping my head around how dependancies get exposed to dependant classes, especially since most of Dagger 2’s functionality is based on annotations and auto-generated classes which are hidden from the developer’s point of view. In this post I am going to explain the basics of Dagger 2's design so that anyone getting started or just wanting to try it out can get an understanding of how to setup Dagger 2 components for dependancy injection.

Dagger 2 is designed around two core elements: Component and Module.

Modules are classes which provide your dependancies and are tagged with the @Module annotation. The @Provides tag is used to annotate methods which expose the dependancy instance(s).

public class MyModule {
public APIService APIService(){
return new APIService();


Components are interfaces which take in two objects: the dependant object, let’s say object D, and a Module instance, M. The Component retreives the dependancies M provides and injects them into object D. Component interfaces are annotated with the @Component tag. The @Component tag is passed a “module” parameter whose value indicates the Module class from which to fetch dependancies. In the Component interface you will also need to define an inject method for the dependant class. This method will have the following signature: void inject(D d); The Module class is provided via the annotation and the dependant class is passed via the inject method.

@Component(modules = {MyModule.class})
public interface MyComponent {

void inject(D d);


In your dependancy class, D, you will need to annotate all your dependancies with the @Inject tag.

public class D {
@Inject APIService apiService;

Once you define these classes, you will need to rebuild your project. Dagger 2’s annotation processor will run through your tags and generate Dagger* prefixed classes for each of your Components. These auto-generated classes expose themselves using the Builder pattern.

D d = new D();

All you need to do now is to call the inject method passing in your dependant class, D. DaggerMyComponent checks for all the @Inject members in D and initializes them with the dependancy providers in its Module. With almost no initialization code in the client class, D, we were able to inject its dependancies. By simply switching out modules or the @Provides methods we can replace core dependancies with minimum side effects to client classes.

This is a very basic introduction to Dagger 2. There are a lot more use cases and complexity Dagger 2 is capable of handling. I hope this post was useful in giving you an understanding of Dagger 2 and helping you get started. If there’s anything I’ve missed please let us know in the comments below.

Show your support

Clapping shows how much you appreciated Akqeel’s story.