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:如何在组织集体或团队里得到认可

健康杂谈

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

相关推荐
nvvas2 小时前
npm : 无法加载文件 D:\nvm\nodejs\npm.ps1,因为在此系统上禁止运行脚本问题解决
前端·npm·node.js
傲世(C/C++,Linux)2 小时前
Linux系统编程——进程通信之有名管道
android·linux·运维
拉不动的猪2 小时前
浏览器之内置四大多线程API
前端·javascript·浏览器
林太白2 小时前
5大排序算法&2大搜索&4大算法思想
前端
摇滚侠2 小时前
浏览器的打印功能,如果通过HTML5,控制样式
前端·html·html5
喵喵侠w2 小时前
uni-app微信小程序相机组件二次拍照白屏问题的排查与解决
前端·数码相机·微信小程序·小程序·uni-app
超大只番薯3 小时前
在Next.js中实现页面级别KeepAlive
前端
快递鸟3 小时前
第三方物流接口优选:快递鸟物流 API,打破单一快递对接壁垒
前端
旧时光_3 小时前
第3章:基础组件 —— 3.6 进度指示器
flutter