在我的 build.gradle
文件中,我有以下内容:
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);
}
}
当我运行以下命令时:./gradlew clean assembleRelease
,它会为我创建以下 .apk 文件:
MyApp-release-1.2.3-e1ad453.apk
MyApp-release-unaligned.apk
(MyApp-release-1.2.3-e1ad453.apk
是 zipaligned,但另一个不是)
我想要它创建的是以下文件:
MyApp-release-1.2.3-e1ad453-unaligned.apk
MyApp-release-1.2.3-e1ad453.apk
MyApp-release-1.2.3-e1ad453-unaligned.apk
不是 zip 对齐的,MyApp-release-1.2.3-e1ad453.apk
是 zip对齐,因为这似乎是 gradle 默认操作的方式(即它添加到未对齐的文件的名称,而不是添加到 是 的文件的名称)。
基本上,似乎正在发生的事情是重命名功能在实际创建文件之前 运行,并且只是指定我想要的输出文件。这很好,但是否也可以指定我希望中间文件 MyApp-release-unsigned.apk
在 zipAlign 运行之前是什么?
最佳答案
我能够使用 variant.packageApplication.outputFile
让 gradle 重命名中间文件:
// 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);
}
}
这里发生的事情是,如果 zipAlign 被禁用,那么 variant.packageApplication.outputFile
将是创建的结果文件。如果启用了 zipAlign,则 variant.packageApplication.outputFile
将是创建的中间(未对齐)文件,而 variant.zipAlign.outputFile
将是创建的最终(对齐)文件。因此,我通过更改 both variant.packageApplication.outputFile
and variant 的名称让 zipAlign 使用不同名称的文件.zipAlign.outputFile
.
给后代的最后一个说明:
如果你犯了和我一样的错误,我最初尝试过:
variant.zipAlign.outputFile = new File(oldFile.parent, newFile);
variant.packageApplication.outputFile = new File(oldFile.parent, newFile);
但是,这不起作用,因为未对齐的文件名和对齐的文件名具有相同的名称,并且 zipAlign 将拒绝将文件对齐到自身(即它必须有一个不同于它的输出文件名输入文件名)。
关于android - 如何让 gradle 重命名中间文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26982991/