Sigitas
Sigitas

Reputation: 163

kotlin qualifier annotaion is ignored

Code below compiles the first time but fails on second build giving:

Error:SomeObject is bound multiple times:

@Provides @NotNull SomeObject SomeModule.provideSomeObject()

@Provides @NotNull SomeObject SomeModule.provideSomeScopedObject()

It seems that the compiler ignores Qualifier annotations on consecutive builds.

It does not help when component and module are written in java as annotation is ignored in Main class.

@dagger.Component(modules = arrayOf(SomeModule::class))
interface Component {
    fun inject(main: Main)
}

class Main {
    @field:[javax.inject.Inject SomeScope] lateinit var obj: SomeObject
}

@dagger.Module
class SomeModule {

    @dagger.Provides
    fun provideSomeObject(): SomeObject {
        return SomeObject("noScope")
    }

    @SomeScope
    @dagger.Provides
    fun provideSomeScopedObject(): SomeObject {
        return SomeObject("someScope")
    }
}

data class SomeObject(val name: String)

@javax.inject.Qualifier
@Retention(AnnotationRetention.SOURCE)
annotation class SomeScope

Upvotes: 1

Views: 844

Answers (1)

AndroidEx
AndroidEx

Reputation: 15824

Please try removing this line @Retention(AnnotationRetention.SOURCE). As far as I know Dagger 2 requires RUNTIME retention (which is the default one in Kotlin, hence no need to specify it explicitly).

Upvotes: 1

Related Questions