1

I am trying to pass environment specific values to Android studio v 2.2.2 (such as server url, keystore location and password) by setting java vm arguments. So far I've tried following options but nothing has worked.

1. Setting the java-vm arguments from studio.vmoptions (as documented here)

I clicked Help -> Edit Custom VM Options... to create a new vmoptions file which is created at ~/.AndroidStudio2.2/studio64.vmoptions. I have edited the file to add following line.

-DRELEASE_KEY_STORE_PATH=/home/ubuntu/ks/myapp/app-release.keystore

2. With a gradle.properties file

I have created a gradle.properties file in the same directory where the root build.gradle file exists. The file has following contents.

RELEASE_KEY_STORE_PATH=/home/ubuntu/ks/myapp/app-release.keystore

3. Edited the studio.sh file

I have also tried editing the studio.sh file, added following line hoping these properties will be available to Android Studio.

export RELEASE_KEY_STORE_PATH=/home/ubuntu/ks/myapp/app-release.keystore

My build.gradle file has signingConfig section where this value is used.

signingConfigs {
        release {
            storeFile file(System.env.RELEASE_KEY_STORE_PATH)
            storePassword System.env.RELEASE_KEYS_STORE_PASSWORD
            keyAlias System.env.RELEASE_KEY_ALIAS
            keyPassword System.env.RELEASE_KEY_PASSWORD
        }
    }

The build fails when gradle project sync / build runs from Android Studio.

The error is displayed: Neither path nor baseDir may be null or empty string. path='null' basedir='/home/ubuntu/path-to-project'. This means that script is not able to resolve the file path as it is coming null.

UPDATE If I run the gradle build from command line it perfectly works, build succeeds, all I have to do is just fire some export commands for the desired properties I want to inherit in build.

Following is my dev environment:

  • Gradle version used by Android Studio: 2.14.1
  • Gradle plugin version: 2.3.1
  • Android Studio version: 2.2.2
  • Java version: oracle jdk 1.8.0_111
  • OS: Ubuntu 16.04 amd64
Pawan
  • 1,183
  • 16
  • 29
  • 1
    When you tried the Gradle option, did you do `System.env.RELEASE_KEY_STORE_PATH` or just `RELEASE_KEY_STORE_PATH`? It should be the second one since it's not an environment variable. – DeeV Dec 02 '16 at 18:33
  • What do you mean by the maven option? setting props in the gradle.properties file? – Pawan Dec 02 '16 at 18:35
  • Sorry. I meant Gradle option (#2). – DeeV Dec 02 '16 at 18:36
  • Oh! I am using `System.env.RELEASE_KEY_STORE_PATH` in build script. If I use just `RELEASE_KEY_STORE_PATH` it works in Android Studio but now the build fails in command line. – Pawan Dec 02 '16 at 18:43
  • 2
    Gradle does not have access to environment variables when launched from Android Studio. Gradle does have access to environment variables when run elsewhere (command line, CI server, etc.). – CommonsWare Dec 02 '16 at 18:48
  • So how to write a script which will work for command line as well as within the Android Studio? – Pawan Dec 02 '16 at 18:50
  • The variables should be taken from the `gradle.properties` file from both the project and home `gradle` directory (on Mac it's at `~/.gradle`. It wouldn't matter where the build happened. I would look at those to make sure it's correct. – DeeV Dec 02 '16 at 18:59
  • The Android Studio problem is resolved but now there is another problem, the CL build fails with error `Failed to read key relase_key from store "/home/ubuntu/ks/myapp/app-release.keystore": Keystore was tampered with, or password was incorrect`. Looks like the gradle when run from CL can resolve the path but fails to read key from KS. – Pawan Dec 02 '16 at 19:13
  • maybe my answer http://stackoverflow.com/a/38046932/4758255 will be helpful to you. – ישו אוהב אותך Dec 02 '16 at 20:29
  • Possible duplicate of [Environment variable in settings.gradle not working with Android Studio](https://stackoverflow.com/questions/21173826/environment-variable-in-settings-gradle-not-working-with-android-studio) – Richard Le Mesurier May 17 '22 at 10:14

1 Answers1

0

I have done investigation based on the comments by @DeeV and @CommonsWare and have done some changes to run the build successfully on Android Studio as well as on command line.

  1. I have placed gradle.properties file in Gradle's home directory. In my environment (ubuntu) the gradle home directory is located at ~/.gradle.

  2. I have to remove the System.env. from the usage of the variables, so this way it uses the properties declared in gradle.properties file.

With these two changes, I noticed that the Android Studio problem got resolved. However this has caused another problem, the CL build was failing with error: Failed to read key relase_key from store "/home/ubuntu/ks/myapp/app-release.keystore": Keystore was tampered with, or password was incorrect. This means the gradle when run from CL can resolve the path but fails to read key from KS.

I have to do the following step to get rid of this error:

  1. In the gradle.properties, I had put the values for RELEASE_KEY_PASSWORD and RELEASE_KEY_STORE_PASSWORD in double quotes, I removed the quotes and then the build completed successfully from command line.

I hope this will help anyone who face similar situation.

Pawan
  • 1,183
  • 16
  • 29