Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Created June 4, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/4c69095c0c53a13fccc73bf5447b07cf to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/4c69095c0c53a13fccc73bf5447b07cf to your computer and use it in GitHub Desktop.
Deploy scrip
import java.text.DateFormat
import java.text.SimpleDateFormat
import org.gradle.internal.os.OperatingSystem;
task(debugSomething) {
doLast {
println OperatingSystem.properties
}
}
debugSomething.group = "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"
deploy.group = 'Deploying'
android.applicationVariants.all { variant ->
println variant.name
def variantName = variant.name
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 = 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")
//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${variant.name}Apk", Copy) {
from apk
into release
rename {
it.replace('-release', '').replace('app', "$project.parent.name(${getDateTime()})")
}
}
copyApk.description = "Copies Release APKs for ${variant.name}"
copyApk.group = 'Deploying'
//Task for Creating directory
def prepare = project.tasks.create("Prepare${variant.name}Directories") {
release.mkdirs()
println release.path
}
prepare.description = "Prepares directories for ${variant.name}"
prepare.group = '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${variant.name}Mapping", Copy) {
//Task for Copying Mapping file for release
from mapping
into mappingTree
rename {
"$variant.name-".concat(it).replace("Release", "")
}
}
copyMappingTask.description = "Copies proguard Mapping File for ${variant.name}"
copyMappingTask.group = 'Deploying'
def prepareMapping = project.tasks.create("Prepare${variant.name}MappingDirs") {
mappingTree.mkdirs()
}
prepareMapping.description = "Prepares Mapping directories for ${variant.name}"
prepareMapping.group = '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