简体   繁体   English

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

[英]Android dagger 2: dependency cannot be provided without an @Provides-annotated method

Dagger 2 dependencies: Dagger 2依赖项:

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

My AppComponent : 我的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
}

My ViewModelModule class: 我的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
}

My ViewModelFactory : 我的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>)

And for example my CountryComponent and CountryModule : 再举例来说,我CountryComponentCountryModule

@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)
}

What i am trying to achieve - i need to inject an instance of ViewModelFactory to my activities and fragments. 我要实现的目标-我需要将ViewModelFactory实例注入到我的活动和片段中。 My viewmodels contains others dependensies. 我的视图模型包含其他依赖项。 Also trying to separate dependencies for each screen. 还尝试为每个屏幕分离依赖项。

After build getting error: 构建后出现错误:

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)

When setting ViewModelFactory as @Singleton getting error: ViewModelFactory设置为@Singleton出现错误:

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})

Is there any reason to mark ViewModel as Singleton or ActivityScope ? 是否有任何理由将ViewModel标记为SingletonActivityScope

Fixed by adding 通过添加固定

fun viewModelFactory(): ViewModelFactory

to my AppComponent . 到我的AppComponent Also removed @Singleton from ViewModelFactory 还从ViewModelFactory删除了@Singleton

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果没有带有限定依赖项的 @Provides-annotated 方法,则无法提供 Dagger 2 - Dagger 2 cannot be provided without an @Provides-annotated method with a qualified dependency Dagger 2错误:依赖项“如果没有@Provides注释的方法就无法提供” - Dagger 2 error: dependency “cannot be provided without an @Provides-annotated method" 不能在没有 @Provides 注释的方法的情况下提供(Android Kotlin 中的 Dagger 2) - Cannot be provided without an @Provides-annotated method (Dagger 2 in Android Kotlin) 如果没有提供注释的方法,则无法提供 Android 匕首 - Android dagger cannot be provided without an Provides-annotated method Dagger 2-如果没有@Provides注释的方法,则无法提供 - Dagger 2 - Cannot be provided without an @Provides-annotated method 如果没有@Provides注释的方法,则无法提供Dagger AndroidInjector - Dagger AndroidInjector cannot be provided without an @Provides-annotated method 如果没有@Provides注释的方法,则无法提供Dagger / MissingBinding - Dagger/MissingBinding cannot be provided without an @Provides-annotated method Dagger Hilt:如果没有 @Provides-annotated 方法,则无法提供存储库 - Dagger Hilt: Repository cannot be provided without an @Provides-annotated method 如果没有@Provides 注释的方法 Dagger/MissingBinding,则无法提供 - cannot be provided without an @Provides-annotated method Dagger/MissingBinding 匕首柄:如果没有@Provides-annotated 方法,则无法提供 - Dagger Hilt: cannot be provided without an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM