Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@dagger.hilt.android.qualifiers.ApplicationContext android.content.Context cannot be provided without an @Provides-annotated method

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?

like image 537
Kristy Welsh Avatar asked Nov 06 '22 05:11

Kristy Welsh


1 Answers

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)
}
like image 72
Taki Avatar answered Nov 15 '22 08:11

Taki