estn
estn

Reputation: 1203

Dagger Kotlin qualifier constructor injection doesn't work

I have following module with @Provides method with a qualifier

@Module
class VocabularyModule {

    @VocabularyProviders
    @Singleton
    @Provides
    fun provideVocabularies(): List<VocabularyProvider> {
        return listOf(
                AnimalsVocabularyProvider(),
                FamilyVocabularyProvider(),
                FoodVocabularyProvider(),
                NumberVocabularyProvider(),
                ColorsVocabularyProvider(),
                FreeTimeVocabularyProvider(),
                SportVocabularyProvider(),
                NatureVocabularyProvider(),
                PeopleVocabularyProvider(),
                TransportationVocabularyProvider()
        )
    }
}

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class VocabularyProviders

Then there is my class that I want to inject this list into via constructor and the qualifier:

class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers: List<VocabularyProvider>) {

    fun getVocabulary(category: VocabularyCategory): Vocabulary {
        for (provider in providers) {
            if (category == provider.category) {
                return provider.vocabulary
            }
        }
        throw IllegalStateException("didn't find provider that could provide vocabulary of $category category")
    }

}

I am getting this error but everything looks correct

11: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] @cz.ejstn.learnlanguageapp.core.dagger.module.vocabulary.VocabularyProviders java.util.List<? extends cz.ejstn.learnlanguageapp.vocabulary.model.factory.VocabularyProvider> cannot be provided without an @Provides-annotated method.

Upvotes: 0

Views: 913

Answers (1)

estn
estn

Reputation: 1203

I continued leafing through similar questions and came across this one: Dagger 2 multibindings with Kotlin

So from my understanding kotlin compiler "messes a bit" with generic types in parameters, which resulted in dagger being unable to link these I guess.

I changed constructor of the factory like this to avoid that - addind @JvmSuppressWildcards annotation:

class VocabularyFactory
@Inject constructor(@param:VocabularyProviders val providers:@JvmSuppressWildcards List<VocabularyProvider>) {
...
}

Keeping this question here because more ppl are likely to run into this

Upvotes: 3

Related Questions