Christopher ElĂas
10/02/2020, 1:42 PMtorresmi
10/02/2020, 2:18 PMChristopher ElĂas
10/02/2020, 2:24 PMtorresmi
10/02/2020, 2:47 PMcreateNetworkClient
and then manages that instance in the main app module. This might be from a scoped dagger module, Singleton, etc. Then when you build the dependencies for the feature module, you can use that instance to create the services needed for the code in the feature module.
You would still have code isolated for modules, but the retrofit instance lives at a higher level module to help build the pieces.
Does that help a litte more?Casey Brooks
10/02/2020, 2:56 PMRetrofit
instance per module, but honestly it probably isn’t noticeable or something to really stress yourself about. What’s more important is how you structure your modules to be clean and understandable.
Generally, you’ll want to use one Retrofit instance for each “backend” you’re calling to. Many apps will only have one “backend”, and so you’d only need a single Retrofit instance. Sometimes you might have different backends, for example a normal API backend at https://api.foo.com, a secure backend for processing payments at https://secure.foo.com, and promotional content hosted in a public CMS at https://foo.com. In that example, each backend will likely have different needs in terms of authorization, headers, etc. and so you’d need to configure each Okhttp/Retrofit separately to account for it, but you can safely use that one instance for multiple modules.
I’ve been working on an app for a couple years using this approach and it works very well. We use Dagger to inject these Retrofit instances, but that’s not necessary. Here’s our general approach:
// create a base OkHttp client with settings shared by all backends. Is a factory (not a Singleton)
fun createOkHttpBuilder(): OkHttpClient.Builder
// create a base Retrofit client with settings shared by all backends
fun createRetrofitBuilder(okHttpClient: OkHttpClient): Retrofit.Builder. Is a factory (not a Singleton)
// Create shared Retrofit instances shared for all APIs at a single backend. Can be a Singleton, but not necessary. Tradeoff of memory vs startup speed (both of which are likely negligible. My team prefers to keep it a factory, not a Singleton)
fun createApiRetrofit(): Retrofit {
val okHttp = createOkHttpBuilder()
.let {
// API-specific OkHttp configuration
}
.build()
val retrofit = createRetrofitBuilder(okHttp)
.let {
// API-specific Retrofit configuration
}
.build()
return retrofit
}
fun createSecureRetrofit(): Retrofit {
val okHttp = createOkHttpBuilder()
.let {
// secure-specific OkHttp configuration
}
.build()
val retrofit = createRetrofitBuilder(okHttp)
.let {
// secure-specific Retrofit configuration
}
.build()
return retrofit
}
fun createCmsRetrofit(): Retrofit {
val okHttp = createOkHttpBuilder()
.let {
// secure-specific OkHttp configuration
}
.build()
val retrofit = createRetrofitBuilder(okHttp)
.let {
// secure-specific Retrofit configuration
}
.build()
return retrofit
}
fun createLoginApi(): LoginApi {
return createApiRetrofit().create(LoginApi::class.java);
}
fun createPaymentsApi(): PaymentsApi {
return createSecureRetrofit().create(PaymentsApi::class.java);
}
fun createBlogApi(): BlogApi {
return createCmsRetrofit().create(BlogApi::class.java);
}
Isaac
10/02/2020, 5:55 PMbuild.gradle
Isaac
10/02/2020, 5:55 PMChristopher ElĂas
10/02/2020, 6:12 PM@Module
object RetrofitModule {
@Provides
@Singleton
internal fun provideRetrofitBuilder(): Retrofit =
createNetworkClient(BuildConfig.BASE_URL, BuildConfig.DEBUG)
.build()
}
features/login(android library)
@Module
object LoginServiceModule {
@Provides
@PerFeatureScope
internal fun provideLoginService(retrofit: Retrofit): LoginService =
retrofit.create(LoginService::class.java)
}
I have a more clear concept now that @Casey Brooks explain that he uses a different retrofit instance if he have different BackEnds, that is not may case on this project, but what Im trying to do is that each feature module (Android Library) can have it's own service for more isolation. So in the above example I just remove the creation of the retrofit builder, probably this is a lil bit better, what do you guys think or I misunderstood your words?Casey Brooks
10/02/2020, 6:14 PMChristopher ElĂas
10/02/2020, 6:14 PM