Kotlin+Dagger2+Retrofit+Named

250 Views Asked by At

Im trying to build two different Retrofit objects pointing to different base urls using Kotlin and Dagger 2, like this-

object Constant {
const val BASE_URL_1 = "https://baseurl1.xyz/"
const val BASE_URL_2 = "https://baseurl2.xyz/"
}


@Provides
@ApplicationScope
@Named(BASE_URL_1)
internal fun retrofit(okHttpClient: OkHttpClient, gson: Gson): Retrofit {
    return Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(BASE_URL_1)
            .client(okHttpClient)
            .build()
}

@Provides
@ApplicationScope
@Named(BASE_URL_2)
internal fun retrofit(okHttpClient: OkHttpClient, gson: Gson): Retrofit {
    return Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(BASE_URL_2)
            .client(okHttpClient)
            .build()
}

And then I create the retrofit interfaces-

@Provides
@ApplicationScope
fun api_1(@Named(BASE_URL_1) retrofit: Retrofit): Api_1 {
    return retrofit.create<Api_1>(Api_1::class.java)
}

@Provides
@ApplicationScope
fun api_2(@Named(BASE_URL_2) retrofit: Retrofit): Api_2 {
    return retrofit.create<Api_2>(Api_2::class.java)
}

Stacktrace -https://gist.github.com/tejasna/18a50cc803116adb48300b7a6d6c98b2

Screenshot compiler error -

But im getting a conflicting overload compiler exception.

Advice please? Thanks in advance.

0

There are 0 best solutions below