Exploring Android Dagger

ninad thakare
3 min readSep 9, 2018

This post is a little too late for Dagger. I have been using android Dagger for a while now but I couldn’t find anything on creating object on scope that is bigger than activity but smaller than application.

The prerequisites before reading this post. Before I start with the actual post let’s go through somethings very quickly for a new reader.

For people who are used to seeing this:

public class FrombulationActivity extends Activity {
@Inject Frombulator frombulator;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// DO THIS FIRST. Otherwise frombulator might be null!
((SomeApplicationBaseType) getContext().getApplicationContext())
.getApplicationComponent()
.newActivityComponentBuilder()
.activity(this)
.build()
.inject(this);
// ... now you can write the exciting code
}
}

I understand your pain, been there done that. Writing the same thing in every activity. Some ways around this are

  1. Writing this snippet in BaseActivity and passing application component through an abstract method and have an abstract method pulling activity instance up to pass it on as activity context or passing views(MVP).
  2. Writing this block of code in App class and give every class static access to the App class.
Yeah, NO!!

Welcome, Android Dagger.

As I said this is not a very new thing. Just quick brushing up of concepts.

Now the solution to this is to add the dagger android dependencies to your existing dagger java dependencies. They might look something like this.

def dagger_version = '2.16'
implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"

If you are using the older dagger version, we will still have to write a lot of code. So just be careful.

Your AppComponent.kt will something like this.

Your App.kt will look like

We can reduce AppComponent.kt and App.kt to

But usually the above configuration is for when you’re not using application context to build singletons, which is hardly ever. So the first configuration is more desirable.

You have to write the activity you want to inject into as follows

Your activity has to only extend DaggerActivity

And that’s it, you’re done. We came all the way down from writing modules and components and subcomponents to just this. You still have to write modules, but that’s about it.

Now, coming to the scopes.

The @Singleton annotation for application scope, @ActivityScope (custom created) to bind activity presenter and other objects to an activity level and @FragmentScope (custom created) to bind objects at fragment level.

But what if we need a scope bigger than Activity and smaller than Application, say @ChatScope for chat listing and detail chat activities. Since creating something like a ChatRepository is going to be an expensive operation. We need a singleton limited to the two or more activities but not the rest.

I found a post by Remco Mokveld which helped me with that. Before that, I didn’t think it was possible with DaggerAndroid.

Activity Binding module should be modified as shown above where we share, ChatModule between the two activities modules.

Let’s look at one of the modules.

Now let’s look at ChatModule

And voila. It is done! The code is shared in a sample project here.

--

--