Android gradle build System.getEnv(“RELEASE_PASSWO

2020-05-29 22: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
    }
  }
}

回答1:

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



回答2:

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.



回答3:

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



回答4:

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



回答5:

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.