Setup Dagger 2.11 on Kotlin Project

This article is focused on implementing dagger 2.11 using Kotlin. Dagger 2.11 has android support module, compiler and few new annotations specific to android. Here, I am discussing very basic implementation using Kotlin.

Before we start, we should know that Kotlin has its own Annotation Processing Tool (kapt).

In order to enable it you need to add this in the build.gradle file:

kapt {
generateStubs = true

Or alternatively the new way of doing this is as below

apply plugin: 'kotlin-kapt'

This will enable the compiler to generate stub classes required for interoperability between Java and Kotlin and will create the necessary self-generated classes for Dagger.

Add Dependancies :

To start implementing dagger 2 in your Kotlin application add following dependancies in build.gradle file.

def daggerVersion = '2.11'
kapt "$daggerVersion"
kapt "$daggerVersion"
implementation "$daggerVersion"
implementation "$daggerVersion"
implementation "$daggerVersion"

Setup module :

Now first of all create appModule, which will provide dependancies all over your application level.For now just provide application context for basic example.

class AppModule()
fun provideApplication(app : App):Context = app

Now, create Builder class using new annotation @ContributesAndroidInjector

abstract class ActivityBuilder {

abstract fun bindMainActivity (): MainActivity

Setup Component :

Now, create AppComponent class which will enable selected modules for performing dependancy injection.

@Component(modules=arrayOf(AppModule::class,AndroidInjectionModule::class, ActivityBuilder::class))
interface AppComponent {

interface Builder
fun application(application:App): Builder

fun build(): AppComponent

fun inject (app:App)

Generate graph object:

Now you have setup your Module and Component, you could generate your graph object in your application class.

Note: Build your project to generate DaggerAppComponent.

Now implement HasActivityInjector interface and override activityInjector() method, as shown below

public class App : Application(),HasActivityInjector {
lateinit var activityInjector : DispatchingAndroidInjector<Activity>

override fun onCreate() {


override fun activityInjector(): AndroidInjector<Activity> = activityInjector
Note: Your class can extend DaggerActivity instead of HasActivityInjector interface. However, inheritance should be avoided so that the option to inherit from something else later on is open.

In the activity class for injecting useAndroidInjection.inject(this)

class MainActivity : AppCompatActivity() {

lateinit var app :Context

override fun onCreate(savedInstanceState: Bundle?) {

To have a variable injected to your Kotlin class object, You’ll need lateinit as this help you to ensure that you could have a non-nullable variable defined without need to assign something to it before hand.

Creating custom scope

If you want to create a custom scope you can do it as following:

annotation class PerActivity

And that’s all! we have everything set to use Dagger 2.11 using kotlin to provide our dependencies.

References :