构建配置: 优化APK输出设置

1. 添加自定义APK输出文件名配置
2. 文件名包含版本号、构建类型和时间信息
3. 更新Java编译版本到11
4. 启用代码混淆和资源压缩
5. 忽略启动器图标文件的Git跟踪
This commit is contained in:
yovinchen 2024-11-27 09:48:10 +08:00
parent faef022017
commit f74471e162

View File

@ -4,6 +4,10 @@ plugins {
id("com.google.devtools.ksp") id("com.google.devtools.ksp")
} }
import java.text.SimpleDateFormat
import java.util.Date
import com.android.build.api.variant.FilterConfiguration
android { android {
namespace = "com.yovinchen.bookkeeping" namespace = "com.yovinchen.bookkeeping"
compileSdk = 34 compileSdk = 34
@ -13,7 +17,7 @@ android {
minSdk = 26 minSdk = 26
targetSdk = 34 targetSdk = 34
versionCode = 1 versionCode = 1
versionName = "1.0" versionName = "1.0.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables { vectorDrawables {
@ -23,19 +27,37 @@ android {
buildTypes { buildTypes {
release { release {
isMinifyEnabled = false isMinifyEnabled = true
isShrinkResources = true
proguardFiles( proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro" "proguard-rules.pro"
) )
signingConfig = signingConfigs.getByName("debug") // 使用debug签名实际发布时应该使用正式的签名配置
} }
} }
applicationVariants.all {
val variant = this
variant.outputs
.map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
.forEach { output ->
// 获取当前时间
val date = SimpleDateFormat("yyyyMMdd_HHmm").format(Date())
// 获取CPU架构如果没有则使用universal
val buildType = variant.buildType.name
// 构建文件名
val outputFileName = "轻记账_${buildType}_v${variant.versionName}_${date}.apk"
output.outputFileName = outputFileName
}
}
compileOptions { compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_11
} }
kotlinOptions { kotlinOptions {
jvmTarget = "1.8" jvmTarget = "11"
} }
buildFeatures { buildFeatures {
compose = true compose = true