2

I know that it's possible to supply different parameters depending on the build type via buildConfigField, eg.:

// app.gradle

buildConfigField "boolean", "ADS_ENABLED", false
buildConfigField "String", "URL", "https://host.de"

Then I would be able to access these fields in the source code like this: BuildConfig.ADS_ENABLED. But what I need, is not to hard-code these values in the gradle file, but instead to supply them at build time via the gradlew command.

Something like this (this does not work, obviously):

./gradlew assembleQa -ADS_ENABLED=true, -URL="https://anotherhost.de"

and then to be able to access these fields from the source code.

The use case for this is automation, and specifically to have different jobs on the CI pipeline that can build an apk with different combinations of parameters, without having to create a new build type for each combination.

Other suggestions are welcomed.

VCODE
  • 535
  • 5
  • 19
  • You can use environment variables. – m02ph3u5 Jun 04 '20 at 10:17
  • 2
    I think [you can use `-P` or `-D`](https://stackoverflow.com/a/11696629/115145) to pass in arbitrary values (e.g., `./gradlew assembleQa -PADS_ENABLED=true, -PURL="https://anotherhost.de"`). Then, you would change your `buildConfigField` to reference those (e.g., `buildConfigField "boolean", "ADS_ENABLED", System.getProperty("ADS_ENABLED")`). – CommonsWare Jun 04 '20 at 11:42

1 Answers1

1

You can solve this by reading an environmental variable in your code and set it in the gitlab ci like this enter image description here

You can find the entire gitlab config from which the example was taken over here: https://gitlab.com/viae-modules/viae-modules/-/blob/master/.gitlab-ci.yml

VIAE IT
  • 103
  • 1
  • 7