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

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

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

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")          } 

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'

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' 

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.

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

回答1:

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")          } 

Hope this helps.



回答2:

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

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

Now update your build.gradle file with:

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!