Android匕首2:如果没有@Provides注释的方法,则无法提供依赖项

时间:2018-11-14 13:34:30

标签: android kotlin dagger-2

匕首2依赖项:

const deletedValuesfromArray =
     [
        { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
        { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
     ]

  const normalArray =
   [
    { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 },
     { "property_name": "Property two", "property_type": 5, "property_required": true, "property_origin": 1 }, 
     { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
     { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
   ]

const result = normalArray.filter(obj => !deletedValuesfromArray.find(o => JSON.stringify(o) == JSON.stringify(obj)));

console.log(result);

我的implementation "com.google.dagger:dagger:2.15" kapt "com.google.dagger:dagger-compiler:2.15"

AppComponent

我的@Singleton @Component(modules = [ DomainModule::class, DataModule::class, PresentationModule::class, ViewModelModule::class, RepositoriesModule::class ]) interface AppComponent { //reps fun topicsRep(): TopicsRepository fun countriesRep(): CountriesRepository fun loginRep(): LoginRepository } 班:

ViewModelModule

我的@Module abstract class ViewModelModule { @Binds internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory @Binds @IntoMap @ViewModelKey(LoginViewModel::class) internal abstract fun loginViewModel(viewModel: LoginViewModel): ViewModel @Binds @IntoMap @ViewModelKey(CountriesViewModel::class) internal abstract fun countriesViewModel(viewModel: CountriesViewModel): ViewModel }

ViewModelFactory

例如我的 @Suppress("UNCHECKED_CAST") @Singleton class ViewModelFactory @Inject constructor(private val viewModels: MutableMap<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>) : ViewModelProvider.Factory { override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T } @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) @kotlin.annotation.Retention(AnnotationRetention.RUNTIME) @MapKey internal annotation class ViewModelKey(val value: KClass<out ViewModel>) CountryComponent

CountryModule

我要实现的目标-我需要为我的活动和片段注入@ActivityScope @Component(modules = [CountryModule::class], dependencies = [AppComponent::class]) interface CountryComponent { fun inject(activity: SelectCountryActivity) } @Module class CountryModule { @Provides @Singleton fun provideCountriesInteractor(rep: CountriesRepository) = SelectCountryInteractor(rep) } 实例。我的视图模型包含其他依赖项。还要尝试为每个屏幕分离依赖项。

构建后出现错误:

ViewModelFactory

CountryComponent.java:10: error: 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 java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> is injected at ViewModelFactory.<init>(viewModels) ViewModelFactory is injected at SelectCountryActivity.factory SelectCountryActivity is injected at CountryComponent.inject(activity) 设置为ViewModelFactory时出错:

@Singleton

是否有任何理由将ViewModel标记为CountryComponent scoped with @ActivityScope may not reference bindings with different scopes: @dagger.Component(modules = {CountryModule.class}, dependencies = {AppComponent.class}) @Singleton class ViewModelFactory AppComponent.java:6: error: AppComponent scoped with @Singleton may not reference bindings with different scopes: @dagger.Component(modules = {DomainModule.class, DataModule.class, PresentationModule.class, ViewModelModule.class, RepositoriesModule.class}) Singleton

1 个答案:

答案 0 :(得分:0)

通过添加

修复
fun viewModelFactory(): ViewModelFactory

到我的AppComponent。 还从@Singleton

中删除了ViewModelFactory