From 7dce9d4b9b9fcb547a844814083a3275e75f2626 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 7 Oct 2020 20:35:46 +1000 Subject: [PATCH] Android: Use automatic build numbers based on Git history --- android/app/build.gradle | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index d0b279d7c..dc6f4117b 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -7,9 +7,8 @@ android { applicationId "com.github.stenzek.duckstation" minSdkVersion 21 targetSdkVersion 29 - versionCode 1 - versionName "1.0" - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + versionCode(getBuildVersionCode()) + versionName "${getVersion()}" } buildTypes { release { @@ -49,3 +48,32 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' } + +// Adapted from Dolphin. + +def getVersion() { + def versionNumber = '0.0-unknown' + + try { + versionNumber = 'git describe --tags --exclude latest'.execute([], project.rootDir).text + .trim() + .replaceAll(/(-0)?-[^-]+$/, "") + } catch (Exception e) { + logger.error('Cannot find git, defaulting to dummy version number') + } + + return versionNumber +} + + +def getBuildVersionCode() { + try { + def versionNumber = 'git rev-list --first-parent --count HEAD'.execute([], project.rootDir).text + .trim() + return Integer.valueOf(versionNumber); + } catch (Exception e) { + logger.error('Cannot find git, defaulting to dummy version number') + } + + return 1; +}