Skip to content

Commit

Permalink
chore: increment versionCode slower [WPB-9816] (#3115)
Browse files Browse the repository at this point in the history
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
vitorhugods and yamilmedina authored Jun 25, 2024
1 parent d27c144 commit 072f73d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
applicationId = AndroidApp.id
defaultConfig.targetSdk = AndroidSdk.target
versionCode = AndroidApp.versionCode
versionName = "${AndroidApp.versionName}-$versionCode"
versionName = "${AndroidApp.versionName}-${AndroidApp.leastSignificantVersionCode}"
setProperty("archivesBaseName", "$applicationId-v$versionName")
}

Expand Down
17 changes: 17 additions & 0 deletions build-logic/plugins/src/main/kotlin/AndroidCoordinates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@ object AndroidApp {
const val id = "com.wire.android"
const val versionName = "4.8.0"
val versionCode = Versionizer().versionCode

/**
* The last 5 digits of the VersionCode. From 0 to 99_999.
* It's an [Int], so it can be less than 5 digits when doing [toString], of course.
* Considering versionCode bumps every 5min, these are
* 288 per day
* 8640 per month
* 51840 per semester
* 103_680 per year. ~99_999
*
* So it takes almost a whole year until it rotates back.
* It's very unlikely that two APKs with the same version (_e.g._ 4.8.0)
* will have the same [leastSignificantVersionCode],
* unless they are build almost one year apart.
*/
@Suppress("MagicNumber")
val leastSignificantVersionCode = versionCode % 100_000
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,29 @@ class Versionizer(private val localDateTime: LocalDateTime = LocalDateTime.now()
val versionCode = generateVersionCode()

private fun generateVersionCode(): Int {
val duration = Duration.between(DATE_OFFSET, localDateTime)
return (duration.seconds / 10).toInt()
return if (localDateTime <= V2_DATE_OFFSET) {
val duration = Duration.between(V1_DATE_OFFSET, localDateTime)
(duration.seconds / V1_SECONDS_PER_BUMP).toInt()
} else { // Use V2
val duration = Duration.between(V2_DATE_OFFSET, localDateTime)
V2_VERSION_CODE_OFFSET + (duration.toMinutes() / V2_MINUTES_PER_BUMP).toInt()
}
}

companion object {
//This is Google Play Max Version Code allowed
//https://developer.android.com/studio/publish/versioning
const val MAX_VERSION_CODE_ALLOWED = 2100000000
// This is Google Play Max Version Code allowed
// https://developer.android.com/studio/publish/versioning
const val MAX_VERSION_CODE_ALLOWED = 2_100_000_000

// The time-based versioning on the current Android project subtracts from this date to start the count
private val DATE_OFFSET = LocalDateTime.of(2021, 4, 21, 1, 0)
private val V1_DATE_OFFSET = LocalDateTime.of(2021, 4, 21, 1, 0)
private const val V1_SECONDS_PER_BUMP = 10

// V2 starts at 100 million and 1 thousand
private const val V2_VERSION_CODE_OFFSET = 100_001_000

// From this date onwards, we bump every 5 min instead of every 10 seconds
private val V2_DATE_OFFSET = LocalDateTime.of(2024, 6, 21, 0, 0)
private const val V2_MINUTES_PER_BUMP = 5
}
}
22 changes: 18 additions & 4 deletions build-logic/plugins/src/test/kotlin/VersionizerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import com.wire.android.gradle.version.Versionizer
import org.amshove.kluent.internal.assertEquals
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldBeGreaterThan
import org.amshove.kluent.shouldBeLessThan
Expand Down Expand Up @@ -44,11 +45,24 @@ class VersionizerTest {
}

@Test
fun `given version generator when I generate a new version AFTER 10 SECONDS then I should get an directly incremented number`() {
val versionCodeOld = Versionizer(LocalDateTime.now().minusSeconds(10)).versionCode
val versionCodeNew = Versionizer().versionCode
fun `given before than 21 of June 2024 Build, then bump every 10 seconds`() {
val oldDate = LocalDateTime.of(2024, 6, 20, 0, 0)
val oldVersionCode = Versionizer(oldDate).versionCode
val newVersionCode = Versionizer(oldDate.plusSeconds(10)).versionCode

versionCodeNew shouldBeEqualTo versionCodeOld + 1
assertVersionCodeAreProperlyIncremented(oldVersionCode, newVersionCode)
assertEquals(1, newVersionCode - oldVersionCode)
}

@Test
fun `given after 21 of June 2024 Build, then bump every 5 minutes`() {
val oldDate = LocalDateTime.of(2024, 6, 22, 14, 0)
val oldVersionCode = Versionizer(oldDate).versionCode
val newVersionCode = Versionizer(oldDate.plusMinutes(5)).versionCode

println("Version number: $newVersionCode")
assertVersionCodeAreProperlyIncremented(oldVersionCode, newVersionCode)
assertEquals(1, newVersionCode - oldVersionCode)
}

@Test
Expand Down
Loading

0 comments on commit 072f73d

Please sign in to comment.