我正在尝试使用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]
答案 0 :(得分:1)
似乎您的func BuildConfigFromKubeconfigGetter(masterUrl string, kubeconfigGetter KubeconfigGetter) (*restclient.Config, error)
有问题。
您在Scopes
中是Installing
您的RatesRepository
,但是您试图在更高级别的依赖项中使用它。我可以看到您正在尝试在FragmentComponent
和RatesRepository
do not relate to FragmentComponent
but ActivityRetainedComponent
中注入ViewModels
。
请参见this graph:
我希望在ViewModels
范围内维护Repository
,所以我将您的设置修改为:
App
如果@Module
@InstallIn(ApplicationComponent::class)
interface DataModule {
@Binds
fun bindsRatesRepository(ratesRepositoryImpl: RatesRepositoryImpl): RatesRepository
}
的依存关系范围较窄,则可以尝试使用RatesRepositoryImpl
而不是ActivityRetainedComponent
。