New Dagger2 Integration with Android Architecture component

In my last tutorial, we had functioned with Dagger2 in android application development. Dagger 2 is an awesome dependency injection library which provides the alternative way of Object instantiation. I already explained what is Dagger2 and why we are using this in android application development and what are the benefits to using this? Here is the detail of Dagger2 dependency injection with Android.

In this tutorial, we will learn to integrate the new Dagger2 with android architecture component. Implementation of Dagger2 with android architecture is slite changed. It provides much more fanciable and easy to provide the instance. Very first thing we need to add the relevant dependencies in your android build.gradle file.

Android architecture design is based on the view model. So Every module will inject with view model by Dagger2. Now in the next step, we need to get the activity injector in the main application (An Application that injects its members and can be used to inject Activities, Fragments, Services, BroadcastReceivers and ContentProviders attached to it.).

We have already checked with the old way instantiation of an object, here is an example of a sample.

Ok, let’s implement new Dagger2 injection. We need to initialize the Dagger2 component in Main Application by implementing new HasActivityInjector (Provides an AndroidInjector of Activitys.)

Dagger2 introduced some of the new component builders. Let’s see in detail.

@ContributesAndroidInjector (module) and @ContributesAndroidInjector

If an activity builder module containing any Fragment or view then we can provide the specific Fragment module to contribute for the android injector and if not then simply annotated with @ContributesAndroidInjector. To mapping all activity and pass it to Dagger2 we will create the ActivityBuilderModule.

Dagger know our activities in compile time. In our app, we have Main and Detail activity. So we map both activities here.

@Componentand @Component.Builder

A component will provide injected instances by using modules. Component.Builder used for instantiation of a component. We can create the interface of the builder and annotate with @Component.Builder and build by @BindInstance.

AppModule: App Module provides retrofit, okhttp, persistence DB, shared pref etc here. So our dagger graph will understand that.

ViewModelModule: VewModelModule has all the module information about the each ViewModel and injects those when we required.

We can inject MovieListViewModel by annotated @Inject.

Now in the last activity and Fragment should know that how Dagger injected. So we need to tell by AndroidInjection.inject() method in onCreate() of Activity or onAttach() of Fragment.

Wrapping up: Ok, You have done so for.The conclusion is that It does not matter how many class files need to create the most important that our code will be in well structured and we do not worry about any refactor or changes in code in future.  I used the article reference here.

Here are the best offer and recommendation for you to download this. Here is the Github link for an Android Architecture Sample project.

Please do subscribe email to get all newsletters of this blog and if you feel that this post will help you to better understand then do not forget to subscribe, share and comment below. Ok, then I will see you in my next tutorial till then enjoy your life and happy coding 🙂


I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at

2 thoughts on “New Dagger2 Integration with Android Architecture component

Leave a Reply

Your email address will not be published. Required fields are marked *