38

I'm having an issues where System.getenv() is returning null for the environment variable. My password is stored in the RELEASE_PASSWORD environment variable. When I do:

$ echo $RELEASE_PASSWORD

it prints out the correct value, so I know the variable is set.

I was originally setting the signingConfig signingConfigs.release in the release buildType and everything was working fine, but I need different signing configs for different product flavors. If I hardcode the password, it works just like it is suppose to. Things only get wonky when I try to read the password from an environment variable.

Is it some sort of scope issue?

This is what I currently have in my build.gradle.

android {

  ...

  signingConfigs {
    release {
      storeFile ...;
      keyAlias ...;
      storePassword System.getenv("RELEASE_PASSWORD");
      keyPassword System.getenv("RELEASE_PASSWORD");
    }

    unsigned {
      keyAlias "";
      storePassword "";
      keyPassword "";
    }
  }

  buildTypes {
    debug {
      versionNameSuffix = "-DEBUG"
    }

    release {
    }
  }

  flavorGroups "storeFront"

  productFlavors {
    def googleVariable = signingConfigs.release
    def amazonVariable = signingConfigs.unsigned

    google {
        flavorGroup "storeFront"
        signingConfig googleVariable
    }

    amazon {
        flavorGroup "storeFront"
        signingConfig amazonVariable
    }
  }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
HMCFletch
  • 1,046
  • 1
  • 10
  • 19

6 Answers6

18

You can run Android Studio from the command-line to get the environment variables passed into the IDE. In Mac OS you can run it from a terminal by running /Applications/Android\ Studio.app/Contents/MacOS/studio

Xavi Rigau
  • 1,421
  • 13
  • 17
  • I came to this answer myself today. This is unfortunately the only way I have found so far to use env variables in gradle scripts... – manroe Sep 25 '15 at 00:18
  • Yeah on our Bamboo build server it works fine because of the command line, but not on my dev iMac : – Henrique de Sousa Mar 07 '16 at 10:45
17

For a Windows user, if you make a new environment variable, you need to restart your PC. I don't know what's wrong with Windows, But that's how it works on it. What I tried is, In my gradle script I wrote a task to print my JAVA_HOME variable path like this:

task printJavaHome{
     println System.getenv("JAVA_HOME") 
}

and then synchronized with gradle.

Then open terminal window in your Studio.

and type:

gradlew -q printJavaHome

This will show your java home path if you set it otherwise it'll print null.

and before executing the above command, make sure the terminal is pointed at the root directory of the project.

Now try setting a new environment variable and load it, and try to print it as mentioned steps above. You'll see null if you're using Windows. But when will you restart PC and again execute the command, it'll then return your environment variable's actual value.

That's what I experienced and thought of sharing, might be useful for someone.

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
15

Android Studio doesn't pass environment variables to Gradle, so what you're trying to do won't work from the IDE. If you want a way to avoid keeping the keystore password in the build file, here's an answer with code for saving it in a separate file:

Sign APK without putting keystore info in build.gradle

Community
  • 1
  • 1
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks! Now that I think about it, I wasn't doing the googleRelease build from Android Studio, so I was never hitting this issue before. The person who actually built the release apks was doing it from the command line. Thanks for the quick response. – HMCFletch May 22 '14 at 23:34
  • 7
    @Scott, do you know why Android Studio doesn't pass environment variables to Gradle? Or a workaround for this? – Jason Hartley Jun 27 '14 at 15:40
  • 2
    I think this changed. For me (using Windows) it was enough to just restart Android Studio. – Tobias Feb 15 '16 at 23:47
  • 4
    No longer (ever?) true. You just aren't getting the env vars you think you should be. See these answers from similar questions (OS X) http://stackoverflow.com/a/30128305/1650674 (Linux) http://stackoverflow.com/a/27795843 – tir38 May 07 '17 at 04:34
  • The same applies for IntellijIdea on Ubuntu 18. If I run `./gradlew -q printJavaHome` it works, where in IDE it's "null" if I click "build" from the Gradle menu. In IDE's terminal it works also. – bogdan.rusu Nov 28 '18 at 11:09
  • A good, Gradle-friendly alternative is to add username and password credentials in the `~/.gradle/gradle.properties` file (i.e. outside of your Android projects) as described in [this answer](https://stackoverflow.com/a/38046724/1071320) in the same thread which Scott Barta has linked to. – Adil Hussain Apr 19 '22 at 11:25
3

For me (using Windows) it was enough to just restart Android Studio.

Tobias
  • 7,282
  • 6
  • 63
  • 85
  • If this doesn't help then restart your windows. That worked for me while you don't have admin credentials and you set user's environment variable. – Aks4125 Oct 05 '17 at 06:00
2

Android studio needs to be closed and restarted to recognize newly changed or added environment variables.

For some reason, I don't think that the operation "Invalidate Cache and restart" picks up new environment variables. I had done that a couple of times, and I don't think I got the new values doing that operation. That sounds hard for me to believe, so maybe I had some typo error and I'm confused on what I observed; but I did checked out the spelling a number of times, because that's the obvious cause. Without making any changes the problem I was having reading environment variables went away after doing a full close of android studio as opposed to doing an Invalidate Cache and restart operation. The reason I did an Invalidate Cache and restart operation, was because I had several Android Studio sessions going and that seems like a simple way to close them all and get them all restarted.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15
0

After I added new env variables I invalidated the IDE (Android Studio 4.0) caches. You can find this option in menu File.

File > Invalidate Caches / Restart...

MontDeska
  • 1,617
  • 19
  • 16