Home > Net >  Class cannot be provided without an @Provides-annotated method
Class cannot be provided without an @Provides-annotated method

Time:02-10

I'm using Hilt for dependency injection and getting an error saying:

[Dagger/MissingBinding] DialogsInteractor cannot be provided without @Provided-annoated method

I bind the class in an Activity scoped module:

@InstallIn(ActivityComponent::class)
@Module(includes = [ActivityModule.BindsModule::class])
class ActivityModule {
    ...
    @Module
    @InstallIn(ActivityComponent::class)
    interface BindsModule {
        ....
        @Binds
        fun bindDialogsInteractor(dialogsInteractorImpl: DialogsInteractorImpl): DialogsInteractor
    }
}

Also of course added the @Inject constructor:

@ActivityScoped
class DialogsInteractorImpl @Inject constructor(
    @ActivityContext context: Context,
    ....
){

I tried adding a @Provide injection method but still getting the same error.

Checked every dependency DialogsInteractorImpl has and nothing is wrong.

The weird thing is that I have other binds under BindsModule that don't cause any problem.

(I'm getting the problem on 5 different classes)

Thank you!

CodePudding user response:

As @IR42 stated,

you can't use ActivityScoped dependencies in ViewModels, check Component hierarchy

I changed the module scope to ActivityRetainedScoped and all the dependencies under it and it works.

The problem was that I tried to inject an @ActivityScoped dependency into a ViewModel and ViewModels are scoped to ActivityRetainedScoped and under.

  •  Tags:  
  • Related