[英]Use different VersionCode for Debug/Release android gradle build
I'd like to apply different VersionCode to make apk file.我想应用不同的 VersionCode 来制作 apk文件。 For debug only fix it to 1
, and for release whatever number specified in defaultConfig.对于调试,仅将其修复为1
,并发布 defaultConfig 中指定的任何数字。
Below code gives mypackage-release-1.apk
file as assembleRelease artifact, which is not expected.下面的代码将mypackage-release-1.apk
文件作为assembleRelease工件提供,这是不期望的。 I expected mypackage-release-10111.apk
for that.我期待mypackage-release-10111.apk
。
why the line debug { defaultConfig.versionCode=1 }
affects assembleRelease artifact?为什么debug { defaultConfig.versionCode=1 }
会影响 assembleRelease 工件?
defaultConfig {
versionCode 10111
versionName '2.5.4'
minSdkVersion 10
targetSdkVersion 21
}
signingConfigs {
debug {
project.ext.loadSign = false
defaultConfig.versionCode = 1 // Why this value applied to assembleRelease?
}
release {
project.ext.loadSign = true
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionCode + ".apk"))
}
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
}
Me too, but I think defaultConfig.versionCode
was set when build.gradle
be compiling.我也是,但我认为在编译build.gradle
时设置了defaultConfig.versionCode
。 It's global static variable, and assigned at compiletime, not runtime.它是全局静态变量,在编译时分配,而不是运行时。
I think we can intercept gradle task execution, and modify defaultConfig.versionCode
at runtime.我觉得我们可以拦截gradle任务执行,在运行时修改defaultConfig.versionCode
。
After goooooooogle, I found this one works for me: https://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2在 goooooooogle 之后,我发现这个对我有用: https ://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2
Late on the party...晚会迟到...
The entire gradle file evaluated before any task execution, so you are basically changing the default versionCode
while declaring debug
configs.在任何任务执行之前评估整个 gradle 文件,因此您基本上是在声明debug
配置时更改默认versionCode
。 There is no direct way to reset versionCode
from buildType
, but the link on the other answer do the trick by declaring a task on build variants.没有直接的方法可以从buildType
重置versionCode
,但是另一个答案上的链接通过在构建变体上声明任务来解决问题。
android {
...
defaultConfig {
...
}
buildTypes {
...
}
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def versionCode = flavor.versionCode
if (variant.buildType.isDebuggable()) {
versionCode += 1
}
flavor.versionCode = versionCode
}
}
Here's an updated version:这是一个更新的版本:
android {
defaultConfig { ... }
applicationVariants.all { variant ->
if (variant.name == 'debug') {
variant.outputs.each { output ->
output.versionCodeOverride = 1
}
}
}
}
To use with Flavors:与风味一起使用:
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def name = flavor.getVersionName()
def code = flavor.getVersionCode()
if (variant.buildType.isDebuggable()) {
name += '-d'
code = 1
}
variant.outputs.each { output ->
output.versionNameOverride = name
output.versionCodeOverride = code
}
}
The easiest solution is moving versionCode and versionName variables from defaultConfig to debug and release respectively.最简单的解决方案是将 versionCode 和 versionName 变量从 defaultConfig 分别移动到调试和发布。
android {
...
defaultConfig {
// without versionCode and versionName
...
}
buildTypes {
debug {
defaultConfig.versionCode X
defaultConfig.versionName 'X.Y.Z'
}
release {
defaultConfig.versionCode A
defaultConfig.versionName 'A.B.C'
}
}
...
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
if (variant.buildType.isDebuggable()) {
output.versionCodeOverride = 26
output.versionNameOverride = "2.2.6"
}
}
}
put it in android{}将其放入 android{}
So recently I had to deal with the same scenario and all the examples I could find use the applicationVariants
property which is ill-documented imo.所以最近我不得不处理同样的场景,我能找到的所有例子都使用了applicationVariants
属性,该属性在 imo 中没有得到很好的记录。
So after some digging through the source code a bit, I realized that in the end versionCode
and versionName
properties from ProductFlavor
get merged into the AndroidManifest which got me thinking: couldn't we just inject them by ourselves, cause we have manifestPlaceholders
property on ProductFlavor AND on BuildType DSL objects, so I came up with this -- don't hesitate to give feedback and tell me why it's wrong因此,在对源代码进行了一些挖掘之后,我意识到最终ProductFlavor
versionCode
和versionName
属性合并到了 AndroidManifest 中,这让我想到:我们不能自己注入它们,因为我们在 ProductFlavor 上有manifestPlaceholders
属性并且在 BuildType DSL 对象上,所以我想出了这个 - 不要犹豫提供反馈并告诉我为什么它是错误的
In build.gradle(app)
在build.gradle(app)
android {
...
buildTypes {
debug {
manifestPlaceholder = [versionCode: X, versionName: "X.Y.Z"]
}
release {
manifestPlaceholder = [versionCode: A, versionName: "A.B.C"]
}
}
...
}
In AndroidManifest.xml
在AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="..."
android:versionCode="${versionCode}"
android:versionName="${versionName}">
...
</manifest>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.