How to Configure a Server URL inside an Android Client

When it comes to communication with a backend you often have different URLs for production and development.

One way to handle this for our Planets Android client is to generate different String resources. This can be done within your Gradle build file: build.gradle:

...
android {
  buildTypes {
    release {
      ...
      resValue 'string', 'serverUrl', '"https://planets.datenkollektiv.de/planets/"'
    }
    debug {
      resValue 'string', 'serverUrl', planetsUrl
    }
}
...

The local variable planetsUrl can be configured within any Gradle gradle.properties file.

planetsUrl = http://192.168.0.42:8080/planets/

This will generate an additional resource within generated.xml:

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

    <!-- Automatically generated file. DO NOT MODIFY -->

    <!-- Values from build type: debug -->
    <string name="serverUrl" translatable="false">http://192.168.0.42:8080/planets/</string>

</resources>

If you put this configuration into your global Gradle configuration you won't have a dirty workspace.