@mles Solution works but it relies on setting an environment variable. This is great for the CI usecase but if you also want to quickly build a dev/staging/release version locally, you should use build types or flavors instead. Check out the Configure build variants guide for further information.
The following example shows how to achieve this with build types, but using flavors is also very similar.
Configuring different API base URLs using gradle build types and source sets
Add an additional build type
The default gradle configuration should already contain a debug and release build type. Add an additional staging build type by adding this lines in your app module's build.gradle file inside the buildTypes:
staging {
initWith debug
}
Your build.gradle should then look like this:
android {
...
defaultConfig {
...
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
staging {
initWith debug
}
}
}
dependencies {
...
}
Execute a gradle sync afterwards (by pressing the "Sync Project with Gradle" button with the Elephant logo at the top).
Add additional source sets
You can tell gradle to package specific code and resources for specific build configurations. In our example, we will create an ExampleConfig file which contains the base URl, for each build type.
object ExampleConfig {
const val BASE_ULR = "http://example.com/"
}
In your code, you can just reference this file to access the base URL. Depending on the selected build type, gradle will then automatically use the correct version of the file.
For this, add the following folders inside your modules src folder (for example inside \app\src):
debug/java
staging/java
release/java
There, create the ExampleConfig class / object which contains a baseUrl String with different values. The result should look like this:
It should look like this:

Changing the CI job
In your CI, build the different versions by calling assemble with different configurations:
./gradlew assembleDebug
./gradlew assembleStaging
./gradlew assembleRelease
The final gitlab-ci configuration in your example should look like this:
image: jangrewe/gitlab-ci-android
stages:
- build
before_script:
- export GRADLE_USER_HOME=$(pwd)/.gradle
- chmod +x ./gradlew
cache:
key: ${CI_PROJECT_ID}
paths:
- .gradle/
build:
stage: build
tags:
- dev-ci
script:
- ./gradlew assembleDebug assembleStaging assembleRelease
artifacts:
paths:
- app/build/outputs/