I’m trying to use HILT to inject an implementation of a repository class into the UseCase class (the...
l
I’m trying to use HILT to inject an implementation of a repository class into the UseCase class (the interface lives in the
domain
module, and the implementation in the
repository
module with the
repository
module depending on the
domain
one) but receiving an error when building the app: DomainModule.kt - lives into
:domain
module
Copy code
@Module
@InstallIn(SingletonComponent::class)
 object DomainModule {

    @Provides
    fun provideGetUpcomingLaunchesUseCase(repository: SpaceHubRepository): GetUpcomingLaunchesUseCase {
        return GetUpcomingLaunchesUseCaseImpl(repository)
    }

}
SpaceHubRepository.kt - lives into:
:domain
module
Copy code
interface SpaceHubRepository {
    suspend fun getUpcomingLaunches(): Flow<List<Launch>>
}
RepositoryModule.kt - lives into
:repository
module
Copy code
@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {

    @Provides
    fun provideRepository(
        apiService: SpaceHubApiService,
        launchMapper: LaunchMapper
    ): SpaceHubRepository {
        return SpaceHubRepositoryImpl(apiService, launchMapper)
    }

    @Provides
    fun provideLaunchMapper(): LaunchMapper = LaunchMapperImpl()
}
😶 3
🧵 1
This is the error im getting:
Copy code
/Users/lpirro/AndroidStudioProjects/SpaceHub/app/build/generated/hilt/component_sources/debug/com/lpirro/spacehub/SpaceHubApp_HiltComponents.java:128: error: [Dagger/MissingBinding] com.lpirro.domain.repository.SpaceHubRepository cannot be provided without an @Provides-annotated method.
  public abstract static class SingletonC implements SpaceHubApp_GeneratedInjector,
                         ^
      com.lpirro.domain.repository.SpaceHubRepository is injected at
          com.lpirro.domain.di.DomainModule.provideGetUpcomingLaunchesUseCase(repository)
      com.lpirro.domain.usecase.GetUpcomingLaunchesUseCase is injected at
          com.lpirro.launches.upcoming.viewmodel.UpcomingLaunchesViewModel(useCase)
      com.lpirro.launches.upcoming.viewmodel.UpcomingLaunchesViewModel is injected at
          com.lpirro.launches.upcoming.viewmodel.UpcomingLaunchesViewModel_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.lpirro.spacehub.SpaceHubApp_HiltComponents.SingletonC → com.lpirro.spacehub.SpaceHubApp_HiltComponents.ActivityRetainedC → com.lpirro.spacehub.SpaceHubApp_HiltComponents.ViewModelC]
c
You cannot easily dependency inject over multiple modules. This might help you get an idea. https://developer.android.com/training/dependency-injection/dagger-multi-module#dagger-dfm
y
Is the app depending on both domain and repository modules ?
l
@Yves Kalume I’ve added
api project(':repository')
to my
:app
module and now it’s working. But in terms of clean arch, feels a bit wrong to me that the app module has a dependency on the repository module, isn’t it?
y
l
Thank you mate
1364 Views