Skip to content

Commit

Permalink
Issue mozilla-mobile#60: Generate versionCode for releases dynamically.
Browse files Browse the repository at this point in the history
  • Loading branch information
pocmo committed Sep 10, 2018
1 parent cb45271 commit b0fe8c4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
31 changes: 30 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ android {
minSdkVersion rootProject.ext.build['minSdkVersion']
targetSdkVersion rootProject.ext.build['targetSdkVersion']
versionCode 1
versionName "1.0"
versionName AppVersion.versionName

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
shrinkResources false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand Down Expand Up @@ -66,6 +68,33 @@ android {
}
}

android.applicationVariants.all { variant ->
def buildType = variant.buildType.name

if (buildType == "release") {
def versionCode = AppVersion.baseVersionCode

// The Google Play Store does not allow multiple APKs for the same app that all have the
// same version code. Therefore we need to have different version codes for our ARM and x86
// builds.

// Our x86 builds need a higher version code to avoid installing ARM builds on an x86 device
// with ARM compatibility mode. In addition to that aarch64 builds should be preferred over
// ARM builds if suitable: x86 (+2) > aarch64 (+1) > arm.

// Note: This assumes that we do not need to release more often than every 2 minutes!
// Otherwise this will create version code conflicts.

if (variant.flavorName.contains("X86")) {
versionCode = versionCode + 2
} else if (variant.flavorName.contains("Aarch64")) {
versionCode = versionCode + 1
}

variant.outputs.all { output -> setVersionCodeOverride(versionCode) }
}
}

repositories {
////////////////////////////////////////////////////////////////////////////////////////////
// GeckoView Nightly
Expand Down
8 changes: 8 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

plugins {
`kotlin-dsl`
}

56 changes: 56 additions & 0 deletions buildSrc/src/main/java/AppVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

// The first Nightly releases of Fenix will ship in 2018 so we are using this as the base year for
// our version code.
private const val BASE_YEAR = 18

object AppVersion {
/**
* The version number shown to users.
*/
val versionName = "1.0"

/**
* Internal version number used for release builds.
*/
val baseVersionCode: Int

init {
// This code generates our base "unique" version code for release builds.
//
// The result of the version code generation depends on the timezone. We assume that this
// code will only be used for release versions and running on our build servers with a
// fixed timezone.
//
// The version code is composed like: yDDDHHmm
// * y = Double digit year, with 18 subtracted: 2018 -> 18 -> 0 or 2019 -> 19 -> 1
// * DDD = Day of the year, pad with zeros if needed: September 6th -> 249
// * HH = Hour in day (00-23)
// * mm = Minute in hour
//
// For September 10th, 2018, 5:04pm am this will generate the versionCode: 2531704 (0-253-1704).

val today = Date()

// We use the current year (double digit) and subtract the base year (see above). This value
// will start counting at 0 and increment by one every year.
val year = (SimpleDateFormat("yy", Locale.US).format(today).toInt() - BASE_YEAR).toString()

// We use the day in the Year (e.g. 248) as opposed to month + day (0510) because it's one
// digit shorter. If needed we pad with zeros (e.g. 25 -> 025)
val day = String.format("%03d", SimpleDateFormat("D", Locale.US).format(today).toInt())

// We append the hour in day (24h) and minute in hour (7:26 pm -> 1926). We do not append
// seconds. This assumes that we do not need to build multiple release(!) builds the same
// minute.
val time = SimpleDateFormat("HHmm", Locale.US).format(today)

baseVersionCode = (year + day + time).toInt()
}
}

0 comments on commit b0fe8c4

Please sign in to comment.