Skip to content

Commit e17fb27

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 e17fb27

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

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)