MVP + Dagger = magic

MVP + Dagger 2 + RX =👌 Clean/Modern Android App/Code

Yassine Gharsallah
Published in
6 min readApr 4, 2017

In this Tutoriel I will show you how to use Dagger 2 to inject MVP layers (Model-View-Presenter) into each Activity/Screen of your App . So your code will be clean, maintainble, and easy to test : wich is the three thing each developer have to keep in mind when developing mobile apps.

So to do this i have created a simple App : MarvelHeroesApp . The code of this App is available in my Github Account : App Code .

The final visual of the app is like this :

Marvel Hero App

This Article is divided in three parts :

  • Part 1 : i will be talking about the most useful library that i have used in this app and each Android developer should use because they facilitate devs.
  • Part 2 : i will talk about MVP pattern and how to use it in android apps dev context .
  • Part 3 : i will talk about how to use dagger2 to inject MVP layers

App Libraries

In this Project i have used many api which i will not go into details for each library but i will describe each one briefly, So let’s take a look at the Gradle app file :

Gradle app file

In this project i have used a bunch of must have/use api like :

Leak Canary : a Library from Square that analyse the memory of your App and notifiy you whenever a leak happens, it’s easy to set up and it’s really helpful.

GLide : Image Loading and caching library very useful for loading images from the net especially for listview whith images from internet.

Timber : Another library from Square which allow you customise your log for Debug and release version .

ButterKnife :Dependencies Injection library for views into Android Component like Activity/Fragment…

Retrofit : Retrofit is one of the most popular HTTP Client Library for Android as a result of its simplicity and its great performance compare to the others.

RxJava/RxAndroid : Reactive extension for Java . I highly recommend to use it in each android app because we are reacting to user interactions while a background network thread is running in background and orchestrating all of this quickly become unmanageable code mess. In the Other side this is a case where RxJava (ReactiveX) excels — orchestrating multiple actions that happen due to certain events in the system.

Retrolambda : Please visit my article below for more details on how to set up and use retrolambda :

Implementing MVP core

Now let’s begin the real stuff :

The project is structured into 5 packages :


api : package where the retrofit api reside in .

Observable<Heroes> getHeroes();

I highly recommend when you have an app that consumes a webservice to use retrofit rxjava adapter so you can easily manipulate ws call with the handy Rxjava operator like OnNext , flatMap …..

application : package where we put all the generic modules of our app to be used by Activities . So you have to think of the application as it‘s a container of our Activities and it still alive all along the app is running . Also you have to differentiate between Application Context and the Activity Context bacause this is a really important point.

In our Exemple we have to expose Schedulers (Threads where our tasks will be executed and attached to ) and the Retrofit api service as shown in code below :

models : package where our POJO class reside

Screens : each screen of the app is in separarte package as a child of this parent package.

This is the most important parts of our app i will go into details in this part to show you how to decouple each layer of the Pattern MVP and how the code will look like at the end, for this i will take as an exemple the splash Screen :

Splash screen package

1- First of all create SplashView class inflate the view from the layout and bind it with ButterKnife :

2- Create SplachModel class which provide a bunch of methods to access DB or network with a return type Observable

3 Create SplashPresenter class which take as a parameters all the observable from SplashModel and turn them into Subscriptions

this is how you have to concept your app, image below describes better the flow of data in our app and you should do this in each app to obtain a maintanble and a testable app :

Clean Android Architecture

At the end this is how your MVP core should look like :


Dagger 2 Take place here

Dagger 2 come to our rescue right here to inject MVP layers into our Activity .

Dagger is based especially on three annotations which are :

@Inject : to be used for Object we want to provide .

@Component : used like an interface between @Inject and @Module

@Module : wich uses @Provides for each method we want to provide in a specific

SplashActivity need SplashView and SplashPresenter so we Inject them using the @Inject dagger annotation like the code below :

We can see right here that the code of the activity is very sharp and clean and there is no boilerplate.

Presenter and View are provided by the dagger Component defined as below :

This Component use SplashModule class to provide our Presenter and View :

That’s it guys for this Article, In the next Article i will write to you how to create fonctionnal Test and also how to unit test your code . because if you create a clean and a well decoupled Code Testing will become a piece of cake.

Certainly creating a modern/clean app need more time than creating an app the classic way but at the end we are sure that we have a maintainble code . That’s why Generally I have to explain to my clients that we have to spend more time on devs to get a modern/clean code .

Questions? Comments? Leave a note here or email me at :

Twitter : Ygharsallah

Also, click the 💚 below so more people will see this article here on Medium. And stay tuned next article will be online soon….



Yassine Gharsallah

Senior Android Developer | Tech Enthusiast . Wantrepreneur, But I will be an Entrepreneur in the near Future, I Promise !