【发布时间】:2013-06-17 10:01:53
【问题描述】:
为了避免以纯文本形式写入密钥库密码,我正在尝试将依赖项添加到由 android Gradle 插件创建的 assembleRelease 任务中。
我已经检查了 Gradle 文档 Manipulating existing tasks,但我无法将依赖项放在应有的位置
这是我的任务,在 android 插件上方的 $root$/myApp/build.gradle 中定义。
task readPasswordFromInput << {
def console = System.console()
ext.keystorePassword = console.readLine('\n\n\n> Enter keystore password: ')
}
apply plugin: 'android'
然后,我尝试了 Gradle 提供的两种可能性:task.dependsOn 和 task.doFirst,但都没有。后者似乎被忽略了,而 dependsOn 确实添加了依赖项,但在依赖项链中为时已晚。运行 ./gradlew tasks --all 会打印出这个
:assembleRelease - Assembles all Release builds [libs:ActionBarSherlock:bundleRelease, libs:DataDroid:bundleRelease, libs:SlidingMenu:bundleRelease]
:compileRelease
...
[SEVERAL TASKS]
...
:packageRelease
...
[SEVERAL TASKS]
...
:readPasswordFromInput
问题是,任务packageRelease
中需要keystore密码顺便说一句,这可以按我的意愿工作
buildTypes {
release {
def console = System.console()
ext.keystorePassword = console.readLine('\n\n\n> IF building release apk, enter keystore password: ')
debuggable false
signingConfigs.release.storePassword = ext.keystorePassword
signingConfigs.release.keyPassword = ext.keystorePassword
signingConfig signingConfigs.release
}
}
但每次使用 gradlew 时它都会要求输入密码,无论是 clean 还是 assemble
谢谢!
编辑
感谢@Intae Kim,这是我的 build.gradle 2.0 版
task readPasswordFromInput << {
def console = System.console()
ext.keystorePassword = console.readLine('\n\n\n> Enter keystore password: ')
android.signingConfigs.release.storePassword = ext.keystorePassword
android.signingConfigs.release.keyPassword = ext.keystorePassword
}
tasks.whenTaskAdded { task ->
if (task.name == 'validateReleaseSigning') {
task.dependsOn readPasswordFromInput
}
}
apply plugin: 'android'
然后,构建类型
release {
debuggable false
signingConfig signingConfigs.release
runProguard true
proguardFile 'my-file.txt'
}
Gradle 执行正确,但它只生成一个 release-unsigned.apk
【问题讨论】: