使用 Android Studio 在 gradle 中使用系统环境变量的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25589052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:33:21  来源:igfitidea点击:

Proper way to use System environment variables in gradle using Android Studio

androidgradleandroid-studioandroid-gradle-plugin

提问by Joel G Mathew

I am using Android Studio to build my project on an Ubuntu 14.04 system.

我正在使用 Android Studio 在 Ubuntu 14.04 系统上构建我的项目。

I wrote the following in my build.gradle files to avoid hardcoding storeFile, storePassword, keyAlias and keyPassword in my git repo:

我在 build.gradle 文件中写了以下内容,以避免在我的 git repo 中硬编码 storeFile、storePassword、keyAlias 和 keyPassword:

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'

但是 gradle 同步错误如下: Error:(49, 0) Neither path nor baseDir may be null or empty string. path='null' basedir='./pathto/TMessagesProj'

My .bashrc contains: source ~/.gradlercand my ~/.gradlerc contains the following:

我的 .bashrc 包含:source ~/.gradlerc我的 ~/.gradlerc 包含以下内容:

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.

我已经确认这些变量是由 shell 正确导入的。但是我不确定为什么它没有被 Android Studio 中的构建环境接收。

What's the proper way to use environment variables in gradle?

在 gradle 中使用环境变量的正确方法是什么?

回答by FMontano

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():

我也喜欢在我的环境变量中包含我的密钥库信息,而不是将它放在项目中。您的代码看起来不错,但我遇到了与文件路径相同的问题。我通过在将该值传递给之前将该值转换为字符串来解决它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")        
 }

Hope this helps.

希望这可以帮助。

回答by Kevin Brotcke

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

gradle.properties在源文件夹中创建一个文件(与 一起build.gradle)以仅应用于当前项目或在~/.gradle/gradle.properties系统范围内应用内容:

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

Now update your build.gradle file with:

现在更新你的 build.gradle 文件:

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

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

或者,您可以使用-P选项从命令行传递参数。例如,./gradlew assemble -Pkey_password=keypass