命名注射不工作

我有2次注射同一类的,我用它们来创建2个改造API客户端:命名注射不工作

改造实例:

@Provides 

@Singleton

@Named(NO_REAUTH_RETROFIT)

fun provideNoAuthRetrofit(moshi: Moshi, okHttpClientBuilder: OkHttpClient.Builder): Retrofit {

return Retrofit.Builder()

.addConverterFactory(MoshiConverterFactory.create(moshi))

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

.baseUrl(Constants.apiUrlLive)

.client(okHttpClientBuilder.build())

.build()

}

@Provides

@Singleton

@Named(REAUTH_RETROFIT)

fun provideAuthRetrofit(moshi: Moshi, okHttpClientBuilder: OkHttpClient.Builder, interceptor: NetworkInterceptor): Retrofit {

return Retrofit.Builder()

.addConverterFactory(MoshiConverterFactory.create(moshi))

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

.baseUrl(Constants.apiUrlLive)

.client(okHttpClientBuilder.addInterceptor(interceptor).build())

.build()

}

OkHttp:

@Singleton 

@Provides

fun provideOkHttpClientBuilder(context: Context, cache: Cache): OkHttpClient.Builder {

Stetho.initializeWithDefaults(context)

val httpLoggingInterceptor = HttpLoggingInterceptor()

httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY

val chuckInterceptor = ChuckInterceptor(context).showNotification(true)

return OkHttpClient.Builder()

.addInterceptor(chuckInterceptor)

.addInterceptor(httpLoggingInterceptor)

.addNetworkInterceptor(StethoInterceptor())

.cache(cache)

}

API实例:

@Provides 

@Singleton

fun provideCartApi(@Named(REAUTH_RETROFIT) retrofit: Retrofit): CartApi {

return retrofit.create(CartApi::class.java)

}

@Provides

@Singleton

fun provideAuthApi(@Named(NO_REAUTH_RETROFIT) retrofit: Retrofit): AuthApi {

return retrofit.create(AuthApi::class.java)

}

这两个API函数都接收NetworkInterceptor的改进实例。我试图切换2个改造实例的@Named值,但没有任何改变。只有当我从两个翻新实例中删除NetworkInterceptor时,才创建API实例,但不包含NetworkInterceptor

我还检查字节码,但@Named注释存在有

回答:

它看起来像这样的问题: https://github.com/google/dagger/issues/848

使用@field:命名

以上是 命名注射不工作 的全部内容, 来源链接: utcz.com/qa/264670.html

回到顶部