Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android Gradle build.gradle What is "it" in it.buildConfigField?

I have search Gradle official website and Android Developer official website, but could not find an answer to this.

  1. In android build.gradle, what is the "it." in the below buildConfigField method? Is this an instance of an object?

I found that moving the buildConfigField method up to defaultConfig lets me use the method without the "it."

  1. What is the difference between the two? Why might I use one method over the other?

    android {
        ...
    
        defaultConfig {
        ...
    
            buildConfigField 'String', 'API_KEY', MyApiKey
        }
        buildTypes {
            release {
                ...
            }
        }
        buildTypes.all {
            ...
        }
        buildTypes.each {
            ...
    
            it.buildConfigField 'String', 'API_KEY', MyApiKey
        }
    }
    
like image 380
ethan Avatar asked Jun 30 '16 23:06

ethan


1 Answers

Gradle build scripts are enhanced Groovy scripts. When you see "it" in a Groovy script or a Gradle build script, it represents the object that was passed into a closure. In your example, the closure is the "{...}" that was passed to "each". So, it iterates through the "buildTypes" collection (or something that is iterable), passes each entry to the closure, and you referenced the passed object as "it". You can change the name of the object passed to the closure, but it's "it" by default.

like image 125
David M. Karr Avatar answered Nov 07 '22 09:11

David M. Karr