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

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


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.