Skip to content

Commit

Permalink
build: add git hash / count into version name
Browse files Browse the repository at this point in the history
Signed-off-by: Art_Chen <[email protected]>
  • Loading branch information
Art-Chen committed Oct 27, 2024
1 parent b172861 commit c82704c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import java.io.*

plugins {
alias(libs.plugins.agp.app)
Expand All @@ -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

Expand All @@ -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}"
}
}

Expand Down

0 comments on commit c82704c

Please sign in to comment.