AS从控制台或者环境变量中获取签名文件密码的方法

来源:互联网 发布:天刀涂山红红捏脸数据 编辑:程序博客网 时间:2024/05/18 03:31

原文地址:http://stackoverflow.com/questions/18328730/how-to-create-a-release-signed-apk-file-using-gradle


有空翻译,Mark


Note that @sdqali's script will (at least when using Gradle 1.6) ask for the password anytime you invoke any gradle task. Since you only need it when doing gradle assembleRelease (or similar), you could use the following trick:

android {    ...    signingConfigs {        release {            // We can leave these in environment variables            storeFile file(System.getenv("KEYSTORE"))            keyAlias System.getenv("KEY_ALIAS")            // These two lines make gradle believe that the signingConfigs            // section is complete. Without them, tasks like installRelease            // will not be available!            storePassword "notYourRealPassword"            keyPassword "notYourRealPassword"        }    }    ...}task askForPasswords << {    // Must create String because System.readPassword() returns char[]    // (and assigning that below fails silently)    def storePw = new String(System.console().readPassword("Keystore password: "))    def keyPw  = new String(System.console().readPassword("Key password: "))    android.signingConfigs.release.storePassword = storePw    android.signingConfigs.release.keyPassword = keyPw}tasks.whenTaskAdded { theTask ->     if (theTask.name.equals("packageRelease")) {        theTask.dependsOn "askForPasswords"    }}

Note that I also had to add the following (under android) to make it work:

buildTypes {    release {        signingConfig signingConfigs.release    }}
shareimprove this answer
 
 
After implementing this, installRelease disappeared from the list of tasks... Why? – Kaarel Oct 17 '13 at 18:32
1 
I've updated the script; this should fix your problem! – caspase Oct 18 '13 at 19:11
1 
@caspase Wish I had taken your comment about that fake "storePassword" and "keyPassword" more seriously. Without initialising these properties ("" for example) the signed *-release.apk is not created, no error is displayed and your are left completely puzzled with just the *-release-unsigned.apk in your PROJECT_NAME/build/apk/ directory. Man... :/ – vizZ Dec 2 '13 at 16:40
 
Thanks for the note about adding signingConfig under buildTypes -> Release. That solved automated signing for me! – mm2001 Dec 14 '13 at 4:28
1 
I made a simple gradle plugin that asks for passwords when building release apk (using mathod described in this post, but you will not need to define fake storePassword & keyPassword). It is also available in maven central. github.com/alexvasilkov/AndroidGradleSignPlugin – Alex Vasilkov Feb 28 '14 at 7:40
 
This is great. Beware that the environment variable KEYSTORE needs to be defined even for debug builds and for "gradle sync" inside Android Studio, otherwise it'll give an error about path being null. – Jerry101 Mar 31 '14 at 8:07
 
If running from GUI and System.console() is not available, try something like: def storePw = new Scanner(Runtime.getRuntime().exec("/usr/libexec/openssh/ssh-‌​askpass Keystore Password").getInputStream()).useDelimiter("\\n").next() – codebeard Apr 23 at 5:14 
0 0