I'm working with Android Studio and the Gradle build script. When I was going to change some settings I needed to iterate some fields. But I have not very clear the difference between all
and each
.
For example I googled some code to change the output apk file name. The code iterates the applicationVariants
by all
and the variant.outputs
by each
:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "MyApp.apk")
}
}
buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.
Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build. gradle file for build settings specific to that module.
group signifies the groupId of the project/task that is being worked on.
The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.
each
is a plain groovy construct. It's used to iterate over a given object, does not modify it (original object) and returns the (unchanged) object after it finishes. See:
assert [1, 2, 3] == [1, 2, 3].each { println it }
While all
is a method added by gradle itself. So, android
plugin adds 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. In the latter all
method is defined, as far as I see it's just a batch processing.
The documentation for DomainObjectCollection.all
says it "Executes the given closure against all objects in this collection, and any objects subsequently added to this collection."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With