feat(android): APK versionCode 自动递增 — 每次编译自增,各环境独立计数

参考 rwadurian mobile-app 的 version.properties 方案,为 genex-mobile
和 admin-app 添加 build 时 versionCode 自动 +1 逻辑:

- build.gradle.kts: calculateNextVersionCode() 读取 version.properties,
  自增后写回;versionName 格式 "pubspec版本.buildNumber"(如 1.0.0.42)
- .gitignore: 排除 /android/app/version.properties,每个构建环境
  (本机、CI、服务器)各自维护独立计数器,互不干扰
- 首次编译从 1 开始,后续跨日期持续递增,保证 APK 版本始终唯一

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-03 09:09:13 -08:00
parent fcf49b5257
commit 0cd5c58ecb
4 changed files with 60 additions and 4 deletions

View File

@ -44,6 +44,9 @@ app.*.map.json
/android/app/profile
/android/app/release
# Auto-generated version code (each build environment maintains its own counter)
/android/app/version.properties
# Signing keys (do NOT commit to public repos)
android/key.properties
android/app/keystore/

View File

@ -1,5 +1,6 @@
import java.util.Properties
import java.io.FileInputStream
import java.io.FileOutputStream
plugins {
id("com.android.application")
@ -14,6 +15,30 @@ if (keystorePropertiesFile.exists()) {
keystoreProperties.load(FileInputStream(keystorePropertiesFile))
}
// ============================================
// Auto-increment version code on each build
// ============================================
val versionPropertiesFile = rootProject.file("app/version.properties")
val versionProperties = Properties()
fun calculateNextVersionCode(): Int {
if (versionPropertiesFile.exists()) {
versionProperties.load(FileInputStream(versionPropertiesFile))
}
val currentCode = versionProperties.getProperty("VERSION_CODE", "0").toInt()
val newCode = currentCode + 1
versionProperties.setProperty("VERSION_CODE", newCode.toString())
versionProperties.store(FileOutputStream(versionPropertiesFile), "Auto-generated version code - DO NOT EDIT MANUALLY")
return newCode
}
val autoVersionCode = calculateNextVersionCode()
fun getAutoVersionName(): String {
val flutterVersionName = flutter.versionName
return "$flutterVersionName.$autoVersionCode"
}
android {
namespace = "cn.gogenex.genex_issuer"
compileSdk = flutter.compileSdkVersion
@ -32,8 +57,8 @@ android {
applicationId = "cn.gogenex.issuer"
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
versionCode = autoVersionCode
versionName = getAutoVersionName()
}
signingConfigs {

View File

@ -44,6 +44,9 @@ app.*.map.json
/android/app/profile
/android/app/release
# Auto-generated version code (each build environment maintains its own counter)
/android/app/version.properties
# Signing keys (do NOT commit to public repos)
android/key.properties
android/app/keystore/

View File

@ -1,5 +1,6 @@
import java.util.Properties
import java.io.FileInputStream
import java.io.FileOutputStream
plugins {
id("com.android.application")
@ -14,6 +15,30 @@ if (keystorePropertiesFile.exists()) {
keystoreProperties.load(FileInputStream(keystorePropertiesFile))
}
// ============================================
// Auto-increment version code on each build
// ============================================
val versionPropertiesFile = rootProject.file("app/version.properties")
val versionProperties = Properties()
fun calculateNextVersionCode(): Int {
if (versionPropertiesFile.exists()) {
versionProperties.load(FileInputStream(versionPropertiesFile))
}
val currentCode = versionProperties.getProperty("VERSION_CODE", "0").toInt()
val newCode = currentCode + 1
versionProperties.setProperty("VERSION_CODE", newCode.toString())
versionProperties.store(FileOutputStream(versionPropertiesFile), "Auto-generated version code - DO NOT EDIT MANUALLY")
return newCode
}
val autoVersionCode = calculateNextVersionCode()
fun getAutoVersionName(): String {
val flutterVersionName = flutter.versionName
return "$flutterVersionName.$autoVersionCode"
}
android {
namespace = "cn.gogenex.genex_consumer"
compileSdk = flutter.compileSdkVersion
@ -33,8 +58,8 @@ android {
applicationId = "cn.gogenex.consumer"
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
versionCode = autoVersionCode
versionName = getAutoVersionName()
}
signingConfigs {