Migrate Dagger-Android to Dagger
By: Maryam Dashtirahmatabadi
Tags: Dagger 2, Android, Dependency Injection

First of all, you need to change the Dagger dependencies. You can get rid of those android ones like the below snippet:

Now let’s change your base application class which is called App inside my project. In the Dagger-Android we used the DaggerApplication() but for Dagger, there is no use for that anymore and instead of that extend your base application class from the Application() and for now, just initialize your AppComponent inside that. So change your base application class as below:

Now let’s change the AppComponent class. For now, remove the AndroidInjectionModule and the ActivityBuildersModule , also there is no need to extend this interface from the AndroidInjector .

With Dagger-Android I had general classes for ViewModelKey and ViewModelFactory but inside the Dagger version because we are going to have the subcomponents we need to separate them for each component e.i. for the main component these classes will be named as MainViewModelKey and MainViewModelFactory . I also scope the factory class with its custom scope which is the MainScope . In this step, you can delete any FragmentsModule (or FragmentsBuilderModule ) as these classes will not be used in Dagger anymore. Below you can see that I had the MainFragmentsModule which was responsible for binding the fragments inside the MainActivity :

Delete these classes!

Now let’s build the subcomponents of the AppComponent as the below snippet:

Notice that the MainViewModelModule class is now responsible for binding the MainViewModelFactory and all fragments’ ViewModel inside that activity. As I have two fragments for my MainActivity this module for the current subcomponent looks like the below snippet:

Here we can delete all the custom modules for the fragments such as my HomeModule which was responsible for providing its ViewModel or binding its repository and other dependencies. Instead of that class, inside the MainModule we are going to provide them:

Now you need to associate the subcomponents to the AppComponent with the help of a module class as the below:

And then just inside the AppComponent module’s list, add the SubComponentsModule . As we want to create those subcomponents and have access to them later inside other classes, we are going to add them inside the AppComponent too. So the final look of our AppComponent will be like the below snippet:

The final look of the AppComponent class!

As I have an abstract activity which is the superclass for my activities, I’m going to create a function inside that with the responsibility of adding all of this super activity’s children to the dagger graph. So my AbstractActivity will look like as below:

Then the inject() function will be overwritten inside all activities. For that purpose, we need a reference of that subcomponent which here is the MainComponent and we’re gonna provide it inside our base application class. So my App class will look like as below:

The final look of the App class!

Now we are able to inject the MainActivity to the dagger graph with the help of the previous changes:

I’m gonna use the field injection for the fragment dependencies. So I need to add inject() function for each fragment inside its related subcomponent and then call that inject() method inside the onAttach() of fragments. So my MainComponent will be changed as: