文件名:build.gradle (:app) android { ... // 打包完成复制一份到apk文件夹内 this.project.afterEvaluate { project -> project.tasks.each { task -> if (task.toString().contains("assemble")) { task.doLast { android.applicationVariants.all { variant -> def simpleName = "app-${variant.buildType.name}.apk" // 默认生成apk的文件夹 def workFolder = "${project.getProjectDir().path}/build/outputs/apk/${variant.buildType.name}" // 定义目标文件夹 def destFolder = new File("${project.getProjectDir().path}/apk/${variant.buildType.name}") try { if (!destFolder.exists()) { destFolder.mkdir() } copy { from "${workFolder}/${simpleName}" into "${destFolder}/" rename { "property_v${versionName}_${versionCode}_${variant.buildType.name}.apk" } } } catch (Exception e) { print e } } } } } } }
这样在每次assembleXXXX任务的时候, 或者Build的时候, 都会在打包完成后复制一份到/app/apk/下面
通过Gradle一键打包多渠道apk至指定文件夹的方法
方法一:
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" } } } }
gradle配置打包后 Copy APK到指定路径
配置文件:copy_apk.gradle
先看图:
配置位置:app的build.gradle(这里使用相对路径,copy_apk.gradle文件位于项目根目录下)
添加依赖:
apply from: "../copy_apk.gradle"
copy_apk.gradle 源码如下:
project.archivesBaseName = "XXXApp" static def releaseTime() { return new Date().format("HHmmss")//yyyyMMdd_HHmmss } android.applicationVariants.all { variant -> variant.outputs.all { if (outputFileName.endsWith('.apk')) { //这里使用之前定义apk文件名称 // outputFileName = "${project.archivesBaseName}_v${variant.productFlavors[0].versionName}_${variant.productFlavors[0].versionCode}_${variant.productFlavors[0].name}_${releaseTime()}_${variant.buildType.name}.apk" outputFileName = "${project.archivesBaseName}_${versionCode}_v${versionName}_${releaseTime()}_${name}.apk" } } //复制到根目录下的output文件夹 File desFilePath = new File("${rootDir}/output") //删除output目录 delete desFilePath //API 'variant.getAssemble()' is obsolete and has been replaced with 'variant.getAssembleProvider()'. //It will be removed in version 7.0 of the Android Gradle plugin. //编译完成后将apk复制到指定目录 // variant.assemble.doLast { // variant.outputs.all { // try { // //判断文件夹是否存在 // if (!desFilePath.exists()) { // desFilePath.mkdir() // } // //将编译好的apk 复制到output目录 // copy { // from outputFile // into desFilePath // include '**/*.apk' // } // } catch (Exception e) { // e.printStackTrace() // } // } // } //used variant.getAssembleProvider(). //(variant.assembleProvider.configure|variant.assembleProvider.get.doLast) //https://stackoverflow.com/questions/54193510/while-android-studio-updated-to-v3-3-getting-api-variant-getassemble-is-obso variant.assembleProvider.configure { it.doLast { variant.outputs.all { try { //判断文件夹是否存在 if (!desFilePath.exists()) { desFilePath.mkdir() } //将编译好的apk 复制到output目录 copy { from outputFile into desFilePath include '**/*.apk' } } catch (Exception e) { e.printStackTrace() } } } } }