为什么我得到@提供注释错误?
我已经花了几个小时尝试在谷歌find我的两个匕首实施的区别是什么。 它是这样实现的
@Module class MatchesModule { @Provides @NetworkScope @IntoMap @RetrofitModulesName(eRetrofitModules.MATCHES) fun retrofitMatches(okHttpClient: OkHttpClient, rxAdaptor: RxJava2CallAdapterFactory, iBuilder: Retrofit.Builder): Retrofit = iBuilder.addConverterFactory(GsonConverterFactory.create(mDeserializerMatches)); }
此方法提供Retrofit对象,我也使用注释@IntoMap
和@RetrofitModulesName(...)
为了把所有这些Retrofit
对象映射。
@Module class PreviewModule { @Provides @PreviewScope fun provideMatchesPresenter(retrofitModules: Map): IMatchPresenter = MatchPresenter(retrofitModules) }
我得到所有Retrofit
对象,并将它们传递给MathcPresenter
一切都很好。 但是我想在我的演示Map<Foo, Provider>
获得Map<Foo, Provider>
。 所以,我添加了这个词Provider
的论点
@Provides @PreviewScope fun provideMatchesPresenter(retrofitModules: Map<eRetrofitModules, Provider>): IMatchPresenter = MatchPresenter(retrofitModules)
以及MathcPresenter
的构造MathcPresenter
class MatchPresenter(retrofitModules: Map<eRetrofitModules, Provider>): IMatchPresenter
现在我不能理解为什么,但我得到这样的错误
错误:(6,1)错误:[com.example.alexeyt.sunshinekotlin.moduls.previewmodule.PreviewComponent.inject(com.example.alexeyt.sunshinekotlin.ui.fragments.previewFragments.PreviewFragment)] java.util.Map> can not没有@提供注释的方法提供。
PreviewScope
@Scope @Retention(AnnotationRetention.RUNTIME) annotation class PreviewScope
我究竟做错了什么?
这可能是Kotlin如何处理generics通配符的一个问题。
当使用Dagger 2 Multibindinds时,Map的types被Dagger(它使用Java Reflection来分析你的代码并生成Component实现)解释为Map
Map
。 发生这种情况是因为Kotlin中的地图将其V
型参数标记为out
。
@JvmSuppressWildcards批注从编译的代码中删除这些信息。 只需在Provider
上使用该注释:
Map
。
你也可能会发现这个答案有趣。