In my build.gradle file, I have the following:
    applicationVariants.all { variant ->
      def oldFile;
      if (variant.zipAlign) {
        oldFile = variant.outputFile;
      } else {
        oldFile = variant.packageApplication.outputFile;
      }
      def newFile;
      def newFileReplacement = "";
      if (getVersionName().indexOf(gitSha()) != -1) {
          newFileReplacement = newFileReplacement + "-" + getVersionName() + ".apk";
      } else {
          newFileReplacement = newFileReplacement + "-" + getVersionName() + "-" + gitSha() + ".apk"
      }
      newFile = oldFile.name.replace(".apk", newFileReplacement);
      if (variant.zipAlign) {
        variant.zipAlign.outputFile = new File(oldFile.parent, newFile);
      } else {
        variant.packageApplication.outputFile = new File(oldFile.parent, newFile);
      }
    }
When I run the following command: ./gradlew clean assembleRelease, it creates the following .apk files for me:
MyApp-release-1.2.3-e1ad453.apk
MyApp-release-unaligned.apk
(MyApp-release-1.2.3-e1ad453.apk is zipaligned, but the other one is not)
What I want it to create are the following files:
MyApp-release-1.2.3-e1ad453-unaligned.apk
MyApp-release-1.2.3-e1ad453.apk
Where MyApp-release-1.2.3-e1ad453-unaligned.apk is not zip aligned, and MyApp-release-1.2.3-e1ad453.apk is zip aligned, since that seems to be the way that gradle operates by default (i.e. it adds to the name of the file that isn't aligned, rather than adding to the name of the file that is).
Basically, what appears to be happening is that the rename functionality operates before files are actually created, and just specifies the output file I supposedly want. This is fine, but is it possible to also specify what I want the intermediate file MyApp-release-unsigned.apk to be before zipAlign runs?
 
                        
I was able to get gradle to rename the intermediate file by using
variant.packageApplication.outputFile:What's going on here is that if zipAlign is disabled, then
variant.packageApplication.outputFilewill be the resulting file that's created. If zipAlign is enabled, thenvariant.packageApplication.outputFilewill be the intermediate (unaligned) file that's created, andvariant.zipAlign.outputFilewill be the final (aligned) file that's created. So, I'm getting zipAlign to use a differently named file by changing the name of bothvariant.packageApplication.outputFileandvariant.zipAlign.outputFile.One final note for posterity:
In case you make the same mistake I made, I originally tried:
but, this doesn't work because then the unaligned file name and the aligned file name have the same name, and zipAlign will refuse to align a file into itself (i.e. it must have an output file name that is different from it's input file name).