Bitrise Android Environment Variables
Developers security best practices rule number one is: don’t add API tokens into GitHub. Inject the API tokens during CI (Continuous Integration) process to secure the tokens. Tutorial shows how to pass Bitrise environment variables as arguments to build.gradle.
This is part of a series of tutorials for building Android Continuous Integration and Continuous Delivery (CI/CD) with Bitrise.
We have three options to pass env vars to android app (build.gradle)
- From Bitrise settings tab Env Vars. (Bitrise UI)
- Bash script. (we create env vars using code)
- From Bitrise settings tab Secrets. (Bitrise UI)
Bitrise Env Vars
- Go to Bitrise App and select -> Workflow Editor-> $Env Vars -> add a new environment name, example: TEST_ENV
- Add the environment variable on build.gradle. Copy and paste below code to your build.gradle
String testEnv = System.getenv("TEST_ENV") as String ?: "testEnv"// Access variable from Java classes
buildConfigField STRING, "TEST_ENV", "\"$testEnv\""}
In Java class call:
public static final String testEnv = BuildConfig.TEST_ENV;
3. Now when you build at runtime Bitrise will pass env var (TEST_ENV) to build.gradle and Java class.
To add Environment Variables from bash script. We need to use Bitrise step “Do anything with Script step”.
- Add step “Do anything with Script”:
#generates date in mmddyy format
# DATE=`date +"%m%d%y"`
BITRISE_DATE=$DATE#creates date environment variable
envman add --key BUILD_START_DATE --value $BITRISE_DATEecho BUILD_START_DATE
3. Now when you build at runtime Bitrise will pass environment variable “BUILD_START_DATE” to build.gradle.
Add private Tokens to Bitrise as secret environment variables, we can use the feature “Secrets”. Bitrise keeps your “secrets” secured and encrypted.
Thanks for reading! Let me know if you have any questions.