Reputation: 5233
I have already spent hours try to find in google what is the difference of my two dagger implementation. It was implemented like this
@Module
class MatchesModule
{
@Provides
@NetworkScope
@IntoMap
@RetrofitModulesName(eRetrofitModules.MATCHES)
fun retrofitMatches(okHttpClient: OkHttpClient, rxAdaptor: RxJava2CallAdapterFactory, iBuilder: Retrofit.Builder): Retrofit = iBuilder.addConverterFactory(GsonConverterFactory.create(mDeserializerMatches));
}
This method provide Retrofit object, also I am using annotation @IntoMap
and @RetrofitModulesName(...)
in order to put all of this Retrofit
objects to map.
@Module
class PreviewModule
{
@Provides
@PreviewScope
fun provideMatchesPresenter(retrofitModules: Map<eRetrofitModules, Retrofit>): IMatchPresenter = MatchPresenter(retrofitModules)
}
I am getting all Retrofit
object and passing them to MathcPresenter
all is ok and fine.
But I would like to get Map<Foo, Provider<Retrovit>>
in my presenter.
So, I added this word Provider
to argument
@Provides
@PreviewScope
fun provideMatchesPresenter(retrofitModules: Map<eRetrofitModules,
Provider<Retrofit>>): IMatchPresenter = MatchPresenter(retrofitModules)
as well as to the constructor of MathcPresenter
class MatchPresenter(retrofitModules: Map<eRetrofitModules, Provider<Retrofit>>): IMatchPresenter
And now I can not undersand why, but I get such error
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.
PreviewScope
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PreviewScope
What am I doing wrong?
Upvotes: 0
Views: 98
Reputation: 1257
This might be a problem with how Kotlin handles generics wildcards.
When using Dagger 2 Multibindinds, the type of the Map is interpreted by Dagger (which uses Java Reflection to analyse your code and generate Component implementations) as Map<eRetrofitModules, ? extends Provider<Retrofit>>
. This happens because maps in Kotlin have their V
type parameter marked as out
.
@JvmSuppressWildcards annotation removes that information from the compiled code. Just use that annotation on Provider<Retrofit>
:
Map<eRetrofitModules, @JvmSuppressWildcards Provider<Retrofit>>
.
You may also find this answer interesting.
Upvotes: 2