Last active
April 25, 2017 06:58
-
-
Save lifuzu/9553671 to your computer and use it in GitHub Desktop.
Copying APK file in Android Gradle project
This file contains 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
def releasePath = file("${rootDir}/archive/${project.name}") | |
def releaseTask = tasks.create(name: 'release') { | |
group 'Build' | |
description "Assembles and archives all Release builds" | |
} | |
android.applicationVariants.all { variant -> | |
if (variant.buildType.name == 'release') { | |
def build = variant.name.capitalize() | |
def releaseBuildTask = tasks.create(name: "release${build}", type: Zip) { | |
group 'Build' | |
description "Assembles and archives apk and its proguard mapping for the $build build" | |
destinationDir releasePath | |
baseName variant.packageName | |
if (!variant.buildType.packageNameSuffix) { | |
appendix variant.buildType.name | |
} | |
if (variant.versionName) { | |
version "${variant.versionName}_${variant.versionCode}" | |
} else { | |
version "$variant.versionCode" | |
} | |
def archiveBaseName = archiveName.replaceFirst(/\.${extension}$/, '') | |
from(variant.outputFile.path) { | |
rename '.*', "${archiveBaseName}.apk" | |
} | |
if (variant.buildType.runProguard) { | |
from(variant.processResources.proguardOutputFile.parent) { | |
include 'mapping.txt' | |
rename '(.*)', "${archiveBaseName}-proguard_\$1" | |
} | |
} | |
} | |
releaseBuildTask.dependsOn variant.assemble | |
variant.productFlavors.each { flavor -> | |
def flavorName = flavor.name.capitalize() | |
def releaseFlavorTaskName = "release${flavorName}" | |
def releaseFlavorTask | |
if (tasks.findByName(releaseFlavorTaskName)) { | |
releaseFlavorTask = tasks[releaseFlavorTaskName] | |
} else { | |
releaseFlavorTask = tasks.create(name: releaseFlavorTaskName) { | |
group 'Build' | |
description "Assembles and archives all Release builds for flavor $flavorName" | |
} | |
releaseTask.dependsOn releaseFlavorTask | |
} | |
releaseFlavorTask.dependsOn releaseBuildTask | |
} | |
} | |
} | |
/*It creates tasks like the following: | |
release - Assembles and archives all Release builds | |
releaseFree - Assembles and archives all Release builds for flavor Free | |
releaseFreeRelease - Assembles and archives apk and its proguard mapping for the FreeRelease build | |
releasePaid - Assembles and archives all Release builds for flavor Paid | |
releasePaidRelease - Assembles and archives apk and its proguard mapping for the PaidRelease build | |
Content of archive/projectName/packageName-buildType-versionName_versionCode.zip would be: | |
packageName-buildType-versionName_versionCode.apk | |
packageName-buildType-versionName_versionCode-proguard_mapping.txt*/ |
This file contains 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
def publish = project.tasks.create("publishAll") | |
android.applicationVariants.all { variant -> | |
def task = project.tasks.create("publish${variant.name}Apk", Copy) | |
task.from(variant.outputFile) | |
task.into(buildDir) | |
task.dependsOn variant.assemble | |
publish.dependsOn task | |
} | |
/*Now you can call gradle publishAll and it'll publish all you variants. | |
One issue with the mapping file is that the Proguard task doesn't give you a getter to the file location, so you cannot currently query it. I'm hoping to get this fixed.*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/21434554/copying-apk-file-in-android-gradle-project