最近、kotlinでdagger-2を使い始めました。残念ながら、私は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)
}
誰が問題になるのか知っていますか?
コンポーネントに@Singletonを追加
@Singleton
@Subcomponent(modules = arrayOf(NetModule::class))
interface NetComponent {
fun inject(service: CurrencyDownloaderService)
}