首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用dagger Handle作为依赖注入来处理多个改装客户端?

使用dagger Handle作为依赖注入来处理多个改装客户端?
EN

Stack Overflow用户
提问于 2021-08-05 12:23:18
回答 1查看 1.7K关注 0票数 4

我想在我的android应用程序中使用两个不同的后端与不同的响应格式,我正在使用句柄作为依赖注入与网络调用的改进,这是很好的工作。

因为我已经添加了我的第二个服务器网络文件和应用程序模块,它给我的错误在末尾列出。

在这种情况下,我需要知道如何在不做任何显著的架构更改的情况下解决问题。

代码语言:javascript
运行
复制
@Keep
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Singleton
@Provides
fun provideRetrofit(gson: Gson,@ApplicationContext appContext: Context): Retrofit = Retrofit.Builder()
    .client(
        OkHttpClient().newBuilder()
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).readTimeout(80,TimeUnit.SECONDS)
            .addInterceptor(
                ChuckerInterceptor.Builder(appContext)
                    .collector(ChuckerCollector(appContext))
                    .maxContentLength(250000L)
                    .redactHeaders(emptySet())
                    .alwaysReadResponseBody(false)
                    .build()
            )
            .build()
    )
    .baseUrl(UtilSingleton.instance!!.GetBaseUrl())
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build()

@Provides
fun provideGson(): Gson = GsonBuilder().create()

@Provides
fun providePostsService(retrofit: Retrofit): ApiService =
    retrofit.create(ApiService::class.java)

@Singleton
@Provides
fun provideApiRemoteDataSource(apiService: ApiService) = ApiRemoteDataSource(apiService)

@Singleton
@Provides
fun provideRepository(
    remoteDataSource: ApiRemoteDataSource
) =
    MainRepo(remoteDataSource)


/**----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------**/

@Singleton
@Provides
fun provideRetrofitEmall(gson: Gson,@ApplicationContext appContext: Context): Retrofit = Retrofit.Builder()
        .client(
                OkHttpClient().newBuilder()
                        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).readTimeout(80,TimeUnit.SECONDS)
                        .addInterceptor(
                                ChuckerInterceptor.Builder(appContext)
                                        .collector(ChuckerCollector(appContext))
                                        .maxContentLength(250000L)
                                        .redactHeaders(emptySet())
                                        .alwaysReadResponseBody(false)
                                        .build()
                        )
                        .build()
        )
        .baseUrl(UtilSingleton.instance!!.GetBaseUrl())
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build()

@Provides
fun providePostsServiceEmall(retrofit: Retrofit): EmallApiService =
        retrofit.create(EmallApiService::class.java)

@Singleton
@Provides
fun provideApiRemoteDataSource(apiService: EmallApiService) = EmallApiRemoteDataSource(apiService)

@Singleton
@Provides
fun provideRepository(
        remoteDataSource: EmallApiRemoteDataSource
) =
        EmallRepo(remoteDataSource)

}

生成错误->

代码语言:javascript
运行
复制
Execution failed for task ':app:kaptLocalDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)

完整日志->

代码语言:javascript
运行
复制
 D:\vaultsNew\vaultspaynewapis\app\build\tmp\kapt3\stubs\localDebug\com\uae\myvaultspay\di\AppModule.java:40: error: Cannot have more than one binding method with the same name in a single module
public final com.uae.myvaultspay.data.remote.ApiRemoteDataSource provideApiRemoteDataSource(@org.jetbrains.annotations.NotNull()
                                                                 ^D:\vaultsNew\vaultspaynewapis\app\build\tmp\kapt3\stubs\localDebug\com\uae\myvaultspay\di\AppModule.java:78: error: Cannot have more than one binding method with the same name in a single module
public final com.uae.myvaultspay.data.remote.emallremote.EmallApiRemoteDataSource provideApiRemoteDataSource(@org.jetbrains.annotations.NotNull()
                                                                                  ^D:\vaultsNew\vaultspaynewapis\app\build\tmp\kapt3\stubs\localDebug\com\uae\myvaultspay\di\AppModule.java:48: error: Cannot have more than one binding method with the same name in a single module
public final com.uae.myvaultspay.data.repository.MainRepo provideRepository(@org.jetbrains.annotations.NotNull()
                                                          ^D:\vaultsNew\vaultspaynewapis\app\build\tmp\kapt3\stubs\localDebug\com\uae\myvaultspay\di\AppModule.java:86: error: Cannot have more than one binding method with the same name in a single module
public final com.uae.myvaultspay.data.repository.EmallRepo provideRepository(@org.jetbrains.annotations.NotNull()
                                                           ^warning: 
File for type 'com.uae.myvaultspay.MyApplication_HiltComponents' created 
in the last round will not be subject to annotation processing.
Execution failed for task ':app:kaptLocalDebugKotlin'.
> A failure occurred while executing 
org.jetbrains.kotlin.gradle.internal.KaptExecution
> java.lang.reflect.InvocationTargetException (no error message)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-05 12:50:46

如果您想使用Dagger或Hilt返回Retrofit client,那么您应该使用@Named注释。此注释帮助Hilt或Dagger理解具有相同Retrofit返回类型的您,您需要获取哪个Retrofit实例。

按照下面的方法,您可以提供多个带柄或短剑的改装实例。

首先,我在这里看到您提供了2个改造实例。为您提供的每个改造实例放置@Named注释。

代码语言:javascript
运行
复制
object AppModule {

   @Singleton
   @Provides
   @Named("Normal")
   fun provideRetrofit(gson: Gson, @ApplicationContext appContext: Context): Retrofit = ...

   @Singleton
   @Provides
   @Named("Email")
   fun provideRetrofitEmall(gson: Gson, @ApplicationContext appContext: Context): Retrofit = ...
}

接下来,当您提供api服务时,向Hilt或Dagger指定它需要哪个改装实例。

代码语言:javascript
运行
复制
object AppModule {

   @Provides
   fun providePostsService(@Named("Normal") retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java)

   @Provides
   fun providePostsServiceEmall(@Named("Email") retrofit: Retrofit): EmallApiService = retrofit.create(EmallApiService::class.java)
}

最后,清理项目并重新构建项目以查看结果。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68666532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档