From c82704cd4334ea0820d85adcfe0e9ed3609896d9 Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Sun, 27 Oct 2024 18:51:41 +0800 Subject: [PATCH] build: add git hash / count into version name Signed-off-by: Art_Chen --- app/build.gradle.kts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b918a3d..d994962 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,4 +1,5 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import java.io.* plugins { alias(libs.plugins.agp.app) @@ -15,6 +16,33 @@ apksign { keyPasswordProperty = "KEY_PASSWORD" } +val getGitCommitCount: () -> Int = { + val output = ByteArrayOutputStream() + ProcessBuilder("git", "rev-list", "--count", "HEAD").start().apply { + inputStream.copyTo(output) + waitFor() + } + output.toString().trim().toInt() +} + +val getVersionCode: () -> Int = { + val commitCount = getGitCommitCount() + val major = 5 + major + commitCount +} + +fun getGitHash(): String { + val process = ProcessBuilder("git", "rev-parse", "--short", "HEAD").start() + val output = process.inputStream.bufferedReader().use { it.readText().trim() } + return output +} + +fun getGitHashLong(): String { + val process = ProcessBuilder("git", "rev-parse", "HEAD").start() + val output = process.inputStream.bufferedReader().use { it.readText().trim() } + return output +} + android { compileSdk = 35 @@ -26,25 +54,34 @@ android { versionName = "2.7.0-U-HyperOS" } + val gitCode = getVersionCode() + val gitHash = getGitHash() buildTypes { debug { isDebuggable = true isMinifyEnabled = false isShrinkResources = false + buildConfigField("String", "GIT_HASH", "\"$gitHash\"") + buildConfigField("String", "GIT_CODE", "\"$gitCode\"") proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) + + versionNameSuffix = "_${gitHash}_r${gitCode}" } release { isMinifyEnabled = true isShrinkResources = true isDebuggable = false multiDexEnabled = true + buildConfigField("String", "GIT_HASH", "\"$gitHash\"") + buildConfigField("String", "GIT_CODE", "\"$gitCode\"") proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) + versionNameSuffix = "_${gitHash}_r${gitCode}" } }