下面给出一份 2025 年最新、可直接复制粘贴的 release 打包配置模板 (app/build.gradle.kts 与 build.gradle 双版本),涵盖:
- 自动读取本地
key.properties完成签名 - 启用 R8 全量压缩(代码 + 资源 + 语言)
- 自动 zipAlign、去调试信息、ABI 过滤
- 同时输出 带版本号命名的 APK + AAB 到
outputs/release/ - 支持 Gradle 8.5 + AGP 8.3 + JDK 17
1️⃣ Groovy 版(app/build.gradle)
groovy
// 读取签名
def keystoreProps = new Properties()
def keystoreFile = rootProject.file('key.properties')
if (keystoreFile.exists()) keystoreProps.load(keystoreFile.newDataInputStream())
android {
namespace "com.example.myapp"
compileSdk 35
defaultConfig {
applicationId "com.example.myapp"
minSdk 24
targetSdk 35
versionCode = project.property("verCode").toInteger()
versionName = project.property("verName")
ndk.abiFilters = listOf("arm64-v8a", "armeabi-v7a")
}
signingConfigs {
create("release") {
keyAlias = keystoreProps["keyAlias"]
keyPassword = keystoreProps["keyPassword"]
storeFile = file(keystoreProps["storeFile"])
storePassword = keystoreProps["storePassword"]
}
}
buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
isZipAlignEnabled = true
isDebuggable = false
signingConfig = signingConfigs.getByName("release")
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
// 输出重命名 + 自动拷贝
applicationVariants.configureEach { variant ->
variant.outputs.configureEach { output ->
val ext = output.outputFile.extension
val name = "MyApp-v${variant.versionName}-${variant.versionCode}-${output.baseName}.${ext}"
output.outputFileName = name
variant.assembleProvider.configure {
it.doLast {
copy {
from(output.outputFile)
into(rootProject.file("outputs/release"))
}
}
}
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
2️⃣ Kotlin DSL 版(app/build.gradle.kts)
kotlin
import java.util.Properties
// 读取签名
val keystoreProps = Properties().apply {
val f = rootProject.file("key.properties")
if (f.exists()) load(f.inputStream())
}
android {
namespace = "com.example.myapp"
compileSdk = 35
defaultConfig {
applicationId = "com.example.myapp"
minSdk = 24
targetSdk = 35
versionCode = extra["verCode"] as Int
versionName = extra["verName"] as String
ndk.abiFilters += listOf("arm64-v8a", "armeabi-v7a")
}
signingConfigs {
create("release") {
keyAlias = keystoreProps["keyAlias"] as String
keyPassword = keystoreProps["keyPassword"] as String
storeFile = file(keystoreProps["storeFile"] as String)
storePassword = keystoreProps["storePassword"] as String
}
}
buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
isZipAlignEnabled = true
isDebuggable = false
signingConfig = signingConfigs.getByName("release")
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
applicationVariants.configureEach {
outputs.configureEach {
val ext = outputFile.extension
val name = "MyApp-v${versionName}-${versionCode}-${baseName}.$ext"
outputFileName = name
assembleProvider.configure {
doLast {
copy {
from(outputFile)
into(rootProject.file("outputs/release"))
}
}
}
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
3️⃣ 版本号集中管理(gradle.properties)
verCode=1008
verName=2.3.4
4️⃣ 签名文件(勿提交 Git)
key.properties(放在项目根目录)
storeFile=../keystore/myapp.jks
storePassword=*****
keyAlias=myapp
keyPassword=*****
5️⃣ 常用命令
bash
# 打 APK
./gradlew assembleRelease
# 打 AAB(Google Play)
./gradlew bundleRelease
# 一键清理 + 打 APK + 安装
./gradlew clean assembleRelease
把以上代码复制到 app/build.gradle(或 .kts)即可 零额外脚本 打出正式、签名、压缩、重命名的 release 包。
更多阅读
大前端++
AI 对大前端项目的冲击,【大前端++】来抵御
【混合开发】进阶到【大前端++】
【大前端++】几大特征
【大前端++】前端、大前端、大前端++的区别有哪些?
Android推荐阅读
Cannot fit requested classes in a single dex file (# methods: 93047 > 65536)
【Android】开发者模式启用
开发工具链推荐
API开发工具postman、国内xxapi和SmartApi的性能对比