Dagger 2 complete tutorial for beginner

Tuan Kiet
3 min readAug 8, 2021

--

Learning path

There are many Dagger tutorials out there, but you always feel like missing something after reading them. You go back to read the official document, read even more tutorials but end up getting more frustrated. In this series, I will walk you through the very basics of Dagger, this also will be the last Dagger tutorial you ever need.

What you’ll archive

  • Understand Dagger 2 from the ground up
  • Progressive learning path (from the very basic to advance)
  • Be able to work on any projects that use Dagger 2

Path 1: Component

P1

First, Dagger 2 is an annotation processing library, when you build the project, it will go through your code and inspect your annotation then generate the code. In other words, you instruct Dagger on how to generate code with annotations. In P1 DaggerAppComponent is a class generated by Dagger, this class implements AppComponent.

A component is an entry point to our dependency. In AppComponent we declare that we want to get an instance of Computer. Computer class depends on Keyboard and Speaker, they form a dependency graph, if you want Dagger to generate the code for you, you need to tell Dagger your application dependency graph, then Dagger will be able to supply anything within that graph. Right now the graph is empty and if you try to build the project, Dagger will complain that it cannot provide an instance of Computer because Computer hasn’t exited in the graph yet.

error: [Dagger/MissingBinding] Computer cannot be provided without an @Inject constructor or an @Provides-annotated method.

Build the dependency graph

P2

There is a lot of ways to put a class into the dependency graph. The easiest way is to use @Inject annotation, you put this annotation on the constructor of a class. If the class has an empty constructor (like Speaker), then Dagger will simply put it into the graph, but if the constructor requires other dependencies (like Computer require Keyboard and Speaker), all of them must be presented in the graph.

P3

In the example in P3, we try to put Computer into the graph, Computer require Speaker and Keyboard but only Speaker presented in the graph (via @Inject annotation and an empty constructor). Hence Computer will not be available in the graph and cannot be provided by Dagger. If you build the project, Dagger will tell you exactly so:

error: [Dagger/MissingBinding] dagger.basic.Computer cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract interface AppComponent {
^
dagger.basic.Computer is requested at
dagger.basic.AppComponent.getComputer()

To fix the problem annotate the Keyboard’s constructor with @Inject .

With the graph now completed, Dagger successfully generates the code.

Summary

  • Component is the entry point to our dependency
  • Component can provide anything within the dependency graph
  • Your primary role when working with Dagger is to describe the dependency graph.

Up to this point, you already have a basic understanding of how Dagger work. These are the very basics of Dagger, other annotations like @Provides @Binds (which I will cover in a future post) are just another tools to help you describe the dependency graph, they work in the same principle.

Source code for this part:

--

--