Kotlin + Dagger2: cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method

亡梦爱人 提交于 2019-12-05 04:39:38

There is a bit of a gotcha with qualified and named injection with JSR-330 + Kotlin (Dagger2 is an implementation of this). From recently reviewing the backlog on the Dagger2 project on Github I know the Google team are looking to provide more proactive assistance/more helpful error messages within a forthcoming release (no timescales).

What you are missing is the @field:<Qualifier> annotation use-type targets as described in the linked documentation. So try;

@Inject @field:Spanish lateinit var holaMundoText: String

I think the issue is in Kotlin Compiler, it doesn't know place where put such annotation (param, setter, field and so on). To avoid ugly @field:Spanish (Spanish annotation class is tagged with Qualifier annotation) I've found another solution: Just tag Spanish annotation with Target annotation with appropriate params, see example:

@Qualifier
@Target(FUNCTION, CONSTRUCTOR, FIELD, VALUE_PARAMETER, PROPERTY_SETTER)
annotation class Spanish

then you can use:

@Inject @Spanish
lateinit var holaMundoText: String
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!