From b0fe8c46a1e148bacb1f90b2ba8127e79108b8f5 Mon Sep 17 00:00:00 2001 From: Sebastian Kaspari Date: Mon, 10 Sep 2018 17:11:46 +0200 Subject: [PATCH] Issue #60: Generate versionCode for releases dynamically. --- app/build.gradle | 31 ++++++++++++++- buildSrc/build.gradle.kts | 8 ++++ buildSrc/src/main/java/AppVersion.kt | 56 ++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 buildSrc/build.gradle.kts create mode 100644 buildSrc/src/main/java/AppVersion.kt diff --git a/app/build.gradle b/app/build.gradle index 2ded0b652e8b..41f008a2c95f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,7 +15,7 @@ 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" } @@ -23,6 +23,8 @@ android { buildTypes { release { minifyEnabled false + shrinkResources false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } @@ -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 diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts new file mode 100644 index 000000000000..2275dbce3f89 --- /dev/null +++ b/buildSrc/build.gradle.kts @@ -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` +} + diff --git a/buildSrc/src/main/java/AppVersion.kt b/buildSrc/src/main/java/AppVersion.kt new file mode 100644 index 000000000000..cd6d1f4b1d2a --- /dev/null +++ b/buildSrc/src/main/java/AppVersion.kt @@ -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() + } +}