Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to use System environment variables in gradle using Android Studio

I am using Android Studio to build my project on an Ubuntu 14.04 system.

I wrote the following in my build.gradle files to avoid hardcoding storeFile, storePassword, keyAlias and keyPassword in my git repo:

signingConfigs {  debug {      storeFile file(System.getenv("KEYSTORE"))     storePassword System.getenv("KEYSTORE_PASSWORD")     keyAlias System.getenv("KEY_ALIAS")     keyPassword System.getenv("KEY_PASSWORD")          } 

But gradle sync errors out with the following: Error:(49, 0) Neither path nor baseDir may be null or empty string. path='null' basedir='./pathto/TMessagesProj'

My .bashrc contains: source ~/.gradlerc and my ~/.gradlerc contains the following:

export KEYSTORE="/home/myname/keystore/mykey" export KEYSTORE_PASSWORD='mypass' export KEY_ALIAS='mykey' export KEY_PASSWORD='keypass' 

I've confirmed that these variables are imported correctly by the shell. However I'm unsure of why it isnt received by the build environment in Android Studio.

What's the proper way to use environment variables in gradle?

like image 311
Joel G Mathew Avatar asked Aug 31 '14 04:08

Joel G Mathew


People also ask

How do I pass an environment variable in build Gradle?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle.

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.

What is system Getenv in Gradle?

getenv(String name) method gets the value of the specified environment variable. An environment variable is a system-dependent external named value. Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).

How do you declare a variable in Gradle?

Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language.


1 Answers

I also like having my keystore information on my environment variables, rather than having it inside the project. Your code seems fine, but I was having the same issue with the file path. I solved it by converting that value to string before passing it to file():

signingConfigs {  debug {     storeFile file(String.valueOf(System.getenv("KEYSTORE")))     storePassword System.getenv("KEYSTORE_PASSWORD")     keyAlias System.getenv("KEY_ALIAS")     keyPassword System.getenv("KEY_PASSWORD")          } 
like image 186
FMontano Avatar answered Sep 17 '22 11:09

FMontano