Android 如何让gradle重命名中间文件?
在我的Android 如何让gradle重命名中间文件?,android,build,gradle,zipalign,Android,Build,Gradle,Zipalign,在我的build.gradle文件中,我有以下内容: applicationVariants.all { variant -> def oldFile; if (variant.zipAlign) { oldFile = variant.outputFile; } else { oldFile = variant.packageApplication.outputFile; } def n
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 assembleerelease
,它会为我创建以下.apk文件:
MyApp-release-1.2.3-e1ad453.apk
MyApp-release-unaligned.apk
(MyApp-release-1.2.3-e1ad453.apk
已zipaligned,但另一个未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
将是创建的最终(对齐)文件。因此,我通过更改variant.packageApplication.outputFile
和variant.zipAlign.outputFile
的名称,让zipAlign使用不同名称的文件
留给后代的最后一句话:
如果你犯了与我相同的错误,我最初尝试:
variant.zipAlign.outputFile = new File(oldFile.parent, newFile);
variant.packageApplication.outputFile = new File(oldFile.parent, newFile);
但是,这不起作用,因为未对齐的文件名和对齐的文件名具有相同的名称,zipAlign将拒绝将文件对齐到自身中(即,它必须具有与其输入文件名不同的输出文件名)。非常有用。快速评论:在Android Studio 1.0中,我们必须循环使用变量输出,而不仅仅是变量:applicationVariants.all{variant->variant.outputs.each{output->
。事实上,您可以简单地引用variant.outputs[0].outputFile
,它将等于variant.outputs[0].packageApplication.outputFile
或variant.outputs[0].zipAlign.outputFile
取决于是否启用zipAlign。