接着学习笔记(五),这篇文章是官方文档的笔记,和自己的一些理解。看了好几天,终于发现一个比较能够讲清楚的逻辑:
必须要看 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脚本都是由语句和脚本块组成的。
语句可以是方法调用,添加变量等。
语句块是一个方法,其参数是一个闭包,该闭包将代表的是配置信息。
从Gradle DSL中大概可以看到,Gradle本身只是提供了一个框架,它的方法并不多,真正能够完成构建任务的是相应的插件,这点也映证了User Guide Chapter 25 Gradle Plugins的说法,真正的功能都是由插件实现的。
可以看到语句块本身接收的是一个闭包参数,而其中描述了我们的配置信息,回想一下Android中的配置,完全是这样的。至此,对Gradle构建有了一个完整的认识。 当然,还是需要练习的。
UserGuide笔记 :
创建你的第一个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.
org.gradle.api.Project
类,它对应于build.gradle文件,当执行build.gradle脚本文件时,会相应的生成该类。
创建变量:
脚本中有两种变量:local variables and extra properties.
Some Groovy basics一些Groovy基础
其中介绍了一些Groovy的基础,和在Gradle中的应用,就是之前学的groovy基础。
获取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获取
Gradle会把这些task形成一个有向图来构建,保证了每个都会被执行到并且只执行一次。
对应 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要被构建。
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。
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"
}
}
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'
}
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"
}
}
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'