Android Dagger 2 + Retrofit + RecyclerView With Examples

This is the one stop complete tutorial for implementing Dagger 2 with Retrofit and RecyclerView in our Android Application.

Dagger 2 is a dependency injection library that’s vital for clean code and architecture. For a basic overview of Dependency Injection and Dagger 2 refer this tutorial before proceeding ahead.

What is Dependency Injection?

Dependency Injection simply put, instead of creating instances yourself using the new keyword, you’ll be provided those instances from outside.

Dependency Injection is our friend in the worst and best times. It helps in refactoring the code quickly and helps in testing individual things since nothing is tightly coupled. It enhances the readability and maintainability.
Regardless, of the knowledge of Dependency Injection, at some point in your Object Oriented Programming, you have definitely used it. Following example will definitely remind you of it.

Class A is a dependent. Class E and B are dependencies that are injected.
As you can see in the above code, instead of initializing classes in the constructors, we do them separately. Though we still have the boilerplate code above.

Important Points to note about Dagger 2

  • It checks and draws the dependency object graph at compile-time.
  • We need to use the annotations : @Module, @Provides, @Inject, @Component @Scope.
  • For two dependencies that conflict, such as Application has a different Context and an Activity has a different. Dagger 2 needs @Qualifier our @Named annotations to differentiate.
  • @Component is set on an interface. It acts as a bridge and is used to provide the dependencies specified in the @Module to the Java class. The dependency would be retrieved with @Inject in our Java class.
  • Dagger 2 cannot inject private fields.

Dagger 2 is best understood through an example. It’s been a while and we’ve tried to create an Android Application that explains the concepts in the best possible way using RecyclerView and Retrofit.
We’ll be using the StarWars API.
Let’s dive deep!

Project Structure


That’s big! We created a separate package di for dependency injection components, modules, scopes and qualifiers. The activities go into the ui package. The API method and POJO class are inside the retrofit and pojo packages respectively. The adapter holds the RecyclerViewAdapter class.
This project consists of two activities – MainActivity and DetailActivity.

Add the following library dependencies inside the app’s build.gradle.

Ensure that you’ve added the the Internet Permissions in your AndroidManifest file.

Dependency Injection package


Scopes define where all those Components would be used. In this application, their are two : ActivityScope and ApplicationScope.

@ActivityScope and @ApplicationScope would be used on the Components later on.


So, a Context can be either of the Activity or the Application. How would Dagger2 differentiate between them?
Using Qualifiers.

The and the classes are defined below.

Now in the Modules wherever we use Context, we must annotate it with @ApplicationContext or @ActivityContext depending on the use case.


Modules are what would provide the dependencies to the dependents via Components.
Let’s plan out our Dependency Graph first.


So the APIInterface isn’t dependent on anything.
The ApplicationComponent would hold the Retrofit and AppContext Modules.
The MainActivity would hold the Adapter and Activity Context Modules along with the ApplicationComponent’s dependencies.
The DetailActivityComponent doesn’t include any of its own modules. It just uses the ones present in the ApplicationComponent.


We’ve specified the Application Level scope and qualifier for the context.


@Provides denotes that a dependency would be provided from that method to it’s dependents.
For Retrofit we use an ApplicationScope.


The above module is used to provide the Activity Context and the Activity’s instance.


The code for the AdapterModule is given below.

It’s used to create the RecyclerViewAdapter from the POJO data.
Also, the ClickListener is an interface defined in the RecyclerViewAdapter class to trigger the click listener callback methods from the Activity itself.
It injects the MainActivity dependency since we’ve included the MainActivityContextModule in the definition.

Let’s look at the APIInterface and the POJO classes.

The code for the class is given below.

@Url is used to do a dynamic URL call in Retrofit. The URL would be specified at runtime.

The POJO classes are created from the .jsonschema2pojo.

We’re only parsing the values which we’ll be using in our application.

The star wars api looks like this:



In the Components, we’ll include the Modules.
We’ll need to expose the fields that’ll be used in our Activity/Application.

Dagger2 would autogenerate a class named Dagger%ComponentName%. Eg. DaggerApplicationComponent.
injectApplication is used to allow @Inject fields in our Activity/Application.

The above component would have access to the ApplicationComponent dependencies too.

Now is the time to use the di in our Activity’s and Application.
Let’s look at the layout codes first.





Before we use dependency injection in our classes, REBUILD THE PROJECT. This is done to generate the Dagger Component classes.

The code for the class is given below.

DaggerApplicationComponent.builder().contextModule(new ContextModule(this)).build(); is used to build the modules present in the component.
getApplicationComponent would be used to return the ApplicationComponent in our Activities.

Don’t forget to add the above application in our AndroidManifest.xml file.

UI package

The code for the class is given below.

Once this happens: mainActivityComponent.injectMainActivity(this);, The fields present with the @Inject would be automatically injected.
The rest is doing a Retrofit call and setting the data in the RecyclerViewAdapter.
The class implements the RecyclerViewAdapter.ClickListener interface callback which triggers a launchIntent method whenever the RecyclerView row gets clicked.
Note that the Context injected needs to be specified with the relevant qualifier we had earlier defined.

The code for the RecyclerViewAdapter is given below.

The code for the class is given below:

Again the inject method is used to inject all the dependency fields.
The retrofit makes a request to the dynamic URL specified and displays the response in a TextView.

The output of the above application in action is given below.
android dagger2 recyclerview retrofit output

This brings an end to this tutorial. You can download the full source code from the link below.

By admin

Leave a Reply

%d bloggers like this: