Android的摇篮:动态生成时更改VERSIONNAME [英] Android Gradle: Dynamically change versionName at build time
问题描述
我想效仿Android中的Maven插件发布使用的摇篮发布插件的定制版本:的https:// github上.COM /乡亲/摇篮释放
I'm trying to emulate Maven release plugin in Android by using a customized version of gradle-release plugin: https://github.com/townsfolk/gradle-release
有趣的步骤是:
- 检查提交的修改
- 步骤版code和删除-SNAPSHOT 后缀的版本名称
- 在建立
- 步骤版本的名称,并添加-SNAPSHOT 后缀为下一个开发版
- Check uncommitted changes
- Step version code and remove -SNAPSHOT suffix from version name
- Build
- Step version name and add -SNAPSHOT suffix for next development version
但是生成的APK总是有previous版本(即1.0.0-快照,而不是1.0.0)。
However the generated APK always has the previous versions (i.e. 1.0.0-SNAPSHOT instead of 1.0.0).
版本号会被保存和正确gradle.properties更新,所以我假设我需要更新的版本中的数据模型,以及以使更改生效。
Version numbers are stored and correctly updated in gradle.properties, so I'm assuming that I need to update the versions in the data model as well for the changes to take effect.
我的Android插件的配置:
My android plugin config:
defaultConfig {
versionCode versionCode as int // taken from gradle.properties
versionName versionName // taken from gradle.properties
minSdkVersion 10
targetSdkVersion 19
}
我试过
东西:
Things I tried:
preBuild << {
android.applicationVariants.each { variant ->
variant.versionName = versionName
}
}
但有没有VERSIONNAME的一个变种。
But there's no versionName in a variant.
preBuild << {
android.buildTypes.each { type ->
type.versionName = versionName
}
}
但有没有VERSIONNAME的类型。
But there's no versionName in a type.
preBuild << {
android.productFlavors.each { flavor ->
flavor.versionName = versionName
}
}
但也有我的应用程序没有任何味道(普通调试和发布版本类型只)。
But there are no flavors in my app (plain debug and release build types only).
我的方法是编写一个bash / BAT脚本调用摇篮,其中pretty的太多失败使用Groovy来提高构建定制化的目的之前步骤版本。
My alternative is to write a bash/bat script to step the versions before invoking Gradle, which pretty much defeats the purpose of using Groovy to improve build customization.
我怎样才能在Android摇篮插件在执行阶段动态更新的版本?
How can I update versions dynamically in the Android Gradle plugin in the execution phase?
推荐答案
这就是 buildTypes
是。你所描述什么是发布
构建,海事组织。
That's what buildTypes
are for. What you're describing is a release
build, IMO.
下面是一个例子:当执行 assembleDebug
它会给你的快照版本,并执行 assembleRelease
会给你创建一个干净,没有任何后缀,增加的版本号。接下来的调试版本也将采用递增的编号。
Here's an example: when executing assembleDebug
it will give you a snapshot build, and executing assembleRelease
will give you a clean build without any suffix and incremented version number. The next debug build will also use the incremented number.
下面是当文件夹中创建一个全功能的版本。它也应该有味道的工作,但是这只是一个副产物:)。摇篮2.2.1,Android插件1.1.3
The following is a fully functional build when the files are created in a folder. It should also work with flavors, but that's just a side product :). Gradle 2.2.1, Android plugin 1.1.3
apply plugin: 'com.android.application'
apply from: 'auto-version.gradle'
buildscript {
repositories { jcenter() }
dependencies { classpath 'com.android.tools.build:gradle:1.1.3' }
}
android {
buildToolsVersion = "21.1.2"
compileSdkVersion = "android-21"
buildTypes {
debug {
versionNameSuffix "-SNAPSHOT"
}
}
}
println "config code: ${calculateVersionCode()}, name: ${calculateVersionName()}"
的src /主/ AndroidManifest.xml中
<manifest package="com.example" />
自动version.gradle
ext {
versionFile = new File(project.rootDir, 'version.properties')
calculateVersionName = {
def version = readVersion()
return "${version['major']}.${version['minor']}.${version['build']}"
}
calculateVersionCode = {
def version = readVersion()
def major = version['major'] as int // 1..∞
def minor = version['minor'] as int // 0..99
def build = version['build'] as int // 0..999
return (major * 100 + minor) * 1000 + build
}
}
Properties readVersion() {
def version = new Properties()
def stream
try {
stream = new FileInputStream(versionFile)
version.load(stream)
} catch (FileNotFoundException ignore) {
} finally {
if (stream != null) stream.close()
}
// safety defaults in case file is missing
if(!version['major']) version['major'] = "1"
if(!version['minor']) version['minor'] = "0"
if(!version['build']) version['build'] = "0"
return version
}
void incrementVersionNumber() {
def version = readVersion()
// careful with the types, culprits: "9"++ = ":", "9" + 1 = "91"
def build = version['build'] as int
build++
version['build'] = build.toString()
def stream = new FileOutputStream(versionFile)
try {
version.store(stream, null)
} finally {
stream.close()
}
}
task incrementVersion {
description "Increments build counter in ${versionFile}"
doFirst {
incrementVersionNumber()
}
}
if (plugins.hasPlugin('android') || plugins.hasPlugin('android-library')) {
android {
defaultConfig {
versionName = calculateVersionName()
versionCode = calculateVersionCode()
}
afterEvaluate {
def autoIncrementVariant = { variant ->
if (variant.buildType.name == buildTypes.release.name) { // don't increment on debug builds
variant.preBuild.dependsOn incrementVersion
incrementVersion.doLast {
variant.mergedFlavor.versionName = calculateVersionName()
variant.mergedFlavor.versionCode = calculateVersionCode()
}
}
}
if (plugins.hasPlugin('android')) {
applicationVariants.all { variant -> autoIncrementVariant(variant) }
}
if (plugins.hasPlugin('android-library')) {
libraryVariants.all { variant -> autoIncrementVariant(variant) }
}
}
}
}
执行摇篮assembleDebug
正常建立,摇篮assembleRelease
递增而建,而摇篮incrementVersion
只递增。
注意:要小心摇篮组装
由于顺序 assembleDebug
和 assembleRelease
将产生不同的结果。的
Execute gradle assembleDebug
to build normally, gradle assembleRelease
to increment and build, and gradle incrementVersion
to just increment.
Note: be careful with gradle assemble
because the order of assembleDebug
and assembleRelease
will yield different results.
检查生成的文件中的建立
目录,看看是否值是根据自己的喜好。
Check the generated files in the build
directory to see if the values are to your liking.
有可能你在这种情况下,版本递增多次因为多个变种匹配发布版本类型多种口味。原来quesion是因为没有味道。如果你想有更多的控制时,版本号增加只是删除 afterEvaluate
块,并调用 incrementVersion
任务时你想要的:
It is possible you have multiple flavors in which case the version is incremented multiple times because multiple variants match the release build type. The original quesion was for no flavors. If you want to have more control when the version number is incremented just remove the afterEvaluate
block and call the incrementVersion
task whenever you want:
gradle incrementVersion assembleFreeRelease assemblePaidRelease
(上面的手动执行的是一个未经考验的想法。)
(The above manual execution is an untested idea.)
检查未提交的更改不包括在这个答案,那是另一场比赛。你可以钩到任务。preBuild.doFirst {这里/ *失败,如果未提交的更改* /}
如果我理解正确。但是,这在很大程度上取决于你的版本控制。问另一个问题的更多!
The "Check uncommitted changes" are not covered in this answer, that's another game. You could hook on to tasks.preBuild.doFirst { /*fail here if uncommited changes*/ }
if I understand correctly. But that highly depends on your version control. Ask another question for more!
这篇关于Android的摇篮:动态生成时更改VERSIONNAME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!