如何让 gradle 重命名中间文件



在我的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是拉链对齐的,但另一个不是)

我希望它创建的是以下文件:

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 默认的运行方式(即它添加到未对齐的文件的名称,而不是添加到文件名中)。

基本上,似乎正在发生的事情是重命名功能在实际创建文件之前运行,并且只指定我想要的输出文件。这很好,但是是否可以在zipAlign运行之前指定我希望中间文件MyApp-release-unsigned.apk是什么?

我能够让 gradle 使用 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);
      }
    }

这里发生的事情是,如果 zipAlign 被禁用,那么variant.packageApplication.outputFile将是创建的结果文件。如果启用了 zipAlign,则variant.packageApplication.outputFile将是创建的中间(未对齐)文件,variant.zipAlign.outputFile将是创建的最终(对齐)文件。因此,我正在通过更改variant.packageApplication.outputFilevariant.zipAlign.outputFile的名称来获得zipAlign以使用不同名称的文件。

给后人的最后一点说明

如果你犯了和我一样的错误,我最初尝试过:

    variant.zipAlign.outputFile = new File(oldFile.parent, newFile);
    variant.packageApplication.outputFile = new File(oldFile.parent, newFile);

但是,这不起作用,因为未对齐的文件名和对齐的文件名具有相同的名称,并且zipAlign将拒绝将文件对齐到自身中(即它必须具有与其输入文件名不同的输出文件名)。

最新更新