英文:
Compile time error with Dagger Hilt Android: okhttp3.Interceptor cannot be provided without an @Provides-annotated method
问题
我正在学习 Dagger Hilt,没有学习 Dagger2,遇到了一个错误,我找不到解决办法。
我有两个方法 provideLoggingInterceptor()
和 provideHeaderInterceptor()
,它们返回相同的 Interceptor
对象,所以我添加了 @Named()
限定符来标识这些提供者。provideOkHttpClient()
方法需要这两个 Interceptor
,但我在编译时得到了错误:
> 错误:[Dagger/MissingBinding] 无法提供 okhttp3.Interceptor,
> 除非有一个带有 @Provides 注解的方法。
现在我很困惑,因为如果 provideLoggingInterceptor()
和 provideHeaderInterceptor()
方法都带有 @Provides
注解,为什么会报错说我的某个方法没有 @Provides
注解。
@InstallIn(ApplicationComponent::class)
@Module
class NetworkModule {
val TIMEOUT = 10
@Singleton
@Provides
@Named("logging")
fun provideLoggingInterceptor(): Interceptor =
HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY
else HttpLoggingInterceptor.Level.NONE
}
@Singleton
@Provides
@Named("header")
fun provideHeaderInterceptor(): Interceptor =
Interceptor { chain ->
val request = chain.request()
val newUrl = request.url.newBuilder()
.addQueryParameter("api_key", BuildConfig.TMBD_API_KEY)
.build()
val newRequest = request.newBuilder()
.url(newUrl)
.method(request.method, request.body)
.build()
chain.proceed(newRequest)
}
@Singleton
@Provides
fun provideOkHttpClient(
logging: Interceptor,
header: Interceptor
): OkHttpClient =
OkHttpClient.Builder()
.connectTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
.readTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
.addInterceptor(header)
.addInterceptor(logging)
.addNetworkInterceptor(StethoInterceptor())
.build()
}
英文:
I am learning dagger hilt without learning dagger2 and came up with an error which I can't find a solution.
I have two methods provideLoggingInterceptor()
and provideHeaderInterceptor()
which returns the same Interceptor
object so I added a @Named()
qualifier to identify the providers. The provideOkHttpClient()
methods needs both the Interceptor
but I am getting a compile time error as
> error: [Dagger/MissingBinding] okhttp3.Interceptor cannot be provided
> without an @Provides-annotated method.
Now I am confused because if both the methods provideLoggingInterceptor()
and provideHeaderInterceptor()
are annotated with the @Provides
annotation then why am I getting error that one of my methods does not have @Provides
annotation.
@InstallIn(ApplicationComponent::class)
@Module
class NetworkModule {
val TIMEOUT = 10
@Singleton
@Provides
@Named("logging")
fun provideLoggingInterceptor(): Interceptor =
HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY
else HttpLoggingInterceptor.Level.NONE
}
@Singleton
@Provides
@Named("header")
fun provideHeaderInterceptor(): Interceptor =
Interceptor { chain ->
val request = chain.request()
val newUrl = request.url.newBuilder()
.addQueryParameter("api_key", BuildConfig.TMBD_API_KEY)
.build()
val newRequest = request.newBuilder()
.url(newUrl)
.method(request.method, request.body)
.build()
chain.proceed(newRequest)
}
@Singleton
@Provides
fun provideOkHttpClient(
logging: Interceptor,
header: Interceptor
): OkHttpClient =
OkHttpClient.Builder()
.connectTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
.readTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
.addInterceptor(header)
.addInterceptor(logging)
.addNetworkInterceptor(StethoInterceptor())
.build()
}
答案1
得分: 4
必须还要使用 Named
注解来为参数命名:
fun provideOkHttpClient(
@Named("logging") logging: Interceptor,
@Named("header") header: Interceptor
): OkHttpClient = ...
英文:
You must also use Named
annotation for arguments
fun provideOkHttpClient(
@Named("logging") logging: Interceptor,
@Named("header") header: Interceptor
): OkHttpClient = ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论