Skip to content

Instantly share code, notes, and snippets.

@uOOOO
Created September 6, 2017 04:47
Show Gist options
  • Select an option

  • Save uOOOO/9f9e636d05551a4b2a81e547efa4a7c4 to your computer and use it in GitHub Desktop.

Select an option

Save uOOOO/9f9e636d05551a4b2a81e547efa4a7c4 to your computer and use it in GitHub Desktop.
Copy apk to specific path after assemble
task zipUnitTestReport(type: Zip) {
from project.buildDir
include "reports/**", "test-results/**"
archiveName "unitTestReport.${extension}"
}
def setBuildTime() {
project.ext.set("buildTime", System.currentTimeMillis())
}
static def getCopyApkTaskName(def variant) {
return "copy${variant.name.capitalize()}Apk"
}
static def getCopyMapTaskName(def variant) {
return "copy${getAssembleTaskInfix(variant)}Map"
}
def getCopyApkName(def variant) {
def date = new Date(project.ext.buildTime)
def formattedDate = date.format('yyyy.MM.dd-HH.mm')
return "${variant.applicationId}".replace('.debug', '').replace('.mock', '') +
"-${variant.buildType.name}-${variant.versionName}-${formattedDate}"
}
static def getAssembleTaskInfix(def variant) {
def buildTypeName = variant.buildType.name
def productFlavorName = variant.productFlavors.get(0).name
def mergedTaskName = productFlavorName.capitalize() + buildTypeName.capitalize()
return mergedTaskName
}
def getDistributionPath() {
def date = new Date(project.ext.buildTime)
def formattedDate = date.format('yyyy.MM.dd-HH.mm.ss')
return "${project.projectDir}/artifact/${formattedDate}"
}
def copyMap(def variant) {
// zip and copy proguard mapping files
if (variant.getBuildType().isMinifyEnabled()) {
task(getCopyMapTaskName(variant), type: Zip) {
from fileTree(variant.mappingFile.parentFile).files
include '**/*.txt'
destinationDir file(getDistributionPath())
archiveName = getCopyApkName(variant) + '-mapping.zip'
eachFile { fileCopyDetails ->
if (!fileCopyDetails.isDirectory()) {
fileCopyDetails.path = fileCopyDetails.name
}
}
doFirst {
println "Copy ${archiveName} to ${getDistributionPath()}"
}
doLast {
println "Copied map"
}
}
}
}
def copyApk(def variant) {
def task = project.tasks.create(getCopyApkTaskName(variant), Copy)
task.from(variant.outputs[0].outputFile)
task.into(getDistributionPath())
def targetName = getCopyApkName(variant) + '.apk'
task.rename '.*', targetName
task.doFirst {
println "Copy ${source.singleFile.name} to $destinationDir"
}
task.doLast { value ->
println "Copied $targetName"
}
}
android.applicationVariants.all { variant ->
setBuildTime()
copyApk(variant).doLast { copyMap(variant) }
}
def copyDistributions() {
android.applicationVariants.all { variant ->
tasks.getByName("assemble${getAssembleTaskInfix(variant)}").doLast {
try {
tasks.getByName(getCopyApkTaskName(variant)).execute()
tasks.getByName(getCopyMapTaskName(variant)).execute()
} catch (all) {
// quietly
}
}
}
}
ext {
copyDistributions = this.&copyDistributions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment