8

I'm getting this error error: [Dagger / MissingBinding] com.eduramza.domain.repositories.RemoteRepository cannot be provided without an @ Provides-annotated method. when implementing my repository interface with android hilt.

That's because my useCase implements my repository interface. What may be wrong with my implementation, below is the code:

app.Viewmodel:

@HiltViewModel
class RemoteListViewModel @Inject constructor(
    private val useCase: GetTickersUseCase
    ): ViewModel() {
}

domain.usecase:

class GetTickersUseCase @Inject constructor(
    private val remoteRepository: RemoteRepository)
    : SingleUseCase<MainCoins> {

    override suspend fun executeCall(): Flow<Result<MainCoins>> = remoteRepository.readAllTickers()

}

domain.repository:

interface RemoteRepository {
    suspend fun readAllTickers(): Flow<Result<MainCoins>>
}

core.repositoryImpl:

class RemoteRepositoryImpl @Inject constructor(
    private val apiService: BraziliexService,
    private val tickersMapper: TickersMapper
) : RemoteRepository{

    override suspend fun readAllTickers(): Flow<Result<MainCoins>> {
        TODO("Not yet implemented")
    }
}

core.module:

@Module
@InstallIn(ActivityComponent::class)
abstract class RemoteModule {

@Binds
abstract fun bindRemoteRepository(
    remoteRepositoryImpl: RemoteRepositoryImpl
    ): RemoteRepository
}

My multimodule app in this structure enter image description here where core implement domain, and app implement both.

why is the bind method not being initialized?

enter image description here

  • you are expecting a `RemoteRepository` to provide a `RemoteRepository` . You need expect `BraziliexService` and `TickersMapper` instances to create a `RemoteRepository` . Also Is there a need to have `RemoteRepository` of Activity Scoped it can be of Singleton Scope i guess. – ADM May 06 '21 at 05:03
  • And please post the whole dagger code for all dependency and try with my answer? – Nitin Prakash May 06 '21 at 05:45

1 Answers1

15

You using the ActivityComponent but the RemoteRepository is the indirect dependency of ViewModel so it should be tied with the ViewModel Lifecycle

so instead of ActivityComponent

@Module
@InstallIn(ActivityComponent::class)
abstract class RemoteModule {

@Binds
abstract fun bindRemoteRepository(
    remoteRepositoryImpl: RemoteRepositoryImpl
    ): RemoteRepository
}

Use this ViewModelComponent

@Module
    @InstallIn(ViewModelComponent::class)
    abstract class RemoteModule {
    
    @Binds
    abstract fun bindRemoteRepository(
        remoteRepositoryImpl: RemoteRepositoryImpl
        ): RemoteRepository
    }
Nitin Prakash
  • 927
  • 9
  • 16