9

I'm doing a relativity simple implementation of Hilt. I've set it up and I can't seem to get around this error:

[Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context cannot be provided without an @Provides-annotated method.

@dagger.hilt.android.qualifiers.ApplicationContext android.content.Context is injected at
      com.greendotcorp.core.managers.featurecontrol.app.FeatureControlManager(context, …)
  com.greendotcorp.core.managers.featurecontrol.app.FeatureControlManager is injected at
      com.greendotcorp.core.features.dashboard.GridDashboardFragment.featureControlManager
  com.greendotcorp.core.features.dashboard.GridDashboardFragment is injected at
      
  com.greendotcorp.core.utils_theme.ViewModelDependencyInjector.inject

Here is my code:

@Singleton
@Component(modules = [ViewModelInjectorModule::class])
interface ViewModelDependencyInjector {
    fun inject(fragment: GridDashboardFragment)
}

@InstallIn(FragmentComponent::class)
@Module
object DashboardModule {

@Provides
@Singleton
fun provideFeatureComponentManager(@ApplicationContext context: Context) : FeatureControlManager {
    return FeatureControlManager.getInstance(context)
}

@AndroidEntryPoint
class GridDashboardFragment : BaseDetailFragment() {

   @Inject lateinit var featureControlManager: FeatureControlManager
}

I'm new to Hilt - any ideas?

Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106

1 Answers1

1

I think the issue is happening because in InstallIn() you are passing a fragment component and you are annotating your dependency with singleton , as far as i know that singleton can be applied to ApplicationComponent , so try to change @Singleton annotation to @FragmentScoped Annotation

@Provides  
@Singleton --> to @FragmentScoped
fun provideFeatureComponentManager(@ApplicationContext context: Context) : FeatureControlManager {
    return FeatureControlManager.getInstance(context)
}
Taki
  • 3,290
  • 1
  • 16
  • 41