How to inject primitive variables in Kotlin?

前端 未结 2 1378
自闭症患者
自闭症患者 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:42

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

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

    Source

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题