ipohfly
ipohfly

Reputation: 2009

How to externalise the Base URL in Retrofit for Android

We have an Android app now that are working fine using Retrofit. We are trying to automate the build for testing and production release, to reduce the chance of human error (i.e. dev forget to point the Base URL to production when build for production..etc)

I noticed that the Base URL for Retrofit are defined in the class file. Is there any method that we can use to configure it to be in a properties file or xml so that we can use the Gradle script to pick the right file to be included with the build?

Thank you

Upvotes: 17

Views: 18054

Answers (6)

Magdy Mohmed
Magdy Mohmed

Reputation: 23

I agree with @jeprubio using the gradle and then

.baseUrl(BuildConfig.API_URL)


buildTypes {
    debug {
        buildConfigField "String", "API_URL", "\"https://url/pre/\""
    }
    release {
        buildConfigField "String", "API_URL", "\"https://url/pro/\""
    }
}

Upvotes: 2

Gk Mohammad Emon
Gk Mohammad Emon

Reputation: 6938

I think to do this kind of task by Gradle is the best choice because it automates the build process easily.But I'm sharing the process to do it by build.gradle(:app) with the image because we need to pay extra attention to java's escape sequence while adding API URL in buildTypes otherwise the URL won't generate properly in BuildConfig and produce Cannot resolve ... error like the following images -

enter image description here

So the proper solution is that add URL obeying escape sequencing like the following photo-

enter image description here

Upvotes: 2

Harsh Maheshawari
Harsh Maheshawari

Reputation: 1

Well I was just stuck on this issue yesterday didn't find anything that can solve my issue so that this is how i made it work:

create a public class anytime you want BASEURL or anything and in it create a public static string name anything (BASE)put the base URL in it.

now Access that class from your retrofit class and choose that static element(in baseURL method) something like BASEURL.BASE;

why i need to do there was some issue like retrofit .baseurl methods want a static string only and when we access a string from strings.xml save it in static string it give some error so this was another way i came to solved it works for well will work for you.

Upvotes: 0

Adib Faramarzi
Adib Faramarzi

Reputation: 4054

Aside from Gradle build type configuration, If you have more logic than a few field (or implementations to be exact), you can define classes/files to exist only in a flavor/build-type by placing it in the right folder.

For example, you can create a config file (Java/Kotlin) and instead of placing it in the main/java/example/package/config.java folder of the module, place it inside debug/java/example/package/config.java and release/java/example/package/config.java with the same package name to differentiate its implementation for those build types.

For your example, this file can include a static field BASE_URL with different values on different build types/flavors.

This can be done exactly the same way for flavors too.

Upvotes: 0

Robert LaFondue
Robert LaFondue

Reputation: 191

You can create a file like "urls.xml" and get your url as a string.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="base_url">https://www.myurl.com</string>
</resources>

This way you can configure different url depending of your flavor and build type.

Upvotes: 0

jeprubio
jeprubio

Reputation: 18002

You can define a constant in your buildTypes:

buildTypes {
    debug {
        buildConfigField "String", "API_URL", "\"https://url/pre/\""
    }
    release {
        buildConfigField "String", "API_URL", "\"https://url/pro/\""
    }
}

And then use it in the retrofit builder:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(BuildConfig.API_URL)
    .build();

Notice that if you have different modules the BuildConfig is defined in each of them. For this cases I define the buildTypes in the app module and pass the BuildConfig.API_URL as parameter in the call to the module with the retrofit builder.

Upvotes: 30

Related Questions