Android studio release 包打包配置 build.gradle

下面给出一份 2025 年最新、可直接复制粘贴的 release 打包配置模板app/build.gradle.ktsbuild.gradle 双版本),涵盖:

  • 自动读取本地 key.properties 完成签名
  • 启用 R8 全量压缩(代码 + 资源 + 语言)
  • 自动 zipAlign、去调试信息、ABI 过滤
  • 同时输出 带版本号命名的 APK + AABoutputs/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的性能对比

心法杂谈

【心力建设】《毛选》里的心法

【心力建设】3:如何在组织集体或团队里得到认可

健康杂谈

【论健康】怎么才算健康(健康的本质)
【论健康】健康的不可能三角

相关推荐
一点一木30 分钟前
深度体验TRAE SOLO移动端7天:作为独立开发者,我把工作流揣进了兜里
前端·人工智能·trae
天外飞雨道沧桑1 小时前
TypeScript 中 omit 和 record 用法
前端·javascript·typescript
赏金术士1 小时前
Compose 教学项目
android·kotlin·compose
Lee川1 小时前
mini-cursor 揭秘:从 Tool 定义到 Agent 循环的完整实现
前端·人工智能·后端
晓梦林1 小时前
ximai靶场学习笔记
android·笔记·学习
canonical_entropy2 小时前
从 Spec-Driven Development 到 Attractor-Guided Engineering
前端·aigc·ai编程
研☆香2 小时前
聊聊前端页面的三种长度单位
前端
给钱,谢谢!3 小时前
React + PixiJS 实现果园成长页:从状态机到浇水动画
前端·react.js·前端框架
暗冰ཏོ4 小时前
VUE面试题大全
前端·javascript·vue.js·面试
次元工程师!4 小时前
LangFlow开发(三)—Bundles组件架构设计(3W+字详细讲解)
java·前端·python·低代码·langflow