Gradle复制并在循环中重命名

时间:2017-11-12 20:24:25

标签: android gradle build.gradle

我正在使用Android Studio 3。

对于每种口味,我想复制mapping.txt并重命名。

我的Gradle任务:

task deployApks(type: Copy) {
        android.applicationVariants.all { variant ->
            if (variant.buildType.name == 'release') {
            variant.outputs.all {
                  def flavor = variant.productFlavors.name.get(0)

                  def dirApk = System.getProperty("user.dir") + '/app/build/' + flavor + '/release/'
                  def dirMapping = System.getProperty("user.dir") + '/app/build/outputs/mapping/' + flavor + '/release/'

                  //copy apk and mapping.txt
                  from dirApk, dirMapping
                  include '*-release.apk', 'mapping.txt'
                  into dirDeploy

                  //rename mapping.txt
                  from dirDeploy
                  include 'mapping.txt'
                  into dirDeploy
                  rename 'mapping.txt', 'mapping-' + flavor + '.txt'
                  println("Rename mapping.txt tomapping-" + flavor + ".txt")
             }
        }
    }
}

我在部署目录中想要的内容:

  • flavor1-release.apk

  • 映射-flavor1.txt

  • flavor2-release.apk

  • 映射-flavor2.txt

我得到了什么:

  • flavor1-release.apk

  • 映射-flavor1.txt

  • flavor2-release.apk

gradle copy是否异步。?

看起来重命名是在所有副本之后完成的。

2 个答案:

答案 0 :(得分:0)

你可能不知道但是gradle build包含3个阶段:

  • 初始化
  • 构造
  • 执行

在第二阶段配置任务(包括您使用的Copy)操作(任务是按顺序运行的操作集合)。如果你把循环放在任务的主体中,那么前一次迭代将获胜。最简单的方法是将任务更改为以下内容(手动复制):

task deployApks {
  doLast {
    android.applicationVariants.all { variant ->
      if (variant.buildType.name == 'release') {
      variant.outputs.all {
        def flavor = variant.productFlavors.name.get(0)

        def dirApk = System.getProperty("user.dir") + '/app/build/' + flavor + '/release/'
        def dirMapping = System.getProperty("user.dir") + '/app/build/outputs/mapping/' + flavor + '/release/'

        //copy apk and mapping.txt
        copy {
          from dirApk, dirMapping
          include '*-release.apk', 'mapping.txt'
          into dirDeploy
          rename 'mapping.txt', 'mapping-' + flavor + '.txt'
        }
       }
      }
    }
  }
}

如果这解决了问题 - (您不需要执行缓存),您可以使用。否则,您需要适当地配置Copy任务,甚至编写自定义任务。

答案 1 :(得分:0)

我认为密钥是variant.assemble.doLast。我先制作所有apk文件,完成后再运行doLast任务复制并重命名mapping.txt文件。

第4级(兼容)

// Map for the version code that gives each ABI a value.
ext.abiCodes = ['armeabi':3, 'armeabi-v7a':4, 'arm64-v8a':5, 'mips':6, 'x86':7, 'x86_64':8].withDefault {0}

import com.android.build.OutputFile
android.applicationVariants.all { variant ->
    def customName = ""
    if (project.hasProperty('projectName')) {
        customName = projectName
    } else {
        customName = project.name
    }

    def flavorName = variant.productFlavors[0].name
    def buildType = variant.variantData.variantConfiguration.buildType.name
    def abiVersionCode = ""
    def abiName = ""
    def fileName = ""
    def mappingDir = "${rootDir}/build/outputs/mapping/${flavorName}/${buildType}"


    variant.outputs.all { output ->
        abiVersionCode = project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))
        if (abiVersionCode != null) {
            output.versionCodeOverride = abiVersionCode * 1000 + variant.versionCode
        }
        abiName = output.getFilter(OutputFile.ABI)
        if (abiName == null) {
            abiName = "universal"
            output.versionCodeOverride = 1 * 1000 + variant.versionCode
        }

        fileName = customName + "_" + variant.versionName + "-" + flavorName + "-" + abiName + "-" + buildType + "-" + output.versionCode
        outputFileName = new File("${fileName}.apk")
    }


    variant.assemble.doLast {
        variant.outputs.all { output ->
            if (buildType == "release") {
                def mappingFile = "${mappingDir}/mapping.txt"
                def newMappingName = "${fileName}-mapping.txt"

                delete "${output.outputFile.parent}/${newMappingName}"
                copy {
                    from "${mappingFile}"
                    into "${rootDir}"
                    rename { String mappingName ->
                        "${output.outputFile.parent}/${newMappingName}"
                    }
                }
            }

            delete "${output.outputFile.parent}/output.json"
        }
    }
}