Dependency Injection on Android

Yo, What’s Up dude ? Hope everything will be fine. Anyway, in my Previous Story I’ve already told you about the Binding View on Android. Today, I’ll continue My Story to share something useful too, regarding the same problem… to minimize the “boilerplate” of your code in Android :)

From Wikipedia, Dependency Injection is…

A design principle in which code creating a new object supplies the other objects that the new object depends on for operation

And… at this time I’ll show you how to implement the Dependency Injection using Google Dagger 2

First of all, let’s add the dependency (especially for the annotationProcessor) in your root build.gradle file :)

After that, apply that plugin in your build.gradle file inside the application module

And then, don’t forget to add the Dagger 2 dependencies on the same file :)

Here we go! Dagger 2 has been completely setup as our library. So, let’s start to implement it in the real case!

Before we go further… I’m trying to explain a concept of this application that separate some module based on its component (activity/fragment or other utilities module)

The dagger graph object component are consist into 3 components. First component called ApplicationComponent that hold 2 components below (ActivityComponent and FragmentComponent). Each component hold at least 1 module, except ApplicationComponent that hold 2 module (the one with UtilityModule)

I’m also create some scope to make other component/object graph decoupled each other. In this example I’m creating the ActivityScope to hold all scope regarding the ActivityContext and FragmentScope to hold all scope regarding the ActivityContext inside the Fragment. But, each of them are follow by the ApplicationContext

Dagger Object Graph





I hope you’re not confuse with my explanation :)) … Ok. let’s start with the code. First of all I’m create an ApplicationModule and ApplicationComponent interface as their parent components

ApplicationComponent interface

Implement the ApplicationComponent inside the Android Application class and make it as static object :)

Create a layout for MainActivity class

After that, Create an ActivityModule in order to register it with ActivityComponent

Don’t forget to create the ActivityComponent :)

Implement the MainActivity class with the injector to ActivityComponent object graph

Then, Register all activities stuff into ActivityComponent

For the fragment, I’ll show the same step that activity does … Create a layout for MainFragment class

After that, Create a FragmentModule in order to register it with FragmentComponent

Don’t forget to create the FragmentComponent :)

Implement the MainFragment class with the injector to FragmentComponent object graph

Then, Register all fragments stuff into FragmentComponent

If you want to create another module, for example the UtilityModule for all object graph. You can do like the following example… Create some class to hold the Utilities (for example, Commons)

After that, just create an UtilityModule that hold the utilities class

Then, finally at that module into ApplicationComponent or you parent components

That’s it! and don’t forget to register your activity and your application class in your AndroidManifest

Run your code and see what’s going on :) … for the source code, I’ll share you on My Github Account

The only way to do great work is to love what you do