Skip to content

Instantly share code, notes, and snippets.

@belinwu
Forked from wilik16/build.gradle
Created March 23, 2020 14:08
Show Gist options
  • Save belinwu/63e45b6a390789704a22fc45f99b83ef to your computer and use it in GitHub Desktop.
Save belinwu/63e45b6a390789704a22fc45f99b83ef to your computer and use it in GitHub Desktop.
Archive/Copy debug or release APK / AAB (Android App Bundle) file and/or mapping.txt to a versioned folder
android {
applicationVariants.all { variant ->
variant.outputs.all {
def fileName = "app"
switch (variant.buildType.name) {
case "debug":
fileName = "${appNameDebug}-${variant.versionCode}"
break
case "release":
fileName = "${appNameRelease}-${variant.versionCode}"
break
}
def taskSuffix = variant.name.capitalize()
def copyAPKTask = tasks.create(name: "archiveApk${taskSuffix}", type: Copy) {
description "Archive/copy APK and/or mappings.txt to a versioned folder."
print "Copying APK and/or mappings.txt file from: ${buildDir}\n"
from("${projectDir}") {
if (variant.buildType.name == "debug") {
include "/build/outputs/apk/debug/${fileName}.apk"
} else if (variant.buildType.name == "release") {
include "/build/outputs/mapping/release/mapping.txt"
include "/release/${fileName}.apk"
}
}
into "Archives/${variant.buildType.name}/${variant.versionCode}/${getDate()}"
eachFile { file ->
file.path = file.name
}
includeEmptyDirs = false
}
def copyAABTask = tasks.create(name: "archiveAab${taskSuffix}", type: Copy) {
description "Archive/copy AAB and/or mappings.txt to a versioned folder."
print "Copying AAB and/or mappings.txt file from: ${buildDir}\n"
from("${projectDir}") {
if (variant.buildType.name == "debug") {
include "/build/outputs/bundle/debug/app.aab"
} else if (variant.buildType.name == "release") {
include "/build/outputs/mapping/release/mapping.txt"
include "/release/app.aab"
}
}
into "Archives/${variant.buildType.name}/${variant.versionCode}/${getDate()}"
eachFile { file ->
file.path = file.name
}
includeEmptyDirs = false
rename "app.aab", "${fileName}.aab"
}
def assembleTaskName = "assemble${taskSuffix}"
def bundleTaskName = "bundle${taskSuffix}"
println "Bundle task name: " + bundleTaskName
if (tasks.findByName(assembleTaskName)) {
outputFileName = "${fileName}.apk"
tasks[assembleTaskName].finalizedBy = [copyAPKTask]
}
if (tasks.findByName(bundleTaskName)) {
tasks[bundleTaskName].finalizedBy = [copyAABTask]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment