Reputation: 6209
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?
Upvotes: 1
Views: 900
Reputation: 6209
I was able to get gradle to rename the intermediate file by using variant.packageApplication.outputFile
:
// This next part renames the .apk file to include the version number
// as well as the git sha for the commit from which it was built.
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) {
// Set the name of the final output file
variant.zipAlign.outputFile = new File(oldFile.parent, newFile);
// Set the name of the intermediate file (input to zipAlign) to contain version
// information, too
def oldIntermediateFile = variant.packageApplication.outputFile
def newIntermediateFile = variant.packageApplication.outputFile.name.replace(".apk", newFileReplacement)
variant.packageApplication.outputFile = new File(oldIntermediateFile.parent, newIntermediateFile)
} else {
// Set the name of the final output file (no intermediate file)
variant.packageApplication.outputFile = new File(oldFile.parent, newFile);
}
}
What's going on here is that if zipAlign is disabled, then variant.packageApplication.outputFile
will be the resulting file that's created. If zipAlign is enabled, then variant.packageApplication.outputFile
will be the intermediate (unaligned) file that's created, and variant.zipAlign.outputFile
will be the final (aligned) file that's created. So, I'm getting zipAlign to use a differently named file by changing the name of both variant.packageApplication.outputFile
and variant.zipAlign.outputFile
.
One final note for posterity:
In case you make the same mistake I made, I originally tried:
variant.zipAlign.outputFile = new File(oldFile.parent, newFile);
variant.packageApplication.outputFile = new File(oldFile.parent, newFile);
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).
Upvotes: 3