From f1cfce17f20e5da72f449c81dffb1c6ab03e7617 Mon Sep 17 00:00:00 2001 From: lisonge Date: Sat, 20 Jan 2024 13:33:59 +0800 Subject: [PATCH] perf: custom git version name --- app/build.gradle.kts | 33 ++++++++++++++++++------- app/src/main/kotlin/li/songe/gkd/App.kt | 6 ++++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e5b4430ec..dd0c36cb7 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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) @@ -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"]) @@ -83,6 +99,9 @@ android { signingConfig = currentSigning } release { + if (gitInfo?.tagName == null) { + versionNameSuffix = vnSuffix + } isMinifyEnabled = true isShrinkResources = true setProguardFiles( @@ -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") } diff --git a/app/src/main/kotlin/li/songe/gkd/App.kt b/app/src/main/kotlin/li/songe/gkd/App.kt index b3866de3b..fe66ee674 100644 --- a/app/src/main/kotlin/li/songe/gkd/App.kt +++ b/app/src/main/kotlin/li/songe/gkd/App.kt @@ -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) {