• Gradle is not a language?

    No, it’s just a project builder using the Groovy language. And the internal code is execution code, and actually run, not just configuration files. And makes extensive use of closures.

  • What is a closure?

    Closures are methods that can be passed. Such as:

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://jitpack.io'}}}Copy the code

This is a common configuration file in Android, but it actually looks like this

allprojects(new Action<Project>() {
    @Override
    void execute(Project project) {
        project.repositories {
            google()
            jcenter()
            mavenCentral()
            maven { url 'https://jitpack.io'}}}})Copy the code

We passed the Action as a closure, and the lamada expression actually passes a closure, so this is a simple example.

Now let’s look at the next paragraph

dependencies {
        classpath "Com. Android. Tools. Build: gradle: 4.2.0 - alpha03"
}
Copy the code

What is this? But those of you who have written about Kotlin should be familiar with it. {} actually has a hidden object it, so we can parse the above code as follows

Dependencies ({it-> it. Add ('classpath' , "Com. Android. Tools. Build: gradle: 4.2.0 - alpha03")})Copy the code

DependencyHandly is one of these callbacks. Closures can pass methods and objects and even hide method names, as long as there is no ambiguity.

It’s pretty clear at this point that the principle is method calls.

Now look at the configuration file under the subpackage

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx. Core: the core - KTX: 1.3.0'
    implementation 'androidx. Appcompat: appcompat: 1.1.0'
    }
Copy the code
  • What’s the difference between implementation and compile?

    Compile depends on pass, implementation doesn’t. For example, we introduced a module we wrote, Compile Project (“: HTTP “), which is a web request that we encapsulate, using OkHttp, Retrofit, room, coroutine extension…. After we use compile, our main package can use the library introduced by the submodule. This leads to the introduction of multiple identical libraries. We introduced the same library for multiple submodules, which resulted in invalidation of the cache every time we compiled to these libraries. Recompile, because Grooovy didn’t check whether your other packages used the library you passed, so we compiled it completely after introducing our HTTP.

Now Compile has been abandoned and changed to API, so if we have some libraries that change frequently, like the web request library that we’re working on, we can use API instead of Implementation to introduce dependencies.