Skip to content

Instantly share code, notes, and snippets.

@uOOOO
Last active April 3, 2019 13:53
Show Gist options
  • Select an option

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

Select an option

Save uOOOO/fbc551f9636700027b905f6c9f73e251 to your computer and use it in GitHub Desktop.
Archive APK and Proguard map files to artifact directory
def deleteBuildDir(def variant) {
delete variant.packageApplicationProvider.get().outputDirectory
}
def setBuildTime() {
project.ext.set("buildTime", System.currentTimeMillis())
}
def getBuildTime() {
def date = new Date(project.ext.buildTime)
return date.format('yyyy.MM.dd-HH.mm.ss')
}
def createFileName(def variant) {
return "${variant.applicationId}".replace('.debug', '').replace('.mock', '') +
"-${variant.buildType.name}-${variant.versionName}-${getBuildTime()}"
}
def createApkName(def variant) {
return createFileName(variant) + '.apk'
}
def createMapZipName(def variant) {
return createFileName(variant) + '-mapping.zip'
}
def zipMappingFiles(def variant) {
// zip and copy proguard mapping files
if (variant.getBuildType().isMinifyEnabled()) {
def dest = variant.packageApplicationProvider.get().outputDirectory.path + '/' + createMapZipName(variant)
ant.zip(destfile: dest) {
fileset(dir: variant.mappingFile.parentFile) {
include(name: '**/*.txt')
}
}
}
}
def createArtifactsPath() {
return "${project.projectDir}/artifacts/${getBuildTime()}"
}
def copyArtifacts(def variant) {
println(variant.packageApplicationProvider.get().outputDirectory)
copy {
from variant.packageApplicationProvider.get().outputDirectory
into createArtifactsPath()
include '*.apk', '*.zip'
}
}
def registerArtifactTasks() {
android.applicationVariants.all { variant ->
setBuildTime()
variant.outputs.all {
deleteBuildDir(variant)
outputFileName = createApkName(variant)
getAssembleProvider().configure {
doLast {
zipMappingFiles(variant)
copyArtifacts(variant)
}
}
}
}
}
ext {
registerArtifactTasks = this.&registerArtifactTasks
}
apply from: 'archiveApk.gradle'
android {
......
afterEvaluate {
registerArtifactTasks()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment