I have a named parameter in Java that I inject in the following way:
@Inject @Named("io")
Scheduler ioScheduler;
How do I do this in Kotlin?
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.
inject annotations. The reason for creating this library was to allow Kotlin common code to be dependency injected using dagger on the JVM. Dagger is a popular dependency injection library, used on Android and the JVM, which processes the javax. inject annotations to create a dependency graph during compile time.
Dagger is implemented using Java's annotations model. It generates code at compile-time using an annotation processor. Annotation processors are supported in Kotlin with the kapt compiler plugin. They are enabled by adding id 'kotlin-kapt' to the top of the file below the id 'kotlin-android-extensions' line.
The following is the form for Dagger2 & Kotlin for named fields.
@field:[Inject Named("io")]
lateinit var ioScheduler: Scheduler
see also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With