请解释了Android的build.gradle常规语法 [英] Please explain the Android build.gradle groovy syntax

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

问题描述

什么是以下常规语法究竟意味着什么?

在摇篮文档吹捧的build.gradle是如何的只是的时髦。 Android团队简化了默认的build.gradle到它没有(至少对我来说)看起来像code点。请解释一下这是在常规语法方面做的事情。例如,是Android的插件使用这些全局变量声明?

奖励积分,如果你包括 http://groovy-lang.org/syntax.html为你的解释的部分。

 应用插件:'com.android.application安卓{
    compileSdkVersion 21
    buildToolsVersion21.1.2    defaultConfig {
        的applicationIDcom.crittercism
        15的minSdkVersion
        targetSdkVersion 21
        版本code 5
        的versionName5.0
    }}依赖{
    编译文件树(导演:'库',包括:['的* .jar'])
}


感谢AndroidGuy用于提供出色的视频是告诉我下面的信息。视频35分钟之久,所以这里的TL; DR

大多数这种语法是电话和的方法的混合物= http://groovy-lang.org/closures.html相对=nofollow>关闭的。该瓶盖重新由大括号psented $ P $。还要注意的是方法调用不需要括号。

 应用插件:'com.android.application

这是调用<一个href=\"http://gradle.org/docs/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:apply(groovy.lang.Closure)\"相对=nofollow>申请方法在项目用一个命名参数插件对象。该项目目标是通过提供摇篮顶层对象。

  {相关性
    编译文件树(导演:'库',包括:['的* .jar'])
}

这是设置<一个href=\"http://gradle.org/docs/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:dependencies(groovy.lang.Closure)\"相对=nofollow>依赖的项目对象的属性。 Groovy的性能基本上是为getter和setter的简写。依赖属性是一个封闭的对象委托给 DependencyHandler Groovy的代表团实质上是增加一个封闭的范围内解决的方法。依赖封闭包含一个方法调用编译,这需要一个的文件树位置参数。该文件树是由<生成href=\"http://gradle.org/docs/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:fileTree(java.util.Map)\"相对=nofollow>这是在项目对象定义的文件树方法。编译的方法仍然是一个有点模糊的给我。这似乎来自的Java插件,但它没有明确记载有。在编译的一部分还是有点神奇的给我。

 安卓{
    ...
}

我要离开这个机器人部分为读者做练习。 Android的摇篮领域特定语言(DSL)是不在网络上。你必须下载

What does the following groovy syntax really mean?

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?

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'])
}

解决方案

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'

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'])
}

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 {
    ...
}

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常规语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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