为避免以纯文本形式写入密钥库密码,我尝试将依赖项添加到由 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
问题是,任务包Release中需要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'
然后,buildTypes
release {
debuggable false
signingConfig signingConfigs.release
runProguard true
proguardFile 'my-file.txt'
}
Gradle 正确执行,但它只生成一个 release-unsigned.apk