Dagger Android Injection

Nepal Brothers
3 min readJul 24, 2019

This article is solely about injecting Android Activities in the Dagger. I found that there is a scare in the resources on how to setup using Dagger Android, while there are ample of resources that tell how to do DI with the Dagger. So, here it goes.

I want to see the code right away?

See the commits to see what changed throughout the development. This way, you can implement dagger gradually.

Subclass Application class to begin with

Make sure that your application starts from MyApp class, rather than Android’s default Application class by specifying in the Android Manifest.

<application
android:name=".MyApp"
....

Add Dagger Dependencies

Add the following Dagger dependencies in your Gradle file.

Start off by adding the process

We will also need kapt for annotationProcessor for Kotlin. For Java, we can just use annotationProcessor

apply plugin: 'kotlin-kapt'

Add Dagger Dependencies in dependencies section of Gradle

//dagger
implementation 'com.google.dagger:dagger:2.23.2'
kapt 'com.google.dagger:dagger-compiler:2.23.2'

//dagger android
implementation 'com.google.dagger:dagger-android:2.23.2'
implementation 'com.google.dagger:dagger-android-support:2.23.2' // if you use the support libraries
kapt 'com.google.dagger:dagger-android-processor:2.23.2'

The end product will look like this:

Dagger: ApplicationComponent

Lets now actually use Dagger. Lets start with defining AppComponent that injects the MyApp. We will use this component to bind our Application class.

import com.rockink.daggerpoc.MyApp
import dagger.BindsInstance
import dagger.Component
import dagger.android.AndroidInjectionModule

@Component(modules = [AndroidInjectionModule::class])
interface AppComponent {
fun inject(app: MyApp)

/**
* This Builder will be constructed by Dagger
*
@BindsInstance binds the current instance into the DI graph,
* It means that we can use this instance anywhere in our DI graph
*/
@Component.Builder
interface Builder {
@BindsInstance
fun application(app: MyApp): Builder
fun build(): AppComponent
}
}
  1. Have AppComponent include AndroidInjectionModule.
  2. Define a void returning function inject(app: MyApp) to inject the MyApp instance to the component.
  3. Build a Builder with @Component.Builder that uses
    (a) bindsInstance to bind an instance of the MyApp. This allows to use the MyApp instance in your defined Dagger Components
    (b) returns an AppComponent that you will be available to use once the AppComponent is build (more about this later)

If you build your project, you will see that Dagger has generate code such as DaggerAppComponent. We will reference this via our MyApp class.

That is it. Dagger now injects and manages the dependencies in your MyApp class. Lets take an example to inject RandomStringer in the MyApp and then show the Toast of the string that RandomStringer randomly selects.

Notice how AppModule is added and it provides RandomStringer

Then, in MyApp, we have:

Now, while running the application, we can see that Dagger works like a charm with the Application.

Inject the Activity now

Implement HasAndroidInjector in MyApp

Start by implementing MyApp with HasAndroidInjector and implement dispatchingAndroidInjector.

Notice the HasAndroidInjector. Since we already injected MyApp, we can use Injected dispatchingAndroidInjector

We use dispatchingAndroidInjector to inject instances of core Android types. In our case, it is MainActivity.

Define SubComponent and attach to Module

So, lets define a subComponent that will contain the bindings to bind MainActivity with AndroidInjector. This is just a SubComponent, and it will need to be attached to a module, namely MainActivityModule.

Update the AppComponent with the MainActivityModule

Add MainActivityModule in a list of modules in the @Component of the AppComponent like this:

Inject the Activity using AndroidInjection

All we need to do now is use the below code snipped to inject the activity to AndroidInjection. It will take care of binding the Activity to the modules we just defined.

AndroidInjection.inject(this)

Lets test it:

Referencing RandomStringer was possible because of the Injection.

Notice AndroidInjection.inject(this)
Notice the number has changed. Randomly Generated

In similar fashion, we can inject LiveModels, Repositories, HttpClients and whatnot.

--

--