1
votes

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
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
What do you mean by the maven option? setting props in the gradle.properties file?Pawan
Sorry. I meant Gradle option (#2).DeeV
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
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

1 Answers

0
votes

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.