46

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?

Milad Nouri
  • 1,587
  • 1
  • 21
  • 32
Joel G Mathew
  • 7,561
  • 15
  • 54
  • 86
  • Have a look at this post: http://stackoverflow.com/questions/21173826/environment-variable-in-settings-gradle-not-working-with-android-studio – Opal Sep 01 '14 at 09:42
  • @joel-g-mathew , please accept the answer you found applicable for your use-case. – HX_unbanned Apr 10 '21 at 16:21

2 Answers2

49

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")        
 }
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
FMontano
  • 907
  • 8
  • 17
  • You rock! This is the easiest solution I've found to the environment var issue in build.gradle and using android studio – spartygw Feb 22 '16 at 19:22
  • 1
    After adding the fix mentioned above, now I have Android studio complaining the debug apk is not signed. "Error : The apk for your current selected variant (..apk) is not signed. Please specify a signing configuration for this variant). It takes me to the run/debug configuration window. The build error is gone, but now, I cannot run the app from Android studio coz of this error. – Sayooj Valsan Jun 13 '16 at 18:53
  • @SayoojValsan you probably don't necessarily want/need this for the `debug` signing config that the example shows, Gradle usually comes with the correct default debug keystore configured, you should swap `debug` in the example with `release` and then you only need to supply the environment variables when you are building a Release to go to the Play Store. – dragon788 Mar 31 '21 at 16:02
  • This worked great locally, but we ran into some issues in a CI pipeline where Gradle was trying to resolve the absolute path as a relative one, we never fully solved it, but using something like `storeFilePath String.valueOf(System.getenv("KEYSTORE")); storeFile file(storeFilePath).absolutePath` may have solved it. – dragon788 Mar 31 '21 at 16:05
  • Great answer, works perfect! But can someone figure out why we need String.valueOf()? Method getenv() returns Strung. – Victor Pozdnyakov May 02 '21 at 09:55
  • @FMontano I am receiving null. I have checked in terminal I have defined all environment variables correctly. Please help. – Kartik Watwani Sep 08 '21 at 17:20
  • you don't have to store your credentials in the project. All you need to do is put the credentials in the ~/.gradle home directory. This is the proper solution, not trashing your project with gradle logic... – Lukasz Ochmanski Feb 07 '22 at 20:19
21

Create a gradle.properties file in your source folder (alongside build.gradle) to apply only to the current project or in ~/.gradle/gradle.properties to apply system-wide with the contents:

keystore=/home/myname/keystore/mykey
keystore_password=mypass
key_alias=mykey
key_password=keypass

Now update your build.gradle file with:

debug {
  storeFile file("${keystore}")
  storePassword "${keystore_password}"
  keyAlias "${key_alias}"
  keyPassword "${key_password}"
}

Optionally, you could pass the parameters from command-line with the -P option. For example, ./gradlew assemble -Pkey_password=keypass.

Kevin Brotcke
  • 3,765
  • 26
  • 34