Can't create Set<Interceptor> using Multibindings
See original GitHub issueHi!
I trying to use multibinding to provide OkHttp Interceptors for the client.
@Provides
@Singleton
fun provideOkHttpClient(interceptors: Set<Interceptor>): OkHttpClient {
val builder = OkHttpClient.Builder()
for (interceptor in interceptors) {
builder.addNetworkInterceptor(interceptor)
}
return builder.build()
}
@Provides @IntoSet
@Singleton
fun provideLoggingInterceptor(): Interceptor =
HttpLoggingInterceptor({ message -> Timber.tag("OkHttp").d(message) })
but can’t compile. There is an error:
error: java.util.Set<? extends okhttp3.Interceptor> cannot be provided without an @Provides-annotated method.
When I provide strings in the same way all works. But not with the Interceptor.
What I use: Kotlin 1.1.1 (kapt{generateStubs = true}) , Dagger 2.10
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:12
Top Results From Across the Web
Multibindings - Dagger
Dagger allows you to bind several objects into a collection even when the objects are bound in different modules using multibindings. Dagger assembles...
Read more >Dagger2 is hard, but it can be easy, part 7 - FunkyMuse
In this post we'll learn about multi bindings and their power. ... The ViewModel can't be created because Dagger doesn't know how to...
Read more >Guice Multibindings: Manually obtain a Set<T> from Injector
I use this snippet to create a TypeLiteral<Set<T>> : import com.google.inject.util.Types; @SuppressWarnings("unchecked") public static <T> ...
Read more >OkHttp Interceptor Multibindings with Dagger - velog
OkHttp Interceptor Multibindings with Dagger ... create(BaseProvidesModule module, Provider<Set<Interceptor>> interceptorsProvider) { return ...
Read more >javax.inject.Singleton Scala Example - ProgramCreek.com
InternalServerError("Can't show Trace.")) } } Example 11 ... FORBIDDEN).build) } } def setInterceptor(in: TokenAuthorizingInterceptor) = interceptor = in }.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Dagger expects a
java.util.Set<okhttp3.Interceptor>
, not? extends okhttp3.Interceptor
. The problem is that you’re using Kotlin and Kotlin will by default translate aSet<X>
into aSet<? extends X>
: http://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#variant-genericsApparently (according to the docs), you should be able to make it work with
Set<@JvmSuppressWildcards okhttp3.Interceptor>
.Wrt
Set<String>
, it works becauseString
isfinal
, so Kotlin emits aSet<String>
asSet<? extends String>
would have no sense. See this note in the docs:This keeps coming up. I wonder if we should try to inspect the classpath for kotlin classes (if that’s possible?) and present an error when we see this