How to inject primitive variables in Kotlin?

前端 未结 2 1391
自闭症患者
自闭症患者 2020-12-31 05:25

I am using Dagger2 for DI in my Android app, and using this code for injecting classes into my Activity is fine:

@field:[Inject ApplicationContext]
lateinit          


        
2条回答
  •  醉话见心
    2020-12-31 05:49

    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
    

提交回复
热议问题