How to inject primitive variables in Kotlin?

The accepted answer didn't work with me, but the following worked well:

@set:[Inject Named("isDemo")]
var isDemo: Boolean = false

Source


First, you don't need lateinit, you can leave it as a var, and initialize with an arbitrary value. Second, you must expose a field in order to allow Dagger to inject there. So, here's the solution:

@JvmField // expose a field
@field:[Inject Named("isDemo")] // leave your annotatios unchanged
var isDemo: Boolean = false // set a default value