Skip to content

Commit

Permalink
perf: custom git version name
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jan 20, 2024
1 parent bfb8dab commit f1cfce1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
33 changes: 24 additions & 9 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@ fun String.runCommand(currentWorkingDir: File = file("./")): String {
workingDir = currentWorkingDir
commandLine = this@runCommand.split("\\s".toRegex())
standardOutput = byteOut
errorOutput = ByteArrayOutputStream()
}
return String(byteOut.toByteArray()).trim()
}

val gitCommitId = try {
"git rev-parse HEAD".runCommand()
data class GitInfo(
val commitId: String,
val tagName: String?,
)

val gitInfo = try {
GitInfo(
commitId = "git rev-parse HEAD".runCommand(),
tagName = try {
"git describe --tags --exact-match".runCommand()
} catch (e: Exception) {
println("app: current git commit is not a tag")
null
},
)
} catch (e: Exception) {
e.printStackTrace()
println("app: git is not available")
null
}

val vnSuffix = "-${gitInfo?.commitId?.substring(0, 7) ?: "unknown"}"

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
Expand Down Expand Up @@ -51,7 +67,7 @@ android {
buildConfigField(
"String",
"GIT_COMMIT_ID",
jsonStringOf(gitCommitId)
jsonStringOf(gitInfo?.commitId)
)
buildConfigField(
"String", "GKD_BUGLY_APP_ID", jsonStringOf(project.properties["GKD_BUGLY_APP_ID"])
Expand Down Expand Up @@ -83,6 +99,9 @@ android {
signingConfig = currentSigning
}
release {
if (gitInfo?.tagName == null) {
versionNameSuffix = vnSuffix
}
isMinifyEnabled = true
isShrinkResources = true
setProguardFiles(
Expand All @@ -93,11 +112,7 @@ android {
)
}
debug {
versionNameSuffix = if (gitCommitId != null) {
"-${gitCommitId.substring(0, 8)}"
} else {
"-unknown"
}
versionNameSuffix = vnSuffix
applicationIdSuffix = ".debug"
resValue("string", "app_name", "GKD-debug")
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/kotlin/li/songe/gkd/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class App : Application() {
setConsoleSwitch(BuildConfig.DEBUG)
saveDays = 7
}
LogUtils.d("GIT_COMMIT_URL: $GIT_COMMIT_URL")
LogUtils.d(
"GIT_COMMIT_URL: $GIT_COMMIT_URL",
"VERSION_CODE: ${BuildConfig.VERSION_CODE}",
"VERSION_NAME: ${BuildConfig.VERSION_NAME}",
)

initFolder()
appScope.launchTry(Dispatchers.IO) {
Expand Down

0 comments on commit f1cfce1

Please sign in to comment.