迁移Android Studio和Kotlin插件后,Dagger注入停止工作

时间:2019-04-19 18:58:19

标签: android kotlin dagger-2

我写了一个小应用程序,它使用dagger2注入依赖项。一切都运转良好(去年),但是现在,经过6个月后,该应用似乎在迁移了Android Studio + gradle + kotlin插件后就停止工作了。

迁移后,每当我尝试运行该应用程序时,都会出现以下错误:

e: D:\code\work\GESAndroid\app\build\tmp\kapt3\stubs\debug\pt\gov\madeira\newsapp\di\AppComponent.java:8: error: [Dagger/MissingBinding] 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.
 public abstract interface AppComponent extends dagger.android.AndroidInjector<pt.gov.madeira.newsapp.MyApp> {
                ^
       java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> is injected at
           pt.gov.madeira.newsapp.di.viewmodels.ViewModelFactory(viewModels)
       pt.gov.madeira.newsapp.di.viewmodels.ViewModelFactory is injected at
           pt.gov.madeira.newsapp.di.viewmodels.ViewModelModule.bindViewModelFactory$app_debug(factory)
      android.arch.lifecycle.ViewModelProvider.Factory is injected at
          pt.gov.madeira.newsapp.ui.main.MainActivity.viewModelFactory
       pt.gov.madeira.newsapp.ui.main.MainActivity is injected at
          dagger.android.AndroidInjector.inject(T) [pt.gov.madeira.newsapp.di.AppComponent ? pt.gov.madeira.newsapp.di.ActivityBindingModule_MainActivity$app_debug.MainActivitySubcomponent]
  The following other entry points also depend on it:
      dagger.android.AndroidInjector.inject(T) [pt.gov.madeira.newsapp.di.AppComponent ? pt.gov.madeira.newsapp.di.ActivityBindingModule_ShowDetailsActivity$app_debug.ShowDetailsActivitySubcomponent]

现在,有人可以帮忙翻译错误吗?似乎是在抱怨ViewModel注入,但是我不确定为什么会这样。

顺便说一句,这是我用来注入视图模型的代码:

class ViewModelFactory @Inject constructor(
        private val viewModels: @JvmSuppressWildcards  Map<Class<out ViewModel>, Provider<ViewModel>>
) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T
        = viewModels[modelClass]?.get() as T
}

@Module
abstract class ViewModelModule {
    @Binds
    internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}

这是活动绑定代码:

@Module
abstract class ActivityBindingModule {
    @ActivityScoped
    @ContributesAndroidInjector(modules = [MainActivityModule::class])
    internal abstract fun mainActivity(): MainActivity

    @ActivityScoped
    @ContributesAndroidInjector(modules = [ShowDetailsModule::class])
    internal abstract fun showDetailsActivity(): ShowDetailsActivity
}

这是活动的注入代码:

@Module
abstract class ShowDetailsModule {
    @Binds
    @IntoMap
    @ViewModelKey(ShowDetailsViewModel::class)
    abstract fun bindShowDetaislViewModel(viewModel: ShowDetailsViewModel): ViewModel

}

@Module
abstract class MainActivityModule {
    @Binds
    @IntoMap
    @ViewModelKey(MainActivityViewModel::class)
    internal abstract fun bindMainActivityViewModel(viewModel: MainActivityViewModel): ViewModel
}

最后,这是属性的代码:

@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
annotation class ActivityScoped

@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey (val value: KClass<out ViewModel>) {
}

那么,有人能指出我正确的方向吗?

谢谢。

0 个答案:

没有答案