Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an environment variable in Android Studio before it runs my gradle build

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?

like image 591
dleerob Avatar asked Jan 22 '16 07:01

dleerob


People also ask

Do we need to set environment variables for Android Studio?

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.

How to create environment variables in Gradle?

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)

What are environment variables in Android Studio?

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.

How to setup Android_home environment variable in Windows 10?

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.

How do I set the prop value in Gradle?

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.


2 Answers

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

  1. set BUILD_NUMBER=42 in your .properties file (project, or global) as you would usually do,
  2. and in your CI you would name the environment variable 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.

like image 183
David Medenjak Avatar answered Sep 22 '22 17:09

David Medenjak


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

like image 22
dleerob Avatar answered Sep 23 '22 17:09

dleerob