androiddependency-injectiondagger-hiltclean-architecturemulti-module

hilt: error: [Dagger/MissingBinding] Repository cannot be provided without an @Provides-annotated method


I am creating a multi module android app with clean architecture layers as modules:

root >
       app (main module)
       presentation (android lib module) //it has dependency on domain in build.gradle
       domain (android lib module)
       data (android lib module) //it has dependency on domain in build.gradle

Domain module has UseCase and Repository interface like:

package com.cleanarchitecturesample.domain.usecase.user


class SaveUserUseCase @Inject constructor(
     private val repository: UserCredentialsRepository,
     @IO private val dispatcher: CoroutineDispatcher
) {

     ...
}

package com.cleanarchitecturesample.domain.respository

interface UserCredentialsRepository {

...

}

and the implementation of Repository & its di is placed in Data module with data sources like:

package com.cleanarchitecturesample.data.repoimpl

import com.cleanarchitecturesample.data.source.local.UserCredentialsLDS
import com.cleanarchitecturesample.domain.respository.UserCredentialsRepository

class UserCredentialsRepositoryImpl(
     private val userCredentialsLDS: UserCredentialsLDS
) : UserCredentialsRepository {

    ...
}

package com.cleanarchitecturesample.data.di

import com.cleanarchitecturesample.data.repoimpl.UserCredentialsRepositoryImpl
import com.cleanarchitecturesample.data.source.local.UserCredentialsLDS
import com.cleanarchitecturesample.domain.respository.UserCredentialsRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {


    @Singleton
    @Provides
    fun provideUserCredentialsRepository(
        userCredentialsLDS: UserCredentialsLDS
    ): UserCredentialsRepository = UserCredentialsRepositoryImpl(userCredentialsLDS)

}

Everything seems to be fine but still I am getting following error:

/AndroidStudioProjects/CleanArchitectureSample/app/build/generated/hilt/component_sources/debug/com/cleanarchitecturesample/app/CleanArchitectureApp_HiltComponents.java:137: error: [Dagger/MissingBinding] com.cleanarchitecturesample.domain.respository.UserCredentialsRepository cannot be provided without an @Provides-annotated method. public abstract static class SingletonC implements CleanArchitectureApp_GeneratedInjector, ^ com.cleanarchitecturesample.domain.respository.UserCredentialsRepository is injected at com.cleanarchitecturesample.domain.usecase.user.IsValidUserCredentialsUseCase(repository, …) com.cleanarchitecturesample.domain.usecase.user.IsValidUserCredentialsUseCase is injected at com.cleanarchitecturesample.presentation.vm.UserAuthenticationVM(…, isValidUserCredentialsUseCase, …) com.cleanarchitecturesample.presentation.vm.UserAuthenticationVM is injected at com.cleanarchitecturesample.presentation.vm.UserAuthenticationVM_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.cleanarchitecturesample.app.CleanArchitectureApp_HiltComponents.SingletonC → com.cleanarchitecturesample.app.CleanArchitectureApp_HiltComponents.ActivityRetainedC → com.cleanarchitecturesample.app.CleanArchitectureApp_HiltComponents.ViewModelC] It is also requested at: com.cleanarchitecturesample.domain.usecase.user.SaveUserUseCase(repository, …)

Note: I have also tried di for Repository using the abstract class and binds syntax with even that was showing the same error.

Any help would be appriciated. Thanks in Advance!! :)


Solution

  • Add data module as a dependency of app module.

    Dagger generated codes stay in app module as such if you don't set data module as a dependency dagger won't be able to access your RepositoryModule