设置Android版本code摇篮任务完成后, [英] Set Android versionCode after Gradle task completes
问题描述
我有更新我的应用程序的版本code,称为任务的 changeVersion code 的。此任务之前,我的Android构建任务运行( assembleRelease 的),但显然它的安卓{}
结束后发生的。这似乎意味着该版本code设置,不能甚至改变时的 changeVersion code 的运行。
I have a task that updates my app's version code, called changeVersionCode. This task runs before my Android build tasks (assembleRelease), but obviously it happens after the android { }
closure. This seems to mean that versionCode is set and cannot be changed even when changeVersionCode runs.
下面是一个演示如何我试图解决这个问题简化构建脚本:
Here's a simplified build script that demonstrates how I have tried to approach this problem:
// .version is a file that starts at "1" the first time I call this
def loadVersionCode() {
// fetch version code from a file, '.version'
return loadVersionCodeFromFile('.version')
}
def incrementVersionCode() {
// fetch version code, update it, and save it back to a file
def newVersion = loadVersionCode() + 1
saveVersionCodeToFile('.version', newVersion)
}
apply plugin: 'com.android.application'
android {
// ... snip ...
defaultConfig {
// Set the version code to contents of .version (eg. 1)
versionCode loadVersionCode()
// ...
}
}
task incrementVersionCode << {
println "Old version code: " + android.defaultConfig.versionCode // prints 1
incrementVersionCode()
def newVersion = loadVersion() // this now returns 2
android.defaultConfig.versionCode = loadVersionCode()
// Also tried:
// android.defaultConfig.versionCode loadVersionCode()
println "New version code: " + android.defaultConfig.versionCode // prints 2
// android.defaultConfig.versionCode is now 2, but APK still has version 1 (until next time I run gradle)
}
然后:
# Build an APK with versionCode 1
$ ./gradlew assembleRelease
# This seems to change versionCode to 2, but still builds an APK with versionCode 1
#
# Prints:
# Old version code: 1
# New version code: 2
$ ./gradlew incrementVersionCode assembleRelease
我使用的:
- 摇篮2.5
- Groovy的2.3.10
- 蚂蚁1.9.3
- 的Java 1.8.0_45
- 的Mac OS X 10.10.5
- Android编译工具22.0.1
有什么办法,我可以从一个任务调用Android编译任务前更改我的版本code?
Is there any way I can change my version code from a task before invoking Android build tasks?
推荐答案
如何配置版本code前的任务启动
您可以使用DSL tasks.whenTaskAdded
。您可以阅读href=\"https://docs.gradle.org/current/userguide/build_lifecycle.html\" rel=\"nofollow\">官方文档,章58.6.2的
You can use the DSL tasks.whenTaskAdded
. You can read the official doc, chapter 58.6.2. Task creation.
您可以领取任务添加到项目后,立即通知。这可以用来设置一些默认值或执行任务前,添加行为的 在构建文件提供。
You can receive a notification immediately after a task is added to a project. This can be used to set some default values or add behaviour before the task is made available in the build file.
您可以定义一个任务:
task incrementVersionCode << {
//do something
}
然后定义依赖性:
Then define the dependency :
tasks.whenTaskAdded { task ->
if (task.name == 'xxxxx') {
task.dependsOn incrementVersionCode
}
}
在你的情况,你可以做somenthing是这样的:
In your case you can do somenthing like this:
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig' || task.name == 'generateDebugBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}
如何配置版本code与函数
在顶层文件,您可以配置一个功能是这样的:
In the top-level file you can configure a function like this:
ext {
buildVersionCode = {
//...
}
}
在你的模块/的build.gradle
,你可以做somehing是这样的:
In your module/build.gradle
you can do somehing like this:
defaultConfig {
versionCode buildVersionCode()
//....
}
否则,你可以在做你的的build.gradle
是这样的:
defaultConfig {
//...
versionCode getMyNumber()
}
def getMyNumber() {
return //.... ;
}
这篇关于设置Android版本code摇篮任务完成后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!