Skip to content

Instantly share code, notes, and snippets.

@sddamico
Created October 24, 2014 00:37
Show Gist options
  • Save sddamico/75747761a4f738afc308 to your computer and use it in GitHub Desktop.
Save sddamico/75747761a4f738afc308 to your computer and use it in GitHub Desktop.
Rename Android APK's to Something Useful
/* put this in your application's build.gradle after the android {} block
* output is in this format: "<projectName>-<buildType>-v<versionName>-<versionCode>.apk"
* e.g. "materiyolo-debug-v5.0.0-12345.apk"
* also, supports injecting jenkins ci's build number into apk, if available
* e.g. "materiyolo-debug-v5.0.0-12345-b6789.apk"
* you must specify your versionName and versionCode in your build.gradle for this to work
* see: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Manifest-entries */
// change this to whatever your app's name is...
project.archivesBaseName = 'materiyolo'
android.applicationVariants.all { variant ->
renameApk(variant)
}
def renameApk(variant) {
def apkPath = variant.packageApplication.outputFile.parentFile
def baseName = project.archivesBaseName
// if it's a release build, the type is implicit, imo, so add it otherwise
if (variant.name != 'release') {
baseName += "-${variant.buildType.name}"
}
// add version name and version code
baseName += "-v${variant.mergedFlavor.versionName}-${variant.mergedFlavor.versionCode}"
// if built on jenkins ci, add jenkins build number:
def buildNumber = System.getenv('BUILD_NUMBER')
if (buildNumber && buildNumber.size() > 0) {
baseName += "-b${buildNumber}"
}
// if the variant will not be zipAligned, specify that
if (!variant.zipAlign) {
baseName += '-unaligned'
}
// set the output file
variant.outputFile = new File(apkPath, "${baseName}.apk");
}
@carlemil
Copy link

carlemil commented Mar 1, 2016

Same for me:

  • What went wrong:
    A problem occurred configuring project ':client'.

    Could not find property 'packageApplication' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@267d9cc0.

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