16 Nov 2015

Dependency injection on Android with Dagger 2


At Google I/O 15 a new version of Android Support Testing Library was announced. It uses Dagger 2, the new dependency injection library created by Google.

This article will explain how to use it in your projects.

About Dagger 2

Dagger 2 is a fork of Dagger, the dependency injection library created by Square, by Google. The main difference between Dagger 2 compared to Dagger and the other dependency injection libraries is that it works with generated code rather than reflection. Its guiding principle is that the generated code could have been written by a developer, which makes the dependency injection simpler and more efficient. For more information on Dagger 2 design I invite you to watch this presentation by Gregory Kick.

Inside Dagger 2


Modules are in charge of the creation and the configuration of the dependency to inject thanks to a set of functions annotated by @Provides. Modules are classes annotated by @Module.

Module example:

What do we do in this code?

@Module tells that the class is a Dagger 2 module

@Provides tells that the method is a provider for the injection. The name of the method doesn’t matter but the convention wants that it start with “provide”

@Singleton tells that we want to use the same reference every time. In this particular case we didn’t need this annotation because we don’t create a new instance. However it allows the developer to understand what the provider does.


Components are used to group and build modules in an object used to obtain dependencies and/or inject fields. It’s an interface annotated by@Component which implementation will be generated by Dagger 2 during the build. A component must list its composing and depending modules.

The interface can have two kinds of method:

  • return an object and has no parameters: we return an instance created by Dagger 2 and its dependency are resolved by injection in the constructor
  • return void but has a parameter: allow to resolve the dependency injection in a non-private field. It’s useful in Android because we don’t create some instance (for example Activity)

What do we do in this code?

@Singleton tells that we want one reference to the component in the application.

If we don’t expose Context and Bus we won’t be able to use them outside our component and we’ll have a build error.


There are 3 ways to inject dependencies:

  • Directly calling the dependency on the component
  • By annotating a property by @Inject and by calling component.inject(object)
  • By annotating the constructor by @Inject


It is possible to create its own scope for its components. We can create a scope @PerActivity which allow to bind a component to an Activity.

Gather everything


In your root build.gradle file add the following dependency:

In your build.gradle app add the following plugin:

and the following dependencies

Build the graph

Now that we have a module and a component (cf Inside Dagger 2) you can build the dependency graph of your application.


In order to inject your elements in your Activity or your Fragment you must use the property injection.