AndroidStudio Ladybug中编译完成apk之后定制名字kts复制到指定目录

背景

编译完release版本后复制apk到特定目录,apk文件名字符合自己的需求,例如增加版本号,版本名字。

翻了一遍网上基本上都是Groovy编写,但是项目刚好切换到kts了,之前Groovy代码用不了。下面是kts版本。

kts适配

全部代码如下,只需要在你的主模块的android中增加代码即可。

kts 复制代码
android {

/其他代码

applicationVariants.all {
    val variant = this
    if (!assembleProvider.name.contains("Release")) {
        return@all
    }
    outputs.forEach {
        val output = it as BaseVariantOutputImpl
        output.outputFileName = "sob_" + buildType.name + "_v" + versionName + "_" +
                versionCode + ".apk"
    }
    // 打包完成后复制apk到指定文件夹,根目录apkFile
    var outputFileDir = File("${rootDir.absolutePath}/apkFile/")
    if (!outputFileDir.exists()) {
        outputFileDir.mkdir()
    }
    assembleProvider.configure {
        doLast {
            copy {
                variant.outputs.forEach { outFile ->
                    // 只复制文件
                    outFile.outputFile.isFile.ifTrue {
                        copy {
                            from(outFile.outputFile)
                            into(outputFileDir)
                        }
                    }
                }
            }
        }
    }
}

编译release版本之后文件被复制到这里。

相关推荐
_祝你今天愉快10 小时前
Android 事件传递机制:[二] 手写模拟事件分发
android
东坡大表哥17 小时前
【Ubuntu】Ubuntu 20.04 LTS → 22.04 LTS 升级实录
android·linux·ubuntu
不要试图纠正别人19 小时前
IdleHandler 一次都不跑,问题不在主线程忙
android·性能优化
booboosui19 小时前
记忆装不下时谁删:500 条上限与每回合十行索引
android
好的佩奇1 天前
Dart 之 JSON
android·flutter·dart
_祝你今天愉快1 天前
Android 事件传递机制:[一] 事件分发机制详解
android
Digitally2 天前
如何将电脑上的联系人传输到安卓手机(4 种方法)
android
Carson带你学Android2 天前
移动开发周报 :一句话出 App、低端机跑 Gemini、手机写代码
android·ai编程
恋猫de小郭2 天前
站起来,Flutter 要支持真正的 3D Texture 了
android·前端·flutter
FungLeo3 天前
FengCMS · PHP+MySQL 升级实录 · 留言模块的六项问题:未审核进公开列表、旧回复覆盖、批量删除恢复
android·mysql·php·工程实践·fengcms