buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.5.6' } } task wrapper(type: Wrapper) { gradleVersion = '1.7' } apply plugin: 'android' android { compileSdkVersion 17 buildToolsVersion "17.0.0" buildTypes { debug { packageNameSuffix ".debug" versionNameSuffix "-SNAPSHOT" } } } android.applicationVariants.all { variant -> variant.assemble.doLast { rename_and_moveout_apk(variant) } } def rename_and_moveout_apk(targetVariant) { // get hash of current commit new ByteArrayOutputStream().withStream { os -> def result = exec { executable = 'git' args = ['rev-parse', '--short', 'HEAD'] standardOutput = os } project.ext.gitHash = os.toString().trim(); } // replace output apk name to <product>-<version>-<buildtype>-<githash>.apk def versionSuffix = targetVariant.buildType.versionNameSuffix ? targetVariant.buildType.versionNameSuffix : "" def versionName = targetVariant.mergedFlavor.versionName + versionSuffix + "-${gitHash}"; if (targetVariant.zipAlign) { def originZipAlignedApkFile = targetVariant.outputFile; def renameZipAlignedApkFile = originZipAlignedApkFile.name.replace(targetVariant.buildType.name, versionName); copy { from "$originZipAlignedApkFile" into "$rootProject.projectDir/out" rename ("$originZipAlignedApkFile.name", "$renameZipAlignedApkFile") } } def originApkFile = targetVariant.packageApplication.outputFile; def renameApkFile = originApkFile.name.replace(targetVariant.buildType.name, versionName); copy { from "$originApkFile" into "$rootProject.projectDir/out" rename ("$originApkFile.name", "$renameApkFile") } } clean.doLast { project.delete "$rootProject.projectDir/out" }