From 09a20e8b8e7bd21e29f77cafea5a00a04c78e789 Mon Sep 17 00:00:00 2001 From: Philip Cohn-Cort Date: Wed, 29 Apr 2020 18:59:00 +0000 Subject: [PATCH] Include git commit in APK filename --- build.gradle | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/build.gradle b/build.gradle index 35b7adbe..25f5f9e0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,7 @@ +import com.android.build.gradle.AppExtension +import com.android.build.gradle.api.ApkVariantOutput +import com.android.build.gradle.api.ApplicationVariant + apply plugin: 'com.android.application' buildscript { @@ -66,6 +70,54 @@ android { } } +/** + * This is a special on-demand Gradle object. + * + * Its value will not be determined until someone calls one of the gitHashProvider.getXXX() methods. + * + * If it does not encounter an explicit 'return' statement, getHashProvider.isPresent() will return false. + */ +Provider gitHashProvider = project.provider { + Process gitCommand = null + try { + // This invokes 'git' immediately, but does not wait for it to finish + gitCommand = 'git rev-parse --short HEAD'.execute([], project.rootDir) + } catch (IOException ignored) { + } + + if (gitCommand == null) { + logger.log(LogLevel.WARN, "Could not make use of the 'git' command-line tool. Output filenames will not be customized.") + } else if (gitCommand.waitFor() == 0) { + // This call to '::getText' (using the 'text' Groovy accessor syntax) collects the + // output stream + return '-' + gitCommand.text.trim() + } else { + logger.log( + LogLevel.WARN, + "Could not determine which commit is currently checked out -" + + " did you download this code without the .git directory?" + ) + } +} + +// We know we can safely cast the 'android' type to the 'AppExtension' class because +// we used the 'com.android.application' plugin at the top of the file. +// +// Note the use of the '::all' extension method; unlike '::each', it can detect every +// object added to the collection, no matter in which build phase that happens. +(android as AppExtension).applicationVariants.all { ApplicationVariant v -> + logger.log(LogLevel.INFO, "Found a variant called '${v.name}'.") + if (v.buildType.debuggable) { + // We're looking at variants made from android.buildTypes.debug! This one + // might have multiple outputs, but only one output will be an APK file. + v.outputs.matching { it instanceof ApkVariantOutput }.all { + // Default output filename is "${project.name}-${v.name}.apk". We want + // the Git commit short-hash to be added onto that default filename. + (it as ApkVariantOutput).outputFileName = "${project.name}-${v.name}${gitHashProvider.getOrElse("")}.apk" + } + } +} + dependencies { repositories {