Error de inyección de Kotlin dagger 2 Android ViewModel

Estoy tratando de usar dagger 2 en mi aplicación Android para inyectar el nuevo ViewModel desde la biblioteca de android de arch.

De lo que veo en este ejemplo https://github.com/googlesamples/android-architecture-components/tree/e33782ba54ebe87f7e21e03542230695bc893818/GithubBrowserSample , necesito usar esto:

@MustBeDocumented @Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) @Retention(AnnotationRetention.RUNTIME) @MapKey internal annotation class ViewModelKey(val value: KClass<out ViewModel>) @Module abstract class ViewModelModule { @Binds @IntoMap @ViewModelKey(LoginViewModel::class) internal abstract fun bindLoginViewModel(viewModel: LoginViewModel): LoginViewModel @Binds @IntoMap @ViewModelKey(MainMenuViewModel::class) internal abstract fun bindSearchViewModel(viewModel: MainMenuViewModel): MainMenuViewModel @Binds internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory } @ApplicationScope @Component(modules = arrayOf(ApplicationModule::class, NetworkModule::class, ViewModelModule::class)) interface ApplicationComponent { fun plusActivityComponent(activityModule: ActivityModule): ActivityComponent fun inject(application: LISAApplication) } 

Y mi fábrica es:

 @ApplicationScope class ViewModelFactory @Inject constructor(private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") 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) } } } 

Pero el proyecto no comstack 🙁 Tengo este error (Map <…> no se puede proporcionar sin un método @ Provides-anotado):

 Using Kotlin incremental comstacktion :mobile:transformDataBindingWithDataBindingMergeArtifactsForDebug UP-TO-DATE :mobile:kaptDebugKotlin e: /Users/jaumard/LISAProjects/LISA/mobile/build/tmp/kapt3/stubs/debug/com/mylisabox/lisa/dagger/components/ApplicationComponent.java:6: error: [com.mylisabox.lisa.dagger.components.ActivityComponent.inject(com.mylisabox.lisa.common.BaseActivity)] java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method. e: e: public abstract interface ApplicationComponent { e: ^ e: java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> is injected at e: com.mylisabox.lisa.dagger.ViewModelFactory.<init>(creators) e: com.mylisabox.lisa.dagger.ViewModelFactory is injected at e: com.mylisabox.lisa.common.BaseActivity.factory e: com.mylisabox.lisa.common.BaseActivity is injected at e: com.mylisabox.lisa.dagger.components.ActivityComponent.inject(activity) e: /Users/jaumard/LISAProjects/LISA/mobile/build/tmp/kapt3/stubs/debug/com/mylisabox/lisa/dagger/components/ActivityComponent.java:4: error: com.mylisabox.lisa.dagger.components.ActivityComponent scoped with @com.mylisabox.network.dagger.annotations.ActivityScope may not reference bindings with different scopes: e: 

Alguna idea sobre como arreglar esto?

    Así que bien, encontré el problema, el problema estaba en mi ViewModelModule. Debo devolver ViewModel de mis methods abstractos, no directamente del tipo que deseo. Se volverá así entonces:

     @Module abstract class ViewModelModule { @Binds @IntoMap @ViewModelKey(LoginViewModel::class) internal abstract fun bindLoginViewModel(viewModel: LoginViewModel): ViewModel @Binds @IntoMap @ViewModelKey(MainMenuViewModel::class) internal abstract fun bindSearchViewModel(viewModel: MainMenuViewModel): ViewModel @Binds internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory }