project.beforeEvaluate {
println “project.app.beforeEvaluate print”
}
println “project.app end”
如果是mac/linux,执行./gradlew 得到如下结果:
settings.gradle start
settings.gradle end
Configure project :
project.root start
project.root end
Configure project :app
project.app start
project.app end
project.app.afterEvaluate print
Groovy 语法
下面讲一些关于groovy的语法,可以打开Android Studio Tools-> Groovy Console练习Groovy 语法 ,如下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bTKz39YQ-1638154551518)(https://user-gold-cdn.xitu.io/2019/9/17/16d3e0773023df7a?imageView2/0/w/1280/h/960/ignore-error/1)]
可选的类型定义,可以省略语句结束符分号(;)
int vs = 1
def version = ‘version1’
println(vs)
println(version)
括号也是可选的
println vs
println version
字符串定义
def s1 = ‘aaa’
def s2 = “version is ${version}”
def s3 = ‘’’ str
is
many
‘’’
println s1
println s2
println s3
集合
def list = [‘ant’,‘maven’]
list << “gradle”
list.add(‘test’)
println list.size()
println list.toString()
//map
def years = [‘key1’:1000,“key2”:2000]
println years.key1
println years.getClass()
输出结果
[ant, maven, gradle, test]
1000
class java.util.LinkedHashMap
闭包
groovy语法中支持闭包语法,闭包简单的说就是代码块,如下:
def v = {
v -> println v
}
static def testMethod(Closure closure){
closure(‘闭包 test’)
}
testMethod v
其中定义的v就为闭包,testMethod 为一个方法,传入参数为闭包,然后调用闭包.
解释 apply plugin: ‘xxxx’和 dependencies{}
准备工作,看gradle的源码
我们先把子项目的build.gradle改为如下形式
apply plugin: ‘java-library’
repositories {
mavenLocal()
}
dependencies {
compile gradleApi()
}
这样,我们就可以直接看gradle的源码了,在External Libraries里如下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cVVSSeeP-1638154551560)(https://user-gold-cdn.xitu.io/2019/9/17/16d3e2c252afef49?imageView2/0/w/1280/h/960/ignore-error/1)]
解释
进入build.gradle 点击apply 会进入到gradle的源码,可以看到
//PluginAware
/**
- Applies a plugin or script, using the given options provided as a map. Does nothing if the plugin has already been applied.
- The given map is applied as a series of method calls to a newly created {@link ObjectConfigurationAction}.
- That is, each key in the map is expected to be the name of a method {@link ObjectConfigurationAction} and the value to be compatible arguments to that method.
-
The following options are available:
-
- {@code from}: A script to apply. Accepts any path supported by {@link org.gradle.api.Project#uri(Object)}.
- {@code plugin}: The id or implementation class of the plugin to apply.
- {@code to}: The target delegate object or objects. The default is this plugin aware object. Use this to