Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Deploy scrip
import java.text.DateFormat
import java.text.SimpleDateFormat
import org.gradle.internal.os.OperatingSystem;
task(debugSomething) {
doLast {
} = "Debugging"
def getReleasePath() {
//Define RELEASE_PATH anywhere(in properties file) and your apk + mapping file will be copied there.
return hasProperty('RELEASE_PATH') ? RELEASE_PATH : "${project.rootDir}\\Release"
def getDateTime() {
DateFormat df = new SimpleDateFormat("hh-mm a")
return df.format(new Date())
def deploy = project.tasks.create("deployReleases")
deploy.description = "Deploy APK of Release" = 'Deploying'
android.applicationVariants.all { variant ->
def variantName =
String variantCode = variant.mergedFlavor.versionCode
String variantVersionName = variant.mergedFlavor.versionName
def projectRootPath = "$project.rootDir" + "/Release/"
if (!variant.buildType.debuggable && (!variantName.contains("Debug")) && variant.buildType.zipAlignEnabled) {
variant.outputs.each { op ->
//Get Current Date and split it into yyyy,MM,dd to create directory structure reflecting to it.
Date date = new Date()
def y = date.format("yyyy")
def m = date.format("MM")
def d = date.format("dd")
def isMapping = variant.mappingFile != null
println variant.mappingFile
//RELEASE_PATH property or project's dir\release.
def releasePath = getReleasePath()
println releasePath
//Get Project Name to make directory
def projectName =
//File path will be generated Like : ReleasePath/ProjectName/Y/M/D
//e.g.: D:/Codes/Release/ShareChannels/2015/07/02
def filePath = "$releasePath/$projectName/$y/$m/$d"
//Directory name by version, where apk and mapping will be stored
def dirName = "$variant.versionName - ($variant.versionCode)"
File release = new File("$filePath/$dirName")
//APK File To copy
// def apk = variant.outputs.outputFile
// def apk= ${buildDir}.join("outputs", "apk",${variant.dirName},${variant.outputs.first().outputFileName})
def dir = variant.dirName.replace('/', File.separator)
def apk = "${buildDir}${File.separator}outputs${File.separator}apk${File.separator}${dir}${File.separator}${variant.outputs.first().outputFileName}"
//Task for copying apk file
def copyApk = project.tasks.create("deploy${}Apk", Copy) {
from apk
into release
rename {
it.replace('-release', '').replace('app', "$${getDateTime()})")
copyApk.description = "Copies Release APKs for ${}" = 'Deploying'
//Task for Creating directory
def prepare = project.tasks.create("Prepare${}Directories") {
println release.path
prepare.description = "Prepares directories for ${}" = 'Deploying'
//Now dependency tree: First APK should be created, which will give us mapping and apk files to copy.
prepare.dependsOn variant.assemble
if (isMapping) {
File mappingTree = new File("$filePath/$dirName/Mapping")
println mappingTree
def mapping = variant.mappingFile
println mapping
def copyMappingTask = project.tasks.create("deploy${}Mapping", Copy) {
//Task for Copying Mapping file for release
from mapping
into mappingTree
rename {
"$".concat(it).replace("Release", "")
copyMappingTask.description = "Copies proguard Mapping File for ${}" = 'Deploying'
def prepareMapping = project.tasks.create("Prepare${}MappingDirs") {
prepareMapping.description = "Prepares Mapping directories for ${}" = 'Deploying'
prepareMapping.dependsOn prepare
//Now copy mapping
copyMappingTask.dependsOn prepareMapping
//then apk file
copyApk.dependsOn copyMappingTask
} else {
copyApk.dependsOn prepare
//Now just run Publish Task and your apks will be copied easily.
deploy.dependsOn copyApk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.