Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wilik16
Last active November 8, 2022 12:52
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wilik16/ebba3fc09cc78c8d86780db807fcb48c to your computer and use it in GitHub Desktop.
Save wilik16/ebba3fc09cc78c8d86780db807fcb48c 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