java - 为什么我收到 @Provides-annotated 错误?

标签 java android kotlin dagger-2

我已经花了几个小时尝试在谷歌中查找我的两个 Dagger 实现的区别是什么。 是这样实现的

@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<eRetrofitModules, Retrofit>): IMatchPresenter = MatchPresenter(retrofitModules)
}

我正在获取所有Retrofit对象并将它们传递给 MathcPresenter一切都很好。 但我想得到 Map<Foo, Provider<Retrovit>>在我的主持人中。 所以,我加了这个词Provider论据

@Provides
@PreviewScope
fun provideMatchesPresenter(retrofitModules: Map<eRetrofitModules, 
Provider<Retrofit>>): IMatchPresenter = MatchPresenter(retrofitModules)

以及 MathcPresenter 的构造函数

class MatchPresenter(retrofitModules: Map<eRetrofitModules, Provider<Retrofit>>): IMatchPresenter

现在我不明白为什么,但我得到这样的错误

Error:(6, 1) error: [com.example.alexeyt.sunshinekotlin.moduls.previewmodule.PreviewComponent.inject(com.example.alexeyt.sunshinekotlin.ui.fragments.previewFragments.PreviewFragment)] java.util.Map> cannot be provided without an @Provides-annotated method.

<小时/>

预览范围

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PreviewScope

我做错了什么?

最佳答案

这可能是 Kotlin 处理泛型通配符的方式存在问题。

使用 Dagger 2 Multibinds 时,Dagger(它使用 Java Reflection 来分析代码并生成组件实现)将 Map 的类型解释为 Map<eRetrofitModules, ? extends Provider<Retrofit>> 。发生这种情况是因为 maps in Kotlin有他们的V类型参数标记为 out .

@JvmSuppressWildcards注释从编译的代码中删除该信息。只需在 Provider<Retrofit> 上使用该注释即可:

Map<eRetrofitModules, @JvmSuppressWildcards Provider<Retrofit>> .

您还可以找到this answer有趣。

关于java - 为什么我收到 @Provides-annotated 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991436/

相关文章:

Android TV 靠背。如何判断用户是否在 MainActivity 的标题导航上?

java - 如何将 mili 秒格式更改为 yyyy-mm-dd hh :mm:ss to insert to mysql with datetype colunm

java - Hibernate:如何将多个连接表映射到Java对象(用户的书籍列表)

java - 益智游戏的逻辑 - 我该怎么做?

android - 为什么谷歌播放服务项目为什么不使用 google-play-services.jar

android - ERROR : Failed to resolve: com. android.support.constraint :constraintlayout:1. 1.3 在项目结构对话框中显示受影响的模块:app

java - Kotlin 和 Java 中的 RuntimeException 和 GraphQLError

Kotlin 。从集合中获取不同的值

java - 玩!没有正确关闭 H2

android-fragments - 与底部导航栏android一起使用导航时,片段已添加问题