gradle中'all'和'each'之间有什么区别?

jay*_*ubi 9 groovy gradle android-studio build.gradle

我正在使用Android Studio和gradle构建脚本.当我要更改某些设置时,我需要all一些字段.但我不是很清楚each之间applicationVariantsall.

例如,我用google搜索一些代码来更改输出apk文件名.代码迭代variant.outputsby eachallby each:

applicationVariants.all { variant ->
   variant.outputs.each { output ->
      output.outputFile = new File(output.outputFile.parent, "MyApp.apk")
   }
}
Run Code Online (Sandbox Code Playgroud)

Opa*_*pal 5

each是一个简单的常规结构。它用于迭代给定的对象,不修改它(原始对象)并在完成后返回(未更改的)对象。看:

assert [1, 2, 3] == [1, 2, 3].each { println it }
Run Code Online (Sandbox Code Playgroud)

whileall是gradle自己添加的一个方法。所以,android插件添加了这个扩展,它有getApplicationVariants方法。由于groovy允许省略get,所以applicationVariants可以使用。现在,提到的扩展使用此类来保存变体集合,该集合扩展了 - this。在后一个all方法中定义的,据我所知它只是一个批处理。

  • 这并没有解释“all”的作用。 (4认同)