我想使用 Dagger Hilt 设置两个 Retrofit2 客户端,因为我的应用程序从两个不同的 api 获取信息。但是当我运行我的代码时,抛出以下异常 ->
App_HiltComponents.java:128: error: [Dagger/DuplicateBindings] retrofit2.Retrofit is bound multiple times:
public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint,
^
@org.jetbrains.annotations.NotNull @Provides retrofit2.Retrofit de.kotlinbooks.di.NetworkBooksModule.retrofitBooks(okhttp3.OkHttpClient)
@org.jetbrains.annotations.NotNull @Provides retrofit2.Retrofit de.kotlinbooks.di.NetworkNYTModule.retrofitNYT(okhttp3.OkHttpClient)
Run Code Online (Sandbox Code Playgroud)
我知道改造客户端是一个单例,所以我将模块分成两个文件以用于不同的 api。这是我的代码->
网络图书模块
@Module
@InstallIn(SingletonComponent::class)
class NetworkBooksModule {
@Provides
fun okHttpClient(): OkHttpClient {
val levelType: HttpLoggingInterceptor.Level = if (BuildConfig.DEBUG)
HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
val logging = HttpLoggingInterceptor()
logging.setLevel(levelType)
return OkHttpClient.Builder()
.addInterceptor(logging)
.build()
}
@Provides
fun retrofitBooks(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create())
.build()
}
@Provides
fun …Run Code Online (Sandbox Code Playgroud)