Use .buildcount file for unique auto-incrementing versionCode

Build counter is incremented by deploy.ps1 on every deploy, ensuring
unique versionCode even from the same commit. File is gitignored.
This commit is contained in:
2026-02-24 12:09:36 +01:00
parent 9259b0fb28
commit 756a3da783
3 changed files with 22 additions and 11 deletions

3
.gitignore vendored
View File

@@ -18,3 +18,6 @@ local.properties
# Platform tools # Platform tools
ovr-platform-util.exe ovr-platform-util.exe
# Build counter
.buildcount

View File

@@ -14,18 +14,19 @@ fun gitVersionName(): String = runGit("describe", "--tags", "--abbrev=0")
.removePrefix("v") .removePrefix("v")
.ifEmpty { "0.1.0" } .ifEmpty { "0.1.0" }
// Build number = total commits (always increments) + offset for store history // Build number from .buildcount file (incremented by deploy.ps1)
const val VERSION_CODE_OFFSET = 4 fun buildNumber(): Int {
fun gitBuildNumber(): Int = (runGit("rev-list", "--count", "HEAD") val file = rootProject.file(".buildcount")
.toIntOrNull() ?: 1) + VERSION_CODE_OFFSET return if (file.exists()) file.readText().trim().toIntOrNull() ?: 1 else 1
}
fun gitShortHash(): String = runGit("rev-parse", "--short", "HEAD") fun gitShortHash(): String = runGit("rev-parse", "--short", "HEAD")
.ifEmpty { "unknown" } .ifEmpty { "unknown" }
// Display: "0.1.0+5.abc1234" // Display: "0.1.0+6.abc1234"
fun gitDisplayVersion(): String { fun gitDisplayVersion(): String {
val base = gitVersionName() val base = gitVersionName()
val build = gitBuildNumber() val build = buildNumber()
val hash = gitShortHash() val hash = gitShortHash()
return "$base+$build.$hash" return "$base+$build.$hash"
} }
@@ -63,7 +64,7 @@ android {
applicationId = "com.omixlab.lckcontrol" applicationId = "com.omixlab.lckcontrol"
minSdk = 32 minSdk = 32
targetSdk = 34 targetSdk = 34
versionCode = gitBuildNumber() versionCode = buildNumber()
versionName = gitVersionName() versionName = gitVersionName()
buildConfigField("String", "DISPLAY_VERSION", "\"${gitDisplayVersion()}\"") buildConfigField("String", "DISPLAY_VERSION", "\"${gitDisplayVersion()}\"")

View File

@@ -11,17 +11,24 @@ $ErrorActionPreference = "Stop"
$AppId = "25653777174321448" $AppId = "25653777174321448"
$Token = "OC|25653777174321448|b861e3eeaf58edf097812b5fe588dabb" $Token = "OC|25653777174321448|b861e3eeaf58edf097812b5fe588dabb"
$OvrUtil = "$PSScriptRoot\ovr-platform-util.exe" $OvrUtil = "$PSScriptRoot\ovr-platform-util.exe"
$BuildCountFile = "$PSScriptRoot\.buildcount"
# --- Increment build count ---
$buildCount = 1
if (Test-Path $BuildCountFile) {
$buildCount = [int](Get-Content $BuildCountFile -Raw).Trim() + 1
}
Set-Content $BuildCountFile $buildCount -NoNewline
Write-Host "Build #$buildCount" -ForegroundColor Cyan
# --- Git version info --- # --- Git version info ---
$versionName = (git describe --tags --abbrev=0 2>$null) -replace '^v', '' $versionName = (git describe --tags --abbrev=0 2>$null) -replace '^v', ''
if (-not $versionName) { $versionName = "0.1.0" } if (-not $versionName) { $versionName = "0.1.0" }
$commitCount = git rev-list --count HEAD 2>$null
if (-not $commitCount) { $commitCount = "1" }
$shortHash = git rev-parse --short HEAD 2>$null $shortHash = git rev-parse --short HEAD 2>$null
if (-not $shortHash) { $shortHash = "unknown" } if (-not $shortHash) { $shortHash = "unknown" }
$displayVersion = "$versionName+$commitCount.$shortHash" $displayVersion = "$versionName+$buildCount.$shortHash"
Write-Host "Version: $displayVersion (versionCode=$commitCount)" -ForegroundColor Cyan Write-Host "Version: $displayVersion (versionCode=$buildCount)" -ForegroundColor Cyan
# --- Build APK --- # --- Build APK ---
$task = if ($BuildType -eq "release") { ":app:assembleRelease" } else { ":app:assembleDebug" } $task = if ($BuildType -eq "release") { ":app:assembleRelease" } else { ":app:assembleDebug" }