Android: Using two or more Base URLs with Kotlin and Retrofit

Lucas Ferreira
4 min readNov 6, 2020

--

I recently developed a project with two different endpoints in the same application. I know that’s often, mainly in holding’s projects, but this is the first time that I’ve come across this. Therefore, I’ve decided to bring this here, due to think this must be a trivial knowledge. I’ve found few material about this topic and it’s simpler than it seems.

Firstly, I would like to say that I’m using Koin, which is a smart library from Kotlin and, with few annotations, it makes dependency injection easier. But calm down! It’s not necessary to know Koin or something about dependency injection to understand what I’ll show here. The code below just shows the Retrofit initializer. Oh, please ignore words like Credentials, Simulator… you don’t need to understand this, ’cause it’s just words that I use in my app context.

Retrofit initializer

That’s my Modules.kt file. I initialize Retrofit in this file ’cause I’m using Koin dependency injection, but you can initialize it where you want.

As you can see, my baseUrl is in a separate object (Endpoints) which I’ve created just to separate responsibilities between classes.

The two endpoints

The code above shows my Endpoints.kt object, where I’ve created the constants for the two Base URLS. That’s it.

Argument being passed directly to @GET annotation

The code above shows my CredentialsService.kt interface, where @GET method is in, responsible for make the request, which we’ll see more forward.
Commonly, as shown in the example above, when we use only one Base URL in the project, we pass the complement of the Base URL in the @GET annotation, forming the endpoint. For example, if my endpoint were https://google.com/images, then I would just pass “images” string as argument to my @GET annotation. This would concatenate both strings and form the endpoint, but that’s useless for us.

@Url annotation, the big secret

The code above shows the CredentialsService.kt interface again, however, now there’s the secret to use two base URLs in the same project:
the @Url annotation.
Different from the example which I've passed the URL as an argument directly to @GET method, now @Url annotation has this responsibility, which is the parameter string I called url.

Repository’s class

Lastly, that’s the repository’s class, where we do the API request.
As I said before, you don’t need to understand how I’m making this request, mainly if you don’t know Coroutines, Clean Arch and Koin. So let’s focus on our goal here:
The variable credentialsService is creating our service in Retrofit and then calling getCredentials method, from the own service. Do you remember this method asks for a parameter from @Url annotation? This parameter is the complete URL of the endpoint. In this case, I’m passing CREDENTIALS_BASE_URL + the “images” string, which forms this endpoint output: https://google.com/images. And that’s it.

If you go back on my Modules.kt file, you’ll see that the Base URL I put in Retrofit is SIMULATOR_BASE_URL, and the service that I’ve shown above has a different URL, which is CREDENTIALS_BASE_URL. It happens ’cause no matter which URL you’ve pointed out as Base URL in Retrofit initializer, if you use the annotation @Url in the service interface and pass a different URL, like I did above, Retrofit will know it and consider the correct endpoint.
Just for exemplify the dynamism of it: thinking we have two repositories, which one is that I’ve presented above as RetrofitCredentialsRepository and the other RetrofitSimulatorRepository, if we use @Url annotation in the SIMULATOR’s service interface, we could do the same procedure for a different service, as shown in the examples below:

SimulatorService.kt
RetrofitSimulatorRepository.kt

Then the output is https://youtube.com/videos.

That’s all, folks. If you have any doubt, please feel free to ask in the comments. I’ll answer as soon as possible.

--

--

Lucas Ferreira
Lucas Ferreira

No responses yet