Okay, let's make this simple.
I have created a simple library called my-network-library with two classes in it. First one is a Hilt module called BaseNetworkModule
@Module
@InstallIn(ApplicationComponent::class)
object BaseNetworkModule {
// Client
@Singleton
@Provides
fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
// my default okhttp setup goes here
.build()
}
}
and the second one is a simple class.
class MyAwesomeClass {
fun doMagic() {
// magic code goes here
}
}
Now I want to use MyAwesomeClass in one of my App. So I added the dependency in the app.
implementation "com.theapache64:my-awesome-library-1.0.0"
I also have some network call implementation and I don't want to use OkHttpClient from my-network-library. So I've created a module in the app to get my own instance of OkHttpClient.
@Module
@InstallIn(ApplicationComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
// CUSTOM CONFIG GOES HERE
.build()
}
}
Now am getting below error.
error: [Dagger/DuplicateBindings] okhttp3.OkHttpClient is bound multiple times:
I know it's because of the @Provides declared in the my-network-library, but I didn't specify includes to the @Module annotation to inherit dependency from BaseNetworkModule. The issue may be fixed using @Qualifier annotation, but IMO, that'd be a workaround.
so my question is
- Why dependency from a library module comes into the app module without using
includesof@Module? - How to tell Hilt "Do not look for @Provides in external libraries (gradle dependencies) ?" unless I mark the module with
@Module(includes = XXXModule)