Android 自定义打包路径和 apk 文件名

此文章发布于 53 个月前,部分信息可能已经过时,请自行斟酌确认。

修改 build.gradle 增加以下代码

//自定生成 apk 文件名和生成路径
android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        // 生成路径
        variant.getPackageApplicationProvider().get().outputDirectory = new File("${rootDir}\\release")

        // 名称+当前版本号
        def fileName = "app_${defaultConfig.versionName}"

        // 如果有渠道,显示渠道名""
        if (variant.flavorName != "") {
            fileName = fileName + "_${variant.flavorName}"
        }
        // 加入打包日期,如20181010
        def releaseDate = new Date().format("yyyyMMdd", TimeZone.getDefault())
        fileName = fileName + "_${releaseDate}"

        // 如果是debug版本,加入debug
        if ('debug' == variant.buildType.name) {
            fileName = fileName + "_debug"
        }
        // 最终生成文件名
        output.outputFileName = fileName + ".apk"
    }
}

最终打包效果:

20191110162124.png

最后修改:2019 年 11 月 10 日 04 : 22 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论