I'm using the new Architecture Components with Dagger2 and I would like to inject my ViewModels using a Factory class. The Factory class is itself injectable. This all works well when the Factory class is defined in Java, but when I convert it to Kotlin, Dagger2 does not know how to generate the Map for the constructor, while in Java it knows how to do so. I presume the difference is that, after conversion, the Factory class uses the Map from the kotlin package, as opposed to from java.util.Map package. How can I get Dagger2 to generate the map for the Factory constructor?
Here's the Factory class
@ActivityScope
class MainActivityViewModelFactory @Inject
constructor(private val creators: Map<Class<out ViewModel>, Provider<ViewModel>>) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
var creator: Provider<out ViewModel>? = creators[modelClass]
if (creator == null) {
for ((key, value) in creators) {
if (modelClass.isAssignableFrom(key)) {
creator = value
break
}
}
}
if (creator == null) {
throw IllegalArgumentException("unknown model class " + modelClass)
}
try {
return creator.get() as T
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
and this is the error
Error:java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,? extends javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
I tried creating a module to provide the map, but that didn't help.
@ActivityScope
@Module
class MapModule {
@Provides
fun provideMap(): Map<Class<out ViewModel>, Provider<ViewModel>> = mutableMapOf()
}