从另一个Gradle模块中注入实现

时间:2020-08-27 23:18:06

标签: android dagger-hilt

我正在尝试使用hilt注入存储库类的实现(接口位于domain模块中,data模块中的实现与data模块有关,具体取决于domain上),但在构建应用程序时收到错误消息。这是我的设置

domain模块中

interface RatesRepository {
    fun getRates(): Single<Rates>
}

data模块中

class RatesRepositoryImpl @Inject constructor(
    private val rateDataSource: CurrencyExchangeDataSource,
    private val mapper: ExchangeResponseToRates
) : RatesRepository {
//..
}

@Module
@InstallIn(FragmentComponent::class)
interface DataModule {
    @Binds
    fun bindsRatesRepository(ratesRepositoryImpl: RatesRepositoryImpl): RatesRepository
}

数据将域声明为依赖项

编辑: 我收到的错误:

error: [Dagger/MissingBinding] com.basil.domain_converter.repository.RatesRepository cannot be provided without an @Provides-annotated method.
  public abstract static class ApplicationC implements CurrenciesApp_GeneratedInjector,
                         ^
      com.basil.domain_converter.repository.RatesRepository is injected at
          com.basil.domain_converter.usecase.GetRatesUseCase(ratesRepository)
      javax.inject.Provider<com.basil.domain_converter.usecase.GetRatesUseCase> is injected at
          com.basil.ui_converter.ui.RatesViewModel_AssistedFactory(getRatesUseCase)
      com.basil.ui_converter.ui.RatesViewModel_AssistedFactory is injected at
          com.basil.ui_converter.ui.RatesViewModel_HiltModule.bind(arg0)
      java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>> is injected at
          androidx.hilt.lifecycle.ViewModelFactoryModules.ActivityModule.provideFactory(…, viewModelFactories)
      @dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory java.util.Set<androidx.lifecycle.ViewModelProvider.Factory> is requested at
          dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.ActivityEntryPoint.getActivityViewModelFactory() [com.basil.example.CurrenciesApp_HiltComponents.ApplicationC → com.basil.example.CurrenciesApp_HiltComponents.ActivityRetainedC → com.basil.example.CurrenciesApp_HiltComponents.ActivityC]
  The following other entry points also depend on it:
      dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.FragmentEntryPoint.getFragmentViewModelFactory() [com.basil.example.CurrenciesApp_HiltComponents.ApplicationC → com.basil.example.CurrenciesApp_HiltComponents.ActivityRetainedC → com.basil.example.CurrenciesApp_HiltComponents.ActivityC → com.basil.example.CurrenciesApp_HiltComponents.FragmentC]

1 个答案:

答案 0 :(得分:1)

合理的解释

似乎您的func BuildConfigFromKubeconfigGetter(masterUrl string, kubeconfigGetter KubeconfigGetter) (*restclient.Config, error) 有问题。

您在Scopes中是Installing您的RatesRepository,但是您试图在更高级别的依赖项中使用它。我可以看到您正在尝试在FragmentComponentRatesRepository do not relate to FragmentComponent but ActivityRetainedComponent中注入ViewModels

请参见this graphcomponent hierarchy


解决方案

我希望在ViewModels范围内维护Repository,所以我将您的设置修改为:

App

如果@Module @InstallIn(ApplicationComponent::class) interface DataModule { @Binds fun bindsRatesRepository(ratesRepositoryImpl: RatesRepositoryImpl): RatesRepository } 的依存关系范围较窄,则可以尝试使用RatesRepositoryImpl而不是ActivityRetainedComponent