I am able to create the single build(apk) like debug.apk
using the Gitlab-CI by below approach in Gitlab.
Inside my .gitlab-ci.yml
, I have done this entry.Please check it once,
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
artifacts:
paths:
- app/build/outputs/
And I have created the docker
image and got the build (apk) on each push on the Gitlab.
My question is that can we set the different stages like Development
, Staging
and Production
which point out different BASE_URL
of the application. I have also searched out in the documents but did not get the solution.Please help me on it. Thanks
@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.
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).
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:
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/