Skip to content

Commit 519189c

Browse files
committed
build: add release signing config and APK output automation
1 parent 42c0845 commit 519189c

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
.cxx
1515
local.properties
1616

17+
# Release keystore (do not commit)
18+
*.jks
19+
*.keystore
20+
app/keystore/
1721

18-
.idea/
22+
# Release APKs
23+
/releases/
1924

25+
.idea/
26+
.kotlin/

app/build.gradle.kts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import java.util.Properties
2+
import java.text.SimpleDateFormat
3+
import java.util.Date
4+
15
plugins {
26
alias(libs.plugins.android.application)
37
alias(libs.plugins.kotlin.android)
@@ -6,27 +10,59 @@ plugins {
610
alias(libs.plugins.hilt)
711
}
812

13+
val localProperties = Properties().apply {
14+
val localPropertiesFile = rootProject.file("local.properties")
15+
if (localPropertiesFile.exists()) {
16+
load(localPropertiesFile.inputStream())
17+
}
18+
}
19+
20+
// 从 versionName 自动生成 versionCode
21+
// "1.2.3" -> 10203, "2.0.0" -> 20000
22+
fun generateVersionCode(versionName: String): Int {
23+
val parts = versionName.split(".").map { it.toIntOrNull() ?: 0 }
24+
val major = parts.getOrElse(0) { 0 }
25+
val minor = parts.getOrElse(1) { 0 }
26+
val patch = parts.getOrElse(2) { 0 }
27+
return major * 10000 + minor * 100 + patch
28+
}
29+
30+
val appVersionName = "1.0.0"
31+
val appVersionCode = generateVersionCode(appVersionName)
32+
val buildTime: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
33+
934
android {
1035
namespace = "com.wongyichen.fastcodescan"
1136
compileSdk = 35
1237

38+
signingConfigs {
39+
create("release") {
40+
storeFile = file(localProperties.getProperty("RELEASE_STORE_FILE", ""))
41+
storePassword = localProperties.getProperty("RELEASE_STORE_PASSWORD", "")
42+
keyAlias = localProperties.getProperty("RELEASE_KEY_ALIAS", "")
43+
keyPassword = localProperties.getProperty("RELEASE_KEY_PASSWORD", "")
44+
}
45+
}
46+
1347
defaultConfig {
1448
applicationId = "com.wongyichen.fastcodescan"
1549
minSdk = 26
1650
targetSdk = 35
17-
versionCode = 1
18-
versionName = "1.0"
51+
versionCode = appVersionCode
52+
versionName = appVersionName
1953

2054
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2155
}
2256

2357
buildTypes {
2458
release {
25-
isMinifyEnabled = false
59+
isMinifyEnabled = true
60+
isShrinkResources = true
2661
proguardFiles(
2762
getDefaultProguardFile("proguard-android-optimize.txt"),
2863
"proguard-rules.pro"
2964
)
65+
signingConfig = signingConfigs.getByName("release")
3066
}
3167
}
3268
compileOptions {
@@ -39,6 +75,27 @@ android {
3975
buildFeatures {
4076
compose = true
4177
}
78+
79+
applicationVariants.all {
80+
val variant = this
81+
variant.outputs.all {
82+
val output = this as com.android.build.gradle.internal.api.BaseVariantOutputImpl
83+
val apkName = "FastCodeScan-${variant.versionName}-${variant.buildType.name}-${buildTime}.apk"
84+
output.outputFileName = apkName
85+
86+
// 构建完成后移动 APK 到 releases 目录
87+
variant.assembleProvider.get().doLast {
88+
val releaseDir = rootProject.file("releases")
89+
if (!releaseDir.exists()) releaseDir.mkdirs()
90+
val srcFile = output.outputFile
91+
val destFile = File(releaseDir, apkName)
92+
if (!destFile.exists()) {
93+
srcFile.renameTo(destFile)
94+
println("APK moved to: ${destFile.absolutePath}")
95+
}
96+
}
97+
}
98+
}
4299
}
43100

44101
dependencies {

app/keystore/release.jks

2.71 KB
Binary file not shown.

0 commit comments

Comments
 (0)