Implementing a sample Android Application using MVP , RxJava , Dagger 2 , Retrofit2 , Test , and all the brand new modern methods and libraries Previously on this article… _Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com Yet another MVP article — Part 1: Let’s get to know the project Let’s think that you have studied which means you know everything about ’s modules and . part1 the project structure now let’s continue… Would you explain how Dagger wire up modules and MVP layers in the core? Dagger diagram uses , and to know what and how to dependencies. You can find more articles which explain more about what’s going on inside Dagger. Dagger2 Modules Components, SubComponents inject From Dagger diagram bottom to top I can mention and which provide and : CacheModule SearchModule View Presenter “Trying new shoes doesn’t require you to saw your feet off”!, — Novoda First of all, take care of return types of provide methods, they all return interfaces, not the implementation of that interface! This is how we can replace the implementation for further tests, and also I have to mention that this is a mixture of , and principles of . Open/closed Liskov substitution Dependency inversion SOLID Secondly, do not forget that we are still inside module and we have no idea about how it is gonna be used by . core View Finally, these two modules are being used by and that are ’s SubComponents, which I will explain in module. CacheSubComponent SearchSubComponent ApplicationComponent app We had two more modules inside that help us with providing retrofit dependencies, and Being familiar with will help you with understanding what’s inside, however, I will explain everything about it in next parts of this article. core ApiModule ClientModule . retrofit2 Both of these modules and DatabaseModule are being used by ApplicationComponent which I will explain in module. app ...and what’s going on with Dagger inside module? app The module most important Dagger part is which uses and to provide all the Application injections. app ApplicationComponent AndroidModule ApplicationModule What’s inside ApplicationComponent : 1. AndroidModule : This module provides Application and Application which always comes handy for injection while developing for android to access for instance to or different . Some developers implement this module inside ApplicationModule but I prefer to take it separate for more clearance. Context Resources Services Resources This module would be created in class which extends class: MarvelApplication Application as you might have been mentioned this class is abstract which complete in or buildTypes by implementing method which comes handy for doing things for release version or debug version. debug release initApplication() In this sample I have used debug to plant library for avoiding Timber logs in release version: MarvelApplicationImpl Timber 2. ApplicationModule : Almost all application requirements are being provided in this module, : use BuildConfig.DEBUG boolean variable to check if the running instance of the application is in debug mode and use it for logging network API calls in logcat. isDebug : provide network parameters which being used when creating the OkHttp client for Retrofit. networkTimeoutInSeconds, cacheSize, cacheMaxAge, cacheMaxStale, cacheDir : provide the API’s endpoint for retrofit. endpoint : provide RxAndroid schedulers which I will explain in RxJava part. appScheduler : provide network state for handling offline situations. isConnect 3. , from core ApiModule ClientModule 4. …and DatabaseModule 5. SubComponents: The nice thing about is , you can add SubComponents to your main and use all providers within and add yours. Dagger SubComponent ApplicationComponent In this sample, and add up to and make it more brilliant. SearchSubComponent CacheSubComponent ApplicationComponent let’s take a look… SearchSubComponent and CacheSubComponent: let’s take another look to ApplicationComponent: as you can see we had the methods which helps with injection, but what are those methods?! inject plus First of all, I have to mention that inject and plus is the name!! you can rename it to whatever you like and Dagger take care of input and output of the method to know what’s their use! but please don’t monkey around with the name cause your team member must read this code later… using methods you can ask Dagger to add a to the and do the method inside that plus SubComponent ApplicationComponent inject SubComponent. methods take a and return a using that plus Module SubComponent module. let’s take a closer look: Well well well… where does Dagger integration begin? By a little change in we can ask Android to use MarvelApplication implementations as the start point of our app: manifest and in class we create application component once and use it in and by extending and classes. Both of them are abstract and the method must be implemented inside the Activity or Fragment. MarvelApplication Activities Fragments BaseActivity BaseFragment injectDependencies How to drown yourself inside Dagger?! You can do more Dagger if you like! Take a look at this which explains how to Daggerify everything… I have done it inside this sample project by extending and from module using and module. article SearchModule CacheModule core AppSearchModule AppCacheModule in app This is how the magic happens and that’s it for now… Please clone the and get more familiar with it because in next part I will explain more about retrofit and how it helps with network API calls and caching using it. project repo from GitHub I look forward to your comments and helping for more improvements with this article. Share this article if you think it is useful, and follow me for more articles . Mohsen Mirhoseini To be continued… _Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com Yet another MVP article — Part 3: Calling APIs using Retrofit