Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between 'all' and 'each' in gradle?

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")
   }
}
like image 896
jayatubi Avatar asked Aug 04 '15 07:08

jayatubi


People also ask

What is Buildscript in build Gradle file?

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.

Why are there multiple build Gradle files?

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.

WHAT IS group in build Gradle?

group signifies the groupId of the project/task that is being worked on.

What is the difference between API and implementation scopes?

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.


2 Answers

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.

like image 196
Opal Avatar answered Oct 06 '22 23:10

Opal


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."

like image 43
BradG Avatar answered Oct 06 '22 23:10

BradG