Guowei Lv

1 minute read

This is a simple one. No more explanation, just paste the code here.

@UiThread
@Module
class AppModule {

    @Provides
    @AppScope
    @Retrofit1
    fun retrofit1(urlProvider: UrlProvider): Retrofit {
        return Retrofit.Builder()
            .baseUrl(urlProvider.baseUrl1())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @AppScope
    @Retrofit2
    fun retrofit2(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(urlProvider().baseUrl2())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Provides
    @AppScope
    fun stackOverflowApi(@Retrofit1 retrofit: Retrofit): StackoverflowApi =
        retrofit.create(StackoverflowApi::class.java)

    @Provides
    @AppScope
    fun urlProvider(): UrlProvider = UrlProvider()
}
@Qualifier
annotation class Retrofit1()

@Qualifier
annotation class Retrofit2()
comments powered by Disqus