Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Created August 27, 2015 07:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/8846df24da30d1e75025 to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/8846df24da30d1e75025 to your computer and use it in GitHub Desktop.
Gradle file for copying release ready apk to desired directory.
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"
}
android.applicationVariants.all { variant ->
//Mapping file for proguard path for each variant
def mappingFile = variant.variantData.mappingFile
//Right now only for release type (not debuggable and zipAlignEnabled)
//Proguard must be enabled to get Mapping file, In future I will eliminate this requirement
if (!variant.buildType.debuggable && variant.variantData.zipAlignEnabled && mappingFile != null) {
//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")
//RELEASE_PATH property or project's dir\release.
def releasePath = getReleasePath()
//Get Project Name to make directory
def projectName = project.parent.name
//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")
//Mapping Directory: Will be part of original Directory
File mapping = new File("$filePath/$dirName/Mapping")
//APK File To copy
def apk = variant.outputs.outputFile
//Main Container task.
def publishTask = project.tasks.create("publish${variant.name}")
//Task for copying mapping file
def copyMapping = project.tasks.create("Copy${variant.name}Mapping", Copy) {
from mappingFile
into mapping
}
//Task for copying apk file
def copyApk = project.tasks.create("copy${variant.name}Apk", Copy) {
from apk
into release
}
//Task for Creating directory
def prepare = project.tasks.create("Prepare${variant.name}Directories") {
mapping.mkdirs()
release.mkdirs()
}
//Now dependency tree: First APK should be created, which will give us mapping and apk files to copy.
prepare.dependsOn variant.assemble
//Then create directories.
copyMapping.dependsOn prepare
//first copy mapping file, then apk file
copyApk.dependsOn copyMapping
//Now just run Publish Task and your apks will be copied easily.
publishTask.dependsOn copyApk
}
}
@PrashamTrivedi
Copy link
Author

//TODO:

  1. Put Publish task out of applicationVariants block. It will stop creating all the folders all the time.
  2. Add Grouping and Description of each task
  3. Add Google Play publishing code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment