My previous Dagger 2 for dummies made Dagger 2 concepts simple. But it’s a relatively long article at 10 minutes of read time.

In programming, we find better methods and refactor code to simplify it further — I found a simpler way to explain Dagger 2. For those who feel a 10 minute read is too long, read this 4 minutes quick start tutorial.

Just 3 lines of code

To get Dagger 2 working, we’ll just need 3 lines of code!

class Info @Inject constructor()

@Component interface MagicBox { val info: Info }

fun main() { println(DaggerMagicBox.create().info) }

Just add this code in any Kotlin file in your Android Studio, and click the ▶️ button next to the fun main() , and it will execute.

Note: You’ll need to include the library first though in the build.gradle file

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

And

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

// ... other dependencies ...

implementation 'com.google.dagger:dagger:2.13'

kapt 'com.google.dagger:dagger-compiler:2.13'

}

The Dagger 2 specific parts

From the above 3 lines of code, you will see 3 unique Dagger 2 specific terms, as explained below.

@Inject constructor()

This is the part where it indicates to Dagger 2 to auto construct this object.

@Component interface

This is the part where it indicates to Dagger 2 to implement the interface (in our case MagicBox ) that will have the code that generates and constructs all the injectable dependency. The implementation is named DaggerMagicBox .

To look into it more check out 3 lines of Working Dagger 2 Example Code.

DaggerMagicBox.create()

As mentioned in the sub-section above, the DaggerMagicBox is an auto-generated implementation of the @component interface defined. It has the create() function that generate instantiate this implementation. From it, we could extract the dependent object easily.

From above, we see that Dagger 2 automatically creates the Info object through the DaggerMagicBox container class.

How could I auto inject my dependency?

The code we have below looks seemingly not like a Dependency Injection pattern, but instead a Service Locator pattern, as the fun main() need to get the Info explicitly from the DaggerMagicBox . This is similar to setting it to a variable as below.

val myInfo = DaggerMagicBox.create().info

But what we want is auto-injection, as seen in the Dagger 2 code below:

@Inject lateinit var myInfo: Info

To achieve this, we just need to do some additional work beyond the 3 lines.

1. Create a target class to contain the dependency

class MainClass {

@Inject lateinit var info : Info

}

In the code above, we need the lateinit var because the variable is not known during the construction of the class object.

2. Replace component val with function to inject target object

Replace the below line:

// Remove this line and replace with the one with `poke`

// @Component interface MagicBox { val info: Info2 } @Component interface MagicBox { fun poke(myTarget: MainClass) }

This is to tell the component, instead of having a way to create Info within the MagicBox itself, it could inject the dependencies (i.e. Info ) into the target class (i.e. MainClass ) object when the poke function is called.

3. Inject the dependencies into the target class

Finally, just instantiate the DaggerMagicBox within the target class, and ask it to inject the needed dependencies inside (using the poke function), as shown below.

class MainClass {

@Inject lateinit var info : Info

init { DaggerMagicBox.create().poke(this) }

}

With the above, the entire code looks as below.

class MainClass {

@Inject lateinit var info : Info

init { DaggerMagicBox.create().poke(this) }

}

class Info @Inject constructor()

@Component interface MagicBox { fun poke(mainClass: MainClass) }

fun main() { println(MainClass().info) }