-
-
Save tonykwok/d7e6297aa57365b6fa2b0b7b15113f06 to your computer and use it in GitHub Desktop.
Archive APK, bundle(AAB) and Proguard map files to artifact directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ext { | |
buildTime = System.currentTimeMillis() | |
} | |
static def getCopyApkTaskName(def variant) { | |
return "copy${variant.name.capitalize()}Apk" | |
} | |
static def getCopyBundleTaskName(def variant) { | |
return "copy${variant.name.capitalize()}Bundle" | |
} | |
static def getCopyMapTaskName(def variant, def target) { | |
return "copy${getTaskNameInfix(variant)}${target.capitalize()}Map" | |
} | |
static def getTaskNameInfix(def variant) { | |
def buildTypeName = variant.buildType.name | |
def productFlavorName = variant.productFlavors.get(0).name | |
def mergedTaskName = productFlavorName.capitalize() + buildTypeName.capitalize() | |
return mergedTaskName | |
} | |
def getNewName(def variant) { | |
def date = new Date(ext.buildTime) | |
def formattedDate = date.format('yyyy.MM.dd-HH.mm') | |
return "${variant.applicationId}".replace('.debug', '').replace('.mock', '') + | |
"-${variant.buildType.name}-${variant.versionName}-${formattedDate}" | |
} | |
def getCopyDestinationPath() { | |
def date = new Date(ext.buildTime) | |
def formattedDate = date.format('yyyy.MM.dd-HH.mm.ss') | |
return "${project.projectDir}/artifact/${formattedDate}" | |
} | |
def addCopyMapTask(def variant, def target) { | |
// zip and copy proguard mapping files | |
if (variant.buildType.minifyEnabled) { | |
task(getCopyMapTaskName(variant, target), type: Zip) { | |
from fileTree(variant.mappingFile.parentFile).files | |
include '**/*.txt' | |
destinationDirectory.set(file(getCopyDestinationPath())) | |
archiveFileName.set(getNewName(variant) + "-${target}-mapping.zip") | |
eachFile { fileCopyDetails -> | |
if (!fileCopyDetails.isDirectory()) { | |
fileCopyDetails.path = fileCopyDetails.name | |
} | |
} | |
doFirst { | |
println "Copy ${archiveFileName.get()} to ${destinationDirectory.get()}" | |
} | |
doLast { | |
println "Copied obfuscation mapping file" | |
} | |
} | |
} | |
} | |
def addCopyApkTask(def variant) { | |
def task = project.tasks.create(getCopyApkTaskName(variant), Copy) | |
task.from(variant.outputs[0].outputFile) | |
task.into(getCopyDestinationPath()) | |
def newName = getNewName(variant) + '.apk' | |
task.rename '.*', newName | |
task.doFirst { | |
println "Copy $newName to $destinationDir" | |
} | |
task.doLast { value -> | |
println "Copied APK file" | |
} | |
} | |
def addCopyBundleTask(def variant) { | |
if (variant.buildType.minifyEnabled) { | |
task(getCopyBundleTaskName(variant), type: Copy) { | |
def infix = getTaskNameInfix(variant).uncapitalize() | |
def path = "${buildDir}/outputs/bundle/${infix}/" | |
def oldName = "app-${variant.productFlavors.get(0).name}-${variant.buildType.name}.aab" | |
def newName = getNewName(variant) + '.aab' | |
from(path) { | |
rename oldName, newName | |
} | |
into file(getCopyDestinationPath()) | |
doFirst { | |
println "Copy $newName to $destinationDir" | |
} | |
doLast { | |
println "Copied bundle file" | |
} | |
} | |
} | |
} | |
afterEvaluate { | |
android.applicationVariants.all { variant -> | |
addCopyApkTask(variant) | |
def copyApkTask = tasks.getByName(getCopyApkTaskName(variant)) | |
variant.assemble.finalizedBy(copyApkTask) | |
if (variant.buildType.minifyEnabled) { | |
addCopyMapTask(variant, 'apk') | |
copyApkTask.finalizedBy(tasks.getByName(getCopyMapTaskName(variant, 'apk'))) | |
} | |
} | |
} | |
tasks.whenTaskAdded { task -> | |
android.applicationVariants.all { variant -> | |
if (variant.buildType.minifyEnabled && | |
task.name == "bundle${getTaskNameInfix(variant)}") { | |
addCopyBundleTask(variant) | |
def copyApkTask = tasks.getByName(getCopyBundleTaskName(variant)) | |
task.finalizedBy(copyApkTask) | |
addCopyMapTask(variant, 'bundle') | |
copyApkTask.finalizedBy(tasks.getByName(getCopyMapTaskName(variant, 'bundle'))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment