Skip to content

Instantly share code, notes, and snippets.

@stefanhoth
Created June 11, 2014 06:56
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stefanhoth/faedeb4c58615992937d to your computer and use it in GitHub Desktop.
Save stefanhoth/faedeb4c58615992937d to your computer and use it in GitHub Desktop.
AndroidDev / gradle: How to rename your output apk during build time to include details like the version.
android {
// .. set up build flavors etc here
//instead of "app-release.apk" this method will rewrite the name to
// "MyCoolCompany-MyGreatProduct-v<defaultConfig.versionName>-RELEASE.apk which is much better suited for archiving and overall handling
// To restore the default behavior just delete the whole block below
applicationVariants.all { variant ->
def apk = variant.outputFile;
def newName;
newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + ".apk");
newName = newName
.replace("-" + variant.buildType.name, "")
.replace(project.name, "MyCoolCompany-MyGreatProduct");
variant.outputFile = new File(apk.parentFile, newName);
if (variant.zipAlign) {
variant.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
}
logger.info('INFO: Set outputFile to ' + variant.outputFile + " for [" + variant.name + "]");
}
}
@stefanhoth
Copy link
Author

Please be aware that your CI probably doesn't know about the name change. It might be necessary to copy the output files after creation rather than renaming them in order to keep your build chain intact.

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