转载请注明原作者,如果你觉得这篇文章对你有帮助或启发,不用请我喝咖啡:D
1.添加releaseConfig
signingConfigs{
releaseConfig{
storeFile file("mykey.keystore.jks")
storePassword "storepwd"
keyAlias "MyApp"
keyPassword "mypwd"
}
}
2.指向releaseConfig
在buildTypes中,release部分,添加sighingConfig 指向releaseConfig
buildTypes {
release {
minifyEnabled false
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
}
}
3. 隐藏密码
不想暴露密码,修改步骤1中的storePassword和keyPassword为
signingConfigs{
releaseConfig{
storeFile file("hnt.keystore.jks")
storePassword System.console().readLine("\nKeyStore password: ")
keyAlias "HaniuTang"
keyPassword System.console().readLine("\nKey password: ")
}
}
这样使用命令行进行打包(assembleRelease命令)会让你输入密码。
4.遇到的问题及解决
设置apk签名时从console中读取keyAlias以及密码,使用"system.console().readLine"方法,在编译时总是出现问题:
Cannot invoke method readLine() on null object"
使用命令行的时候没有问题,但是当使用android studio的时候,还是报这个错,可能是android studio默认设置gradle为deamon方式。
- 方法1:去除优化gradle设置的./gradle/gradle.properties中的damon为true。
Reference:
http://forums.gradle.org/gradle/topics/standard_in_when_using_the_daemon
- 方法2:加上
if(system.console() != null)
这之后再从命令行读取密码以及别名
Reference:
https://www.timroes.de/2013/09/22/handling-signing-configs-with-gradle/
5.最终脚本
最终,gradle脚本中的相关部分长这样:
signingConfigs{
releaseConfig{
storeFile file("mykey.keystore.jks")
storePassword
if(System.console() != null)
System.console().readLine("\nKeystore password: ")
keyAlias "MyApp"
keyPassword
if(System.console() != null)
System.console().readLine("\nKey password: ")
}
}
网友评论
* What went wrong:
A problem was found with the configuration of task ':app:packageRelease'.
> No value has been specified for property 'signingConfig.storePassword'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
option to get more log output. 用详细的日志再看看
signingConfigs {
config {
storeFile file(RELEASE_STORE_FILE)
storePassword
if(System.console() != null)
System.console().readLine("\nKeystore password: ")
keyAlias RELEASE_KEY_ALIAS
keyPassword
if(System.console() != null)
System.console().readLine("\nKey password: ")
}
}
buildTypes {
release {
signingConfig signingConfigs.config
}
}
怎么弄的?