使用Android Studio在Gradle中正确使用系统环境变量的正确方法 [英] Proper way to use System environment variables in gradle using Android Studio

查看:188
本文介绍了使用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 repo中对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) Neither path nor baseDir may be null or empty string. 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中正确使用系统环境变量的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆