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版本之后文件被复制到这里。

相关推荐
果子没有六分钟20 小时前
setprop debug.hwui.profile visual_bars有什么作用
android
键来大师1 天前
Android15 源码关闭Selinux
android·java·framework·rk3588
Ro Jace1 天前
三国华容道(横刀立马)移动策略
android·java·数据库·人工智能·深度学习·神经网络·游戏
Digitally1 天前
如何将短信从安卓手机传输到电脑
android
木易 士心1 天前
Android Studio + Gemini 移动开发领域的一次范式转移
android·ide·android studio
GISer_Jing1 天前
跨平台Hybrid App开发实战指南
android·flutter·react native
新之助小锅1 天前
java版连接汇川PLC,发送数据,读取数据,保持重新链接,适用安卓
android·java·python
2501_916007471 天前
iOS性能调试工具终极指南,从系统底层到多端协同的全方位优化实践(2025版)
android·ios·小程序·https·uni-app·iphone·webview
2501_915921431 天前
iOS崩溃日志深度分析与工具组合实战,从符号化到自动化诊断的完整体系
android·ios·小程序·uni-app·自动化·cocoa·iphone