By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account
[Dagger/MissingBinding] com.xxx.data.repo.UserPreferenceDataRepository cannot be provided without an @Provides-annotated method.
  public abstract static class SingletonC implements AppStart_GeneratedInjector,
      com.xxx.data.repo.UserPreferenceDataRepository is injected at
          com.example.myapplication.MainViewModel(dataSource)
      com.example.myapplication.MainViewModel is injected at
          com.example.myapplication.MainViewModel_HiltModules.BindsModule.binds(arg0)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.example.myapplication.AppStart_HiltComponents.SingletonC → com.example.myapplication.AppStart_HiltComponents.ActivityRetainedC → com.example.myapplication.AppStart_HiltComponents.ViewModelC]

I did it as follows

  • Application:
  • @HiltAndroidApp
    class AppStart: Application()
    
  • MainActivity
  • @AndroidEntryPoint
    class MainActivity : ComponentActivity() {
     .....
    val mainViewModel:MainViewModel = hiltViewModel()
    
  • MainViewModel
  • @HiltViewModel
    class MainViewModel @Inject constructor(
        private val dataSource: UserPreferenceDataRepository
    ) :ViewModel() {
    
  • interface DataModule
  • @Module
    @InstallIn(SingletonComponent::class)
    interface DataModule {
        @Binds
        fun bindsUserDataRepository(
            userDataRepository: UserPreferencesDataImpl,
        ): UserPreferenceDataRepository
    
  • UserPreferenceDataRepository
  • interface UserPreferenceDataRepository {
    
  • UserPreferencesDataImpl
  • class UserPreferencesDataImpl @Inject constructor(private val store: UserPreferencesDataSource):
        UserPreferenceDataRepository {
    

    how to solve this problem?
    If I delete @HiltViewModel it can run normally, if I don't delete it, I will report an error。

    Are all of these classes in the same Gradle module? If not, are you applying the hilt-compiler processor in all of your modules (specifically the one with the @Module)?

    Thanks for the reminder, let me solve this problem, thanks again, this has been bugging me for 1 day