Kotlin + Dagger 2 Gotchas

Vlad Chernis
1 min readSep 30, 2017

--

A veteran Kotliner may consider “gotchas” to be an exaggeration, but for the majority of us just starting our Kotlin journeys, I hope the following is helpful.

1. Prefix field qualifiers.

Field qualifiers at an injection site such as @Named in the below example:

@Inject
@Named(“ioExecutor”)
lateinit var ioExecutor: Executor

must be prefixed like so:

@Inject
@field:Named(“ioExecutor”)
lateinit var ioExecutor: Executor

2. Annotate Module companion objects.

To translate the following example from Java to Kotlin:

@Module
abstract class FooModule {
@Provides
static Bar bar() {
// …
}
}

is to discover one of the rare cases when the Kotlin version is more verbose:

@Module
abstract class FooModule {
@Module
companion object {
@Provides
@JvmStatic
fun bar(): Boo {
// ...
}
}
}

--

--