@Qualifier annotated implementations of an interface fails to compile when used in a clean architecture style multi-module project.
Say we have the following module structure: :domain
, :presentation
(includes :domain), :data
(includes :domain).
In :domain
we define
interface RepositoryInterface
// and annotation
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class SomeRepository1Annotation
Then in :data
we define implementation
class SomeRepository1 : RepositoryInterface
// and provide dependency
@Module
@InstallIn(SingletonComponent::class)
object DataModule {
@SomeRepository1Annotation
@Singleton
@Provides
fun provideSomeRepository1(): RepositoryInterface = SomeRepository1()
In :presentation
we try to inject several implementations of the same interface
class Main @Inject constructor(
@SomeRepository1Annotation val repo1: RepositoryInterface,
@SomeRepository2Annotation val repo2: RepositoryInterface,
It fails to compile with error: [Dagger/DuplicateBindings] org.daggercleanarchitecture.domain.RepositoryInterface is bound multiple times:
But interestingly enough this same setup compiles just fine when using @Named
annotation.
Error log
daggercleanarchitectureAndroidApplication_HiltComponents.java:127: error: [Dagger/DuplicateBindings] org.daggercleanarchitecture.domain.RepositoryInterface is bound multiple times:
public abstract static class SingletonC implements FragmentGetContextFix.FragmentGetContextFixEntryPoint,
^
@org.daggercleanarchitecture.domain.SomeRepository1Annotation @Singleton @Provides org.daggercleanarchitecture.domain.RepositoryInterface org.daggercleanarchitecture.data.DataModule.provideSomeRepository1()
@org.daggercleanarchitecture.domain.SomeRepository2Annotation @Singleton @Provides org.daggercleanarchitecture.domain.RepositoryInterface org.daggercleanarchitecture.data.DataModule.provideSomeRepository2()
org.daggercleanarchitecture.domain.RepositoryInterface is injected at
New contributor