做过java开发或者android开发,你一定使用gradle,这也是现代大部分项目使用的项目构建和项目依赖管理的工具。
用过很多年,也知道怎么用,但就是不知道为什么要那么写,gradle的各种花式配置,到底怎么是什么意思呢,要怎么理解这些写法呢?
比如以下的gradle配置(android项目的gradle配置)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.xxx"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName 1.0
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.+'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
}
要理解这些,必须先搞清楚Groovy语法,gradle正是基于Groovy来实现的。
Groovy是一种DSL(Domain Specific Languages)特定领域语言,一般只用在特定领域
怎么去理解Groovy语言呢?
废话少说,先写个hello world!
如果你用的是 idea 工具,一般 tools菜单下都一个 Groovy Console

打开以后,什么都没有。
1、先写个hello world

这是这样:

2、定义一个变量
//定义一个变量i,i为弱类型,它会根据赋的值来定义类型
def i = 1
println i
def s = 'hello'
println s
3、定义一个集合
//定义一个List集合
list = ['a','b']
println(list.get(1)) //输出b
//向集合添加数据
list << 'c'
println(list.get(2)) //输出c
//定义一个map集合
map = [
key1:'value1',
key2:'value2'
]
println(map.key1)//输出value2
//向Map添加数据
map.key3 = 'value3'
println(map.key3)//输出value3
4、定义一个方法
//定义一个方法,参数为map
void method1(Map<String,?> args){
println(args.key1)
}
//调用
method1 key1:'value1' //输出value1
5、定义一个闭包(闭包其实就是一个代码块)
//定义一个闭包(闭包其实就是一个代码块)
bb = {
println('hello bibao')
}
//定义一个方法,参数为闭包类型
static void method1(Closure closure){
closure()
}
//调用方法
method1(bb)//输出 hello bibao
//定义一个带参闭包
bb2 = {
name ->
println('hello '+name)
}
static void method2(Closure closure,String name){
closure(name)
}
//调用方法
method2(bb2,"带参闭包") //输出 hello 带参闭包
6、创建一个对象,跟java语法类似
class MyClass{
}
现在我们可以来重新理解一下gradle中写的哪些玩意了
比如文章开始贴的gradle配置 :
apply plugin: 'com.android.application'
// apply是一个方法,参数为map,这里传的键值对为 plugin: 'com.android.application'
//android 是一个方法,参数是一个闭包
android {
compileSdkVersion 26
//compileSdkVersion 是一个方法,参数为int,值为26,也可以写成compileSdkVersion(26)
defaultConfig {
applicationId "com.xxx"
//applicationId 是一个方法,参数为String,值为 "com.xxx",也可以写成 applicationId ("com.xxx")
minSdkVersion 17
//同上
targetSdkVersion 26
//同上
versionCode 1
//同上
versionName 1.0
//同上
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//同上
}
//defaultConfig 是一个是一个方法,参数是一个闭包
}
//dependencies 是一个方法,参数是一个闭包
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//implementation是一个方法,参数为ConfigurableFileTree
//fileTree 是一个方法,参数为Map<String,Object>,该方法返回一个ConfigurableFileTree对象
//Map中的第一个参数是include是key ,['*.jar']是Value,它一个List集合。
implementation 'com.android.support:appcompat-v7:26.+'
//同上
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//同上
testImplementation 'junit:junit:4.12'
//同上
}
终于可以理解gradle配置中的各种花式写法了
附上完整的demo例子
//写个hello world
//println('hello world');
//groovy语法,可以不写括号和分号
//println 'hello world'
//定义一个变量i,i为弱类型,它会根据赋的值来定义类型
//def i = 1
//println i
//def s = 'hello'
//println s
//定义一个List集合
//list = ['a','b']
//println(list.get(1)) //输出b
//向集合添加数据
//list << 'c'
//println(list.get(2)) //输出c
//定义一个map集合
//map = [
// key1:'value1',
// key2:'value2'
//]
//println(map.key1)//输出value2
//向Map添加数据
//map.key3 = 'value3'
//println(map.key3)//输出value3
//定义一个方法,参数为map
void method1(Map<String,?> args){
println(args.key1)
}
//调用
method1 key1:'value1' //输出value1
//定义一个闭包(闭包其实就是一个代码块)
bb = {
println('hello bibao')
}
//定义一个方法,参数为闭包类型
static void method3(Closure closure){
closure()
}
//调用方法
method3(bb)//输出 hello bibao
//定义一个带参闭包
bb2 = {
name ->
println('hello '+name)
}
static void method4(Closure closure,String name){
closure(name)
}
//调用方法
method4(bb2,"带参闭包") //输出 hello 带参闭包
static void method4(Closure closure){
closure()
}
void name(String name){
println(name)
}
void age(int age){
println(age)
}
method4{
//调用name方法
name 'mi'
//调用age方法
age 20
}
//定义一个类
class MyClass{
}
//定义一个返回MyClass的方法,参数为map
MyClass method5(Map<String,?> args){
println(args.key1)
}
//定义一个参数为MyClass 的类
void method6(MyClass){
}
//调用
method6 method5(key1: '============') //输出============
网友评论