Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you inject named field parameters in Kotlin with Dagger 2?

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?

like image 609
loeschg Avatar asked Jun 06 '17 21:06

loeschg


People also ask

What is field injection dagger 2?

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.

What is @inject in Kotlin?

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.

Does dagger work with Kotlin?

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.


1 Answers

The following is the form for Dagger2 & Kotlin for named fields.

 @field:[Inject Named("io")]
 lateinit var ioScheduler: Scheduler

see also:

  • Late initialized properties
  • Kotlin annotation syntax
  • Annotation use-site targets
like image 112
loeschg Avatar answered Nov 15 '22 12:11

loeschg