请解释一下Android build.gradle groovy 语法 [英] Please explain the Android build.gradle groovy syntax

查看:20
本文介绍了请解释一下Android build.gradle groovy 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 groovy 语法的真正含义是什么?

What does the following groovy syntax really mean?

Gradle 文档吹捧 build.gradle 如何只是 groovy.Android 团队已将默认 build.gradle 简化到看起来不像代码的程度(至少对我而言).请根据常规语法解释这是做什么的.例如,这些是Android插件使用的全局变量声明吗?

The Gradle docs tout how the build.gradle is just groovy. The Android team has simplified the default build.gradle to the point that it doesn't look like code (to me at least). Please explain what this is doing in terms of groovy syntax. For example, are these global variable declarations that the Android plugin uses?

如果您包含对 http://groovy-lang.org/syntax.html 作为解释的一部分.

Bonus points if you include references to http://groovy-lang.org/syntax.html as part of your explanation.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.crittercism"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 5
        versionName "5.0"
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

推荐答案

感谢 AndroidGuy 提供出色的视频,让我了解以下信息.视频时长 35 分钟,这是 TL;DR.

Thanks to AndroidGuy for supplying the excellent video that informed me of the information below. The video is 35 minutes long, so here's the TL;DR.

这种语法的大部分是方法调用和闭包.闭包用花括号表示.另请注意,方法调用不需要括号.

Most of this syntax is a mixture of method calls and closures. The closures are represented by curly braces. Also note that method calls do not require parenthesis.

apply plugin: 'com.android.application'

这是调用 apply 方法在 project 对象.项目对象是 Gradle 提供的顶级对象.

This is calling the apply method on the project object with a single named parameter "plugin". The project object is the top level object supplied by Gradle.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

这是设置 dependencies 项目对象的属性.Groovy 属性 基本上是 getter 和 setter 的简写.依赖属性是一个闭包对象,委托给 DependencyHandler.Groovy 委托 本质上是一种增加闭包范围解析的方法.依赖项闭包包含一个用于编译的方法调用,它需要一个 FileTree 位置参数.FileTree 由 fileTree 方法,该方法在项目对象中定义.编译方法对我来说还是有点模糊.它似乎来自 Java 插件,但没有明确记录在那里.编译"部分对我来说仍然有点神奇.

This is setting the dependencies property of the project object. Groovy properties are basically shorthand for getters and setters. The dependencies property is a Closure object that delegates to DependencyHandler. Groovy delegation is essentially a way to augment the scope resolution of a closure. The dependencies closure contains a single method call to compile, which takes a FileTree positional parameter. The FileTree is generated by the fileTree method which is defined in the project object. The compile method is still a bit nebulous to me. It appears to come from the Java plugin, but it isn't explicitly documented there. The 'compile' part is still a bit magical to me.

android {
    ...
}

我将把android"部分留给读者作为练习.Android Gradle 域特定语言 (DSL) 在网络上不可用.您必须下载.

I'll leave the 'android' section as an exercise to the reader. The Android Gradle Domain Specific Language (DSL) is not available on the web. You have to download it.

这篇关于请解释一下Android build.gradle groovy 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆