【问题标题】:How can I get gradle to rename an intermediate file?如何让 gradle 重命名中间文件?
【发布时间】:2014-11-17 22:14:43
【问题描述】:

在我的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 运行之前是什么?

【问题讨论】:

    标签: android build gradle zipalign


    【解决方案1】:

    我能够通过使用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 将是创建的最终(对齐)文件。因此,我让 zipAlign 通过更改 both variant.packageApplication.outputFile variant.zipAlign.outputFile 的名称来使用不同命名的文件。

    给后代的最后一点说明

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

        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.outputFilevariant.outputs[0].zipAlign.outputFile,具体取决于是否启用了zipAlign。
    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2020-03-23
    • 2023-02-11
    • 2017-12-13
    相关资源
    最近更新 更多