gradle中的'all'和'each'有什么区别?
What is the difference between 'all' and 'each' in gradle?
我正在使用 Android Studio 和 Gradle 构建脚本。当我要更改一些设置时,我需要迭代一些字段。但是我还不是很清楚all
和each
的区别。
例如,我用谷歌搜索了一些代码来更改输出 apk 文件名。代码通过 all
迭代 applicationVariants
并通过 each
:
迭代 variant.outputs
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "MyApp.apk")
}
}
each
是一个普通的 groovy 结构。它用于迭代给定的对象,不修改它(原始对象)并且 returns 完成后的(未更改的)对象。参见:
assert [1, 2, 3] == [1, 2, 3].each { println it }
而all
是gradle自己添加的方法。因此,android
插件添加了 this extension, which has getApplicationVariants
method. Since groovy allows to omit get
, just applicationVariants
can be used. Now, the mentioned extension uses this class to keep the collection of variants, which extends - this。在后面定义了all
方法,据我看只是一个批处理。
DomainObjectCollection.all
的 documentation 表示“针对此集合中的所有对象执行给定的闭包, 以及随后添加到此集合中的任何对象。 “
我正在使用 Android Studio 和 Gradle 构建脚本。当我要更改一些设置时,我需要迭代一些字段。但是我还不是很清楚all
和each
的区别。
例如,我用谷歌搜索了一些代码来更改输出 apk 文件名。代码通过 all
迭代 applicationVariants
并通过 each
:
variant.outputs
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "MyApp.apk")
}
}
each
是一个普通的 groovy 结构。它用于迭代给定的对象,不修改它(原始对象)并且 returns 完成后的(未更改的)对象。参见:
assert [1, 2, 3] == [1, 2, 3].each { println it }
而all
是gradle自己添加的方法。因此,android
插件添加了 this extension, which has getApplicationVariants
method. Since groovy allows to omit get
, just applicationVariants
can be used. Now, the mentioned extension uses this class to keep the collection of variants, which extends - this。在后面定义了all
方法,据我看只是一个批处理。
DomainObjectCollection.all
的 documentation 表示“针对此集合中的所有对象执行给定的闭包, 以及随后添加到此集合中的任何对象。 “