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

健康杂谈

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

相关推荐
似霰6 小时前
AIDL Hal 开发笔记2----AIDL HAL 实例分析light hal
android·framework·hal
—Qeyser6 小时前
Flutter 颜色完全指南
android·flutter·ios
niucloud-admin7 小时前
web 端前端
前端
奋斗的小青年!!7 小时前
Flutter跨平台开发鸿蒙应用实战:OA系统考勤打卡组件深度解析
flutter·harmonyos·鸿蒙
2501_916008898 小时前
iOS 上架需要哪些准备,账号、Bundle ID、证书、描述文件、安装测试及上传
android·ios·小程序·https·uni-app·iphone·webview
—Qeyser10 小时前
Flutter GestureDetector 完全指南:让任何组件都能响应手势
flutter·云原生·容器·kubernetes
豆豆菌10 小时前
Flutter运行时Running Gradle task ‘assembleDebug‘...很久无法启动
flutter
摘星编程10 小时前
React Native for OpenHarmony 实战:DatePickerAndroid 日期选择器详解
android·react native·react.js
胖者是谁10 小时前
EasyPlayerPro的使用方法
前端·javascript·css
鸣弦artha10 小时前
Flutter框架跨平台鸿蒙开发 —— Image Widget 基础:图片加载方式
flutter·华为·harmonyos