Dagger 2, Providing Application Context in Module

前端 未结 4 1327
北海茫月
北海茫月 2021-01-13 22:17

Im pretty new in Android development and newer in DI. I am using Kotlin on a personal project where I am experimenting with Dagger 2. I managed to set it up for a util class

4条回答
  •  醉酒成梦
    2021-01-13 22:52

    You can't use class PineApplication @Inject constructor(): Application() to create PineApplication. It's a framework class and has to be created by the Android Framework.

    Doing so Dagger will create PineApplication, but applicationContext will return null as it has never been initialized (by the system).

    Don't use constructor injection for framework classes and don't create the yourself. Use @Bindsintance to add the object to the component with its builder, or use a module to provide it.

提交回复
热议问题