Skip to content

Commit 85f7f46

Browse files
committed
feat(android): use git commit count for sample app version code
Replace time-based version code generation with git commit count, providing deterministic and monotonically increasing version codes.
1 parent 489dadc commit 85f7f46

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

.github/workflows/android-build-sample.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build Sample App
1+
name: Build Android Sample App
22

33
on:
44
workflow_dispatch:

android/sample/build.gradle.kts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,22 @@ dependencies {
114114
androidTestImplementation(libs.androidx.espresso.core)
115115
}
116116
/**
117-
* Computes the version code based on the current system time in minutes since epoch.
117+
* Computes the version code based on the number of git commits on the current branch.
118118
*
119-
* This is typically a not a good idea, but it's done so that we always get a unique
120-
* version code for every build so that every time we record data from sample app,
121-
* it shows up as a separate version on the dashboard.
119+
* This ensures a unique, monotonically increasing version code for each commit so that
120+
* every time we record data from the sample app, it shows up as a separate version on
121+
* the dashboard.
122122
*/
123123
fun computeVersionCode(): Int {
124-
return (System.currentTimeMillis() / (1000 * 60)).toInt()
124+
val process = ProcessBuilder("git", "rev-list", "--count", "HEAD")
125+
.directory(project.rootDir)
126+
.redirectErrorStream(true)
127+
.start()
128+
val output = process.inputStream.bufferedReader().readText().trim()
129+
val exitCode = process.waitFor()
130+
if (exitCode != 0) {
131+
logger.warn("Failed to compute version code from git, falling back to 1")
132+
return 1
133+
}
134+
return output.toIntOrNull() ?: 1
125135
}

0 commit comments

Comments
 (0)