[英]Use different VersionCode for Debug/Release android gradle build
我想應用不同的 VersionCode 來制作 apk文件。 對於調試,僅將其修復為1
,並發布 defaultConfig 中指定的任何數字。
下面的代碼將mypackage-release-1.apk
文件作為assembleRelease工件提供,這是不期望的。 我期待mypackage-release-10111.apk
。
為什么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
}
}
我也是,但我認為在編譯build.gradle
時設置了defaultConfig.versionCode
。 它是全局靜態變量,在編譯時分配,而不是運行時。
我覺得我們可以攔截gradle任務執行,在運行時修改defaultConfig.versionCode
。
在 goooooooogle 之后,我發現這個對我有用: https ://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2
晚會遲到...
在任何任務執行之前評估整個 gradle 文件,因此您基本上是在聲明debug
配置時更改默認versionCode
。 沒有直接的方法可以從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
}
}
這是一個更新的版本:
android {
defaultConfig { ... }
applicationVariants.all { variant ->
if (variant.name == 'debug') {
variant.outputs.each { output ->
output.versionCodeOverride = 1
}
}
}
}
與風味一起使用:
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
}
}
最簡單的解決方案是將 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"
}
}
}
將其放入 android{}
所以最近我不得不處理同樣的場景,我能找到的所有例子都使用了applicationVariants
屬性,該屬性在 imo 中沒有得到很好的記錄。
因此,在對源代碼進行了一些挖掘之后,我意識到最終ProductFlavor
versionCode
和versionName
屬性合並到了 AndroidManifest 中,這讓我想到:我們不能自己注入它們,因為我們在 ProductFlavor 上有manifestPlaceholders
屬性並且在 BuildType DSL 對象上,所以我想出了這個 - 不要猶豫提供反饋並告訴我為什么它是錯誤的
在build.gradle(app)
android {
...
buildTypes {
debug {
manifestPlaceholder = [versionCode: X, versionName: "X.Y.Z"]
}
release {
manifestPlaceholder = [versionCode: A, versionName: "A.B.C"]
}
}
...
}
在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.