Dagger 2, RxJava and RetroFit 2 — Efficient Networking with Injection

Ashwini Kumar
AndroidPub
Published in
3 min readOct 20, 2016
Inject everything!

It was a colossal task to have Dagger 2 on an already stable codebase, and then integrating Retrofit 2 replacing OkHttp, while still maintaining MVP. It is aptly said that

Sometimes you have no choice but to burn the goddamn house down and bathe in the blood of your binary enemies. Your sanity and the future of your project depends on it.

It all started with an experiment with Arun Sasidharan. The idea was to have Dagger 2 integrated and then implementing Retrofit 2.

This idea could possibly destroy the complete stable codebase while creating or redefining the much stable, optimised and scalable codebase. Everyone aims to learn Dagger 2 and integrate it in the codebase but the learning curve for this library is quite acute. You have to be patient and look out for every minute detail like DAG-er (which means Directed Acyclic Graph).

I went through a series of articles which kept on creating scattered dots. Those dots started connecting to each other when I went through the articles by Mirek Stanek. I highly recommend everyone to go through his articles on Dagger 2 and I promise life will become elegant after that.

Here I will talk about key concepts which motivated and helped me to integrate Dagger 2 on MovieGuide(forked from Arun Sasidharan MovieGuide). To have the dependencies satisfied, you create the dependency graph for the complete application. This graph will help you in finding those instances which are interdependent on each other. One simple rule to follow:

Module provides injections and Component provides dependency graph.

As an instance, take the case of NetworkModule from MovieGuide. The aim is to provide the singleton instance of OkHttp and Retrofit through out the application. Lets start by creating the dependency graph for NetworkModule:

All the dependencies needed are provided by NetworkModule except Context which is provided by AppModule. Now since these dependencies are required at the application level, singleton scope has been provided. AppComponent creates the graph and ensures that every dependency needed at application level must be satisfied.

As mentioned earlier, Component provides dependency graph. So for every Fragment or Activity, create the component and module. For instance, ListingComponent will provide dependency graph for the ListingFragment, once the app level dependencies have been provided. This ListingComponent has been given ActivityScope which further ensures that the life of those dependencies is limited to ListingActivity only. The use of SubComponent on ListingModule will have the entire objects graph from its parent while Component dependency gives access only to those which are exposed in Component interface.

Moreover, the dependencies which were not provided properly by Module will lead to compiler errors while building the application. These errors are self understandable as to where you have missed the dependency. According to me, Dependency Injection states just one point:

No new operator in the codebase and every dependency should be provided from dependency graph.

Following that, I have also injected adapters using AutoFactory. I am still learning and exploring the internal areas of Dagger 2 and Retrofit 2. Hope this helps.

#Complete MovieGuide repository is hosted at GitHub

## References:

  1. https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2
  2. http://frogermcs.github.io/dagger-graph-creation-performance/
  3. http://frogermcs.github.io/inject-everything-viewholder-and-dagger-2-example/
  4. https://code.tutsplus.com/tutorials/dependency-injection-with-dagger-2-on-android--cms-23345

If you liked this post, please show your appreciation by clapping.

--

--

Ashwini Kumar
AndroidPub

🎯 Engineering towards excellence every single day.