gradle学习笔记(六) 官方文档笔记+理解

前言

接着学习笔记(五),这篇文章是官方文档的笔记,和自己的一些理解。看了好几天,终于发现一个比较能够讲清楚的逻辑:

  1. User Guide第三大章都有必要看
  2. 看完User Guide直接看Gradle Build Language Reference即文件夹中的dsl,马上就能有个清晰的理解。

必须要看 User Guide 的 Chapter 25. Gradle Plugins 和 Gradle DSL。这样一来就差不多能够理解 Gradle是个框架,真正功能是插件实现的 这句话了。

当有了这些基础后,再来阅读 深入理解Android之Gradle 就不再懵逼了,哈哈哈哈。



以下是自己的一些笔记。

Gradle DSL笔记:
先理解一些概念,以帮助你写脚本。
第一, Gradle脚本是配置脚本,当执行脚本时,会生成与该脚本对应的特殊对象。
build.gradle生成org.gradle.api.Project对象;
settings.script 生成 org.gradle.api.initialization.Settings 对象
第二, 每个脚本对象都实现 org.gradle.api.Script 接口,所以你可以使用它定义的属性和方法。

A build script is made up of zero or more statements and script blocks. Statements can include method calls, property assignments, and local variable definitions. A script block is a method call which takes a closure as a parameter. The closure is treated as a configuration closure which configures some delegate object as it executes.

Gradle脚本都是由语句和脚本块组成的。
语句可以是方法调用,添加变量等。
语句块是一个方法,其参数是一个闭包,该闭包将代表的是配置信息。

  1. 从Gradle DSL中大概可以看到,Gradle本身只是提供了一个框架,它的方法并不多,真正能够完成构建任务的是相应的插件,这点也映证了User Guide Chapter 25 Gradle Plugins的说法,真正的功能都是由插件实现的。

  2. 可以看到语句块本身接收的是一个闭包参数,而其中描述了我们的配置信息,回想一下Android中的配置,完全是这样的。至此,对Gradle构建有了一个完整的认识。 当然,还是需要练习的。

UserGuide笔记 :

Chapter 14. Build Script Basics

创建你的第一个Gradle程序: Hello World。
.The gradle command looks for a file called build.gradle in the current directory. We call this build.gradle file a build script.

Chapter 16,Writing Build Scripts

org.gradle.api.Project类,它对应于build.gradle文件,当执行build.gradle脚本文件时,会相应的生成该类。

  • 脚本中未定义的方法,会到project中寻找
  • 脚本中未定义的属性,会到project中寻找

创建变量:
脚本中有两种变量:local variables and extra properties.

Some Groovy basics一些Groovy基础
其中介绍了一些Groovy的基础,和在Gradle中的应用,就是之前学的groovy基础。

Chapter 17. More about Tasks

获取task:
1. each task is available as a property of the project, using the task name as the property name
每一个task就是该project的一个属性,可以通过task name来获取该task.

2.Tasks are also available through the tasks collection.
可以通过task collections获取

Chapter 20. The Build Lifecycle

Gradle会把这些task形成一个有向图来构建,保证了每个都会被执行到并且只执行一次。

Build phases构建阶段

  1. Initialization初始化
    初始化时,Gradle会去判断哪些应该被构建,并为这些需要构建的项目创建相应的Project对象。
  2. Configuration配置
    During this phase the project objects are configured.The build scripts of all projects which are part of the build are executed.
    所有项目的build.gradle脚本会被执行。
  3. Execution执行
    Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed.
    执行在Configuration阶段生成的一系列tasks。

Settings file

对应 org.gradle.api.initialization.Settings 类。

A multiproject build must have a settings.gradle file in the root project of the multiproject hierarchy. It is required because the settings file defines which projects are taking part in the multi-project build
mutiproject必须要有settings.gradle,因为settings.gradle定义了哪些project要被构建。

Multi-project builds

1.Hierarchical layouts
include 'project1', 'project2:child', 'project3:child1'
对应于该方法void include(String[] projectPaths)
'services:hotels:api' 意味着将创建3个project, services,hotels,api.

2.Flat layouts
includeFlat 'project3', 'project4'
用这种方法引入的project必须是rootProject的一级project。

监听task

1.监听task添加是否到Project中
You can receive a notification immediately after a task is added to a project

//tasks是taskContainer,参数是Action,查阅API理解
tasks.whenTaskAdded { task ->
    task.ext.srcDir = 'src/main/java'
}

2.监听task执行前后
You can receive a notification immediately before and after any task is executed.

//org.gradle.api.invocation.Gradle
//org.gradle.api.execution.TaskExecutionGraph
//全是API
gradle.taskGraph.beforeTask { Task task ->
    println "executing $task ..."
}

gradle.taskGraph.afterTask { Task task, TaskState state ->
    if (state.failure) {
        println "FAILED"
    }
    else {
        println "done"
    }
}

Chapter 23. Dependency Management

23.4 添加依赖的几种方式:

org.gradle.api.artifacts.dsl.DependencyHandler 中有更详细的说明

1.External dependencies 外部依赖
External module dependencies are the most common dependencies. They refer to a module in an external repository.

dependencies { compile 'commons-lang:commons-lang:2.6' }

2.Project dependencies 依赖项目

compile project(':someProject')

3.File dependencies 文件依赖
compile fileTree(dir: ‘libs’, include: [‘*.jar’])

4.Excluding transitive dependencies 去除一些东西

compile("org.gradle.test.excludes:api:1.0") {
    exclude module: 'shared'
}

23.6 Repositories

You may configure any number of repositories, each of which is treated independently by Gradle.
有以下几种仓库:

repositories {
    mavenCentral() //Maven central repository
    jcenter() //Maven JCenter repository
    mavenLocal() //Local Maven repository,本地的Maven cache

    //Maven repositories, Adding custom Maven repository,自定义从该url仓库中获取依赖
    maven { 
        url "http://repo.mycompany.com/maven2"
    }
}

Chapter 25. Gradle Plugins

Gradle at its core intentionally provides very little for real world automation. All of the useful features, like the ability to compile Java code, are added by plugins.
Gradle本身提供了很少的自动化构建,所有有用的功能,都是由插件提供的。

插件扩展了project的功能,可以添加更多的DSL配置。
A plugin is simply any class that implements the Plugin interface.

添加插件,调用Project.apply()方法:

apply plugin: 'java'

你可能感兴趣的:(android,gradle,文档)