36
votes

I've recently started using dagger-2 with kotlin. Unfortunately I have ecnountered a problem with sumbcomponants and I have trouble understanding why I get this gradle error:

...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)

Module:app

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)
}

Does anyone know what could be the issue ?

1
you have to add same scope to Component, try: @Singleton @Subcomponent(modules = arrayOf(NetModule::class))Rafal Malek
I doesn't help as it shouldn't: stackoverflow.com/questions/37797519/…Kremik13
Modules in subcomponent should have another scope as in parent component. Try to use custom scopes for your moduleEduard Kornev
The error message tells you exactly what is wrong. Your subcomponent is unscoped. It can't reference scoped (@Singleton) bindings. The solution is to define a new scope for your SubcomponentDavid Rawson

1 Answers

63
votes

Put @Singleton with your Component

@Singleton
@Subcomponent(modules = arrayOf(NetModule::class))
interface NetComponent {
fun inject(service: CurrencyDownloaderService)
}