As more and more apps undergo Kotlinification the frameworks we lean on as app developers have largely remained the same. While the advice in Keeping the Dagger Sharp is still applicable to Kotlin users, the syntax has changed when writing @Module and @Component classes. Lets take a look at some common patterns to be aware of when working with Dagger 2 in Kotlin.

1. Static @Provides

If you want a static provides function you need to use a named object .

2. @Binds

Requires an abstract class; when a module also requires static provides they should placed in a companion object .

3. Injecting Qualified Types

Injecting a qualified type requires the custom @Qualifier to target fields, functions and value parameters. If you forget to add these as annotation targets you may accidentally get the unqualified type. Watch out for this one!

4. Parameterized Type Injection

Requires you to annotate injection sites with @JvmSuppressWildcards otherwise compilation will fail.

5. Inferred Return Types

It’s important to think about return types when writing @Provides functions. If a return type isn’t provided the inferred type will be that of the object returned.

6. Array Literals

Many Dagger annotations take arrays as parameters. With Kotlin 1.2 array literals can be used in annotations.

That’s all for now! Big thanks to the folks that contributed to this issue, Mitchell Tilbrook and Mike Nakhimovich. Drop a comment and let me know what you’ve run into while using Dagger 2 with Kotlin.