我最近开始使用dagger-2和kotlin。不幸的是,我已经遇到了sumbcomponants的问题,我无法理解为什么会出现这个gradle错误:
...NetComponent (unscoped) may not reference scoped bindings:
@dagger.Subcomponent(modules =
{com.example.kremik.cryptodroid.di.module.NetModule.class})
@Singleton @Provides com.example.kremik.cryptodroid.data.remote.CMCService com.example.kremik.cryptodroid.di.module.NetModule.providesCMCService(retrofit2.Retrofit)
@Singleton @Provides retrofit2.Retrofit com.example.kremik.cryptodroid.di.module.NetModule.providesRetrofit()
@org.jetbrains.annotations.NotNull @Singleton @Provides com.example.kremik.cryptodroid.data.service.CurrencyDataPresenter com.example.kremik.cryptodroid.di.module.NetModule.providesCurrencyDataPresenter(com.example.kremik.cryptodroid.data.local.CurrenciesProvider, com.example.kremik.cryptodroid.ui.LivePrices.LivePricesPresenter)
模块:应用
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.kremik.cryptodroid"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
// dataBinding {
// enabled = true
// }
kapt {
generateStubs = true
}
}
dependencies {
(...)
//Dagger2
implementation 'com.google.dagger:dagger:2.11'
implementation 'com.google.dagger:dagger-android-support:2.11'
implementation 'com.google.dagger:dagger-android-processor:2.11'
kapt 'com.google.dagger:dagger-compiler:2.11'
(...)
}
NetModule: @Module class NetModule(private val BASE_URL:String){
@Provides
@Singleton
fun providesCurrencyDataPresenter(provider: CurrenciesProvider,
pricesPresenter: LivePricesPresenter) =
CurrencyDataPresenter(provider, pricesPresenter)
@Provides
@Singleton
fun providesRetrofit() = Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
@Provides
@Singleton
fun providesCMCService(retrofit: Retrofit) =
retrofit.create(CMCService::class.java)
}
NetComponent:
@Subcomponent(modules = arrayOf(NetModule::class))
interface NetComponent {
fun inject(service: CurrencyDownloaderService)
}
有谁知道这可能是什么问题?
答案 0 :(得分:20)
将@Singleton与您的组件
放在一起@Singleton
@Subcomponent(modules = arrayOf(NetModule::class))
interface NetComponent {
fun inject(service: CurrencyDownloaderService)
}