Android Studio 通过Gradle一键打包多渠道apk至指定文件夹的方法

当前环境:Android studio arctic fox

gradle版本:6.8.2

android gradle plugin 版本:4.2.2

方法一:

   android.applicationVariants.all { variant ->

        // 打包完成后输出路径
        def name = "app"+
                "_" + variant.flavorName +
                "_" + variant.buildType.name +
                "_" + variant.versionName +
                "_" + new Date().format('yyyy-MM-dd') + ".apk"

        def path = "../../../../../apk/" //相当于路径 app/apk/
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                //指定路径输出
                output.outputFileName = new File(path, name)
            }
        }

    }

方法二:

android.applicationVariants.all { variant ->

        variant.outputs.forEach {
            it.outputFileName = "${variant.productFlavors[0].name}_v${defaultConfig.versionName}_${new Date().format('yyyy-MM-dd')}.apk"
        }

        //  打包完成后复制到的目录
        def outputFileDir = "${project.projectDir.absolutePath}/apk/"

        // 打包完成后做的一些事,复制apk到指定文件夹
        variant.assemble.doLast {
            File out = new File(outputFileDir)
            copy {
                variant.outputs.forEach { file ->
                    //复制到指定文件夹
                    //copy {
                    //      from file.outputFile
                    //      into out
                    //}
                      //移动到指定文件夹
                      ant.move file: file.outputFile,
                        todir: "${project.rootDir}/apk"
                }
            }
        }

       
 }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值