Compile time error with Dagger Hilt Android: okhttp3.Interceptor cannot be provided without an @Provides-annotated method

huangapple go评论80阅读模式
英文:

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 = ...

huangapple
  • 本文由 发表于 2020年9月27日 23:03:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64089905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定