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

Proper way to use System environment variables in gradle using Android Studio(使用Android Studio在gradle中使用系统环境变量的正确方法)
本文介绍了使用Android Studio在gradle中使用系统环境变量的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

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

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

signingConfigs {
 debug {

    storeFile file(System.getenv("KEYSTORE"))
    storePassword System.getenv("KEYSTORE_PASSWORD")
    keyAlias System.getenv("KEY_ALIAS")
    keyPassword System.getenv("KEY_PASSWORD")        
 }

但 gradle 同步错误并出现以下错误:Error:(49, 0) path 和 baseDir 都不能为 null 或空字符串.path='null' basedir='./pathto/TMessagesProj'

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'

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

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

export KEYSTORE="/home/myname/keystore/mykey"
export KEYSTORE_PASSWORD='mypass'
export KEY_ALIAS='mykey'
export KEY_PASSWORD='keypass'

我已确认 shell 正确导入了这些变量.但是我不确定为什么 Android Studio 中的构建环境没有收到它.

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.

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

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

推荐答案

我也喜欢在我的环境变量中包含我的密钥库信息,而不是在项目中包含它.您的代码看起来不错,但我在文件路径上遇到了同样的问题.我通过在将它传递给 file() 之前将该值转换为字符串来解决它:

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

signingConfigs {
 debug {
    storeFile file(String.valueOf(System.getenv("KEYSTORE")))
    storePassword System.getenv("KEYSTORE_PASSWORD")
    keyAlias System.getenv("KEY_ALIAS")
    keyPassword System.getenv("KEY_PASSWORD")        
 }

这篇关于使用Android Studio在gradle中使用系统环境变量的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)