How do you configure debug builds to point to different apis?

I have two base urls for my api, one for prod & one for debug. I was wondering if there was a way I can use Android Studio's Gradle build types to grab the right api.

So basically, I want to move away from

String url;
if(isDebug){
   url = getDebugApiURL();
}
else{
    url = getReleaseApiURL();

to just have the app return the right api url. Is there a way I can do this? If so how should I go about configuring it?

Answers


You can use the buildConfigField method inside your buildTypes.

For example:

android {
    buildTypes {
        release {
             buildConfigField "String", "API_URL", "\"api.example.com\""
        }

        debug {
             buildConfigField "String", "API_URL", "\"debug-api.example.com\""
        }
    }
}

You then use this field in your code via BuildConfig.API_URL. The build tools plugin already define a number of BuildConfig fields for you, such as BuildConfig.DEBUG, and with Gradle you can add your own to the mix!

Documentation is located here.


Need Your Help

Quick way creating counts for keys in a table

python pandas ipython

I have a pandas series of word frequencies for a certain word across multiple extracts of text e.g. 10, 5, 10, 5, 3, 3, 2, 1... and would like to produce a 2-column table where I would like the word

Logic game: maximising (or minimising) the chances for two agents to meet

python algorithm optimization language-agnostic logic

Note: this question is tagged both language-agnostic and python as my primary concern is finding out the algorithm to implement the solution to the problem, but information on how to implement it