My Gradle build looks at an environment variable called BUILD_NUMBER to determine the version to allocate to my android application as follows:
def buildNumber = System.getenv("BUILD_NUMBER") ?: "local"
So as long as that environment variable is set, the build number is used in defaultConfig as follows:
versionName "1.4.0."+buildNumber
Usually, Jenkins will call this Gradle build and supply the BUILD_NUMBER environment variable.
If I run the Gradle build from my command prompt, I know I can set BUILD_NUMBER = x.
However, if I build using Android Studio, how can I set the BUILD_NUMBER environment variable through Android Studio itself?
You can set environment variables for Android Studio and the command-line tools that specify things like where the SDK is installed and where user-specific data is stored. This page describes the most commonly used environment variables.
In your gradle.properties create the environment variables (You can find this file inside of your Android Studio project or in your /Users/ Your Username /.gradle)
Android Studio configuration environment variables. The Android Studio configuration variables contain settings that customize the location of configuration files and the JDK. On start-up, Android Studio checks these variables for settings.
Follow the steps given below to setup ANDROID_HOME environment variable – 1. Click on Start menu. Then right click on Computer and select Properties option 2. This would open the System Properties window as shown below. Now from the Advanced Tab, click on Environment Variables button 3.
If the environment variable name looks like ORG_GRADLE_PROJECT _ prop =somevalue, then Gradle will set a prop property on your project object, with the value of somevalue. and in your CI you would name the environment variable ORG_GRADLE_PROJECT_BUILD_NUMBER, overwriting or setting BUILD_NUMBER in your CI build.
One option is to make use of gradle properties that can be overriden by environment variables. You can read about it here.
If the environment variable name looks like ORG_GRADLE_PROJECT_prop=somevalue, then Gradle will set a prop property on your project object, with the value of somevalue.
What this means is that you can
BUILD_NUMBER=42
in your .properties
file (project, or global) as you would usually do,ORG_GRADLE_PROJECT_BUILD_NUMBER
, overwriting or setting BUILD_NUMBER
in your CI build.Note: Use gradle.properties
in your project root directory, and do not modify local.properties
.
I ended up using the following in my build.gradle:
def buildNumber = System.getenv("BUILD_NUMBER") if (buildNumber == null) { Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) buildNumber = properties.getProperty('buildNumber')?:"NoBuildNumberFound" }
Because the local.properties file is not supposed to be committed to your source code repository, each developer manages their own copy.
So if they want to set the buildNumber on their local Android Studio, they simply add the following to their local.properties:
buildNumber=7
So on a local developers machine, the build number will be set to what ever they put into their local.properties file, but on our Jenkins server, it will use the environment variable BUILD_NUMBER
Our Jenkins server sets the BUILD_NUMBER environment variable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With