AndroidStudio下gradle的入门介绍与使用

1 Groovy

Groovy 是没有类型的 Java 代码 ,语法更简洁。形式有点类似脚本语言。被gradle用于构建脚本的语言

1.1 特殊类型
1、范围
范围 是一系列的值。

比如 “0..4” 表明包括 整数 0、1、2、3、4。Groovy 还支持排除范围,“0..<4” 表示 0、1、2、3。还能够创建字符范围:“a..e” 相当于 a、b、c、d、e。“a..<e” 包括小于 e 的全部值。

def range = 0..4

2、集合
def coll = ["Groovy", "Java", "Ruby"]

3、映射
def hash = [name:"Andy", "VPN-#":45]

1.2 闭包Closure
闭包是可运行的代码块。

它们不须要名称。能够在定义之后运行。


1、 闭包的格式
  1. def xxx = {paramters -> code}  //或者  
  2. def xxx = {无參数,纯code}  这样的case不须要->符号

2、 闭包的特点
闭包在Groovy中大量使用,比方非常多类都定义了一些函数,这些函数最后一个參数都是一个闭包。


Groovy中。当函数的最后一个參数是闭包的话,能够省略圆括号。比方  
事实上标识doLast为一个函数,函数的最后一个參数为闭包
  1. doLast({  
  2.    println'Hello world!'  
  3. })  
有了圆括号。你会知道 doLast仅仅是把一个Closure对象传了进去。非常明显。它不代表这段脚本解析到doLast的时候就会调用println 'Hello world!' 。
可是把圆括号去掉后。就感觉好像println 'Hello world!'马上就会被调用一样!

举例:闭包用于迭代
def acoll = ["Groovy", "Java", "Ruby"]
		
acoll.each{
 println it
}
闭包中的 it 变量是一个keyword,指向被调用的外部集合的每一个值 — 它是默认值,能够用传递给闭包的參数覆盖它。



1.3 groovy脚本的实质
既然是基于Java的,Groovy会先把xxx.groovy中的内容转换成一个Java类。比方:
test.groovy的代码是:
  1. println 'Groovy world!'  
Groovy把它转换成这种Java类:

运行 groovyc-d classes test.groovy

groovyc是groovy的编译命令,-d classes用于将编译得到的class文件复制到classes目录下


2 Gradle

Gradle是一种依赖管理工具,基于Groovy语言。面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言。

用于自己主动化构建、測试、公布打包。


2.1 基本组件
Gradle是一个框架。它定义一套自己的游戏规则。我们要玩转Gradle。必需要遵守它设计的规则。以下我们来讲讲Gradle的基本组件:
Gradle中,每个待编译的project都叫一个Project。

每个Project在构建的时候都包括一系列的Task。

比方一个Android APK的编译可能包括:Java源代码编译Task、资源编译Task、JNI编译Task、lint检查Task、打包生成APK的Task、签名Task等。

1.  gradle projects查看工程信息

到眼下为止,我们了解了Gradle什么呢?

每个Project都必须设置一个build.gradle文件。至于其内容。我们留到后面再说。

对于multi-projects build。须要在根文件夹下也放一个build.gradle,和一个settings.gradle。

一个Project是由若干tasks来组成的,当gradlexxx的时候,实际上是要求gradle运行xxx任务。

这个任务就能完毕详细的工作。


2.  gradle tasks查看任务信息

gradleproject-path:tasks 即可。注意,project-path是文件夹名。后面必须跟冒号。

对于Multi-project。在根文件夹中,须要指定你想看哪个poject的任务。

只是你要是已经cd到某个Project的文件夹了,则不需指定Project-path。


2.2 gradle build的生命周期
  • Gradle有一个初始化流程,这个时候settings.gradle会运行。
  • 在配置阶段,每一个Project都会被解析。其内部的任务也会被加入到一个有向图里,用于解决运行过程中的依赖关系。
  • 然后才是运行阶段。

    你在gradle xxx中指定什么任务。gradle就会将这个xxx任务链上的所有任务所有按依赖顺序运行一遍!

(1)生命周期

eg:Single project build example

settings.gradle

println 'This is executed during the initialization phase.'

build.gradle

println 'This is executed during the configuration phase.'

task configured {
    println 'This is also executed during the configuration phase.'
}

task test << {
    println 'This is executed during the execution phase.'
}

task testBoth {
    doFirst {
      println 'This is executed first during the execution phase.'
    }
    doLast {
      println 'This is executed last during the execution phase.'
    }
    println 'This is executed during the configuration phase as well.'
}

(2)setting.gradle中多project配置
当中setting.gradle在多project的配置中。用于声明包括的project及其层级关系(平坦和层级结构的project都支持)
include 'project1', 'project2:child', 'project3:child1'
includeFlat 'project3', 'project4'

(3)依据生命周期定制task

eg:Logging of start and end of each task execution

build.gradle

task ok

task broken(dependsOn: ok) << {
    thrownew RuntimeException('broken')
}

gradle.taskGraph.beforeTask { Task task ->
    println "executing $task ..."
}

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

3 Gradle用途

(1)依赖管理
我们的project可能会用到其它的库(本地、maven或者ivy)
eg:Declaring dependencies
本地project依赖
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':bugrpt')
}
远程库依赖
apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}
标识project须要hibernate-core一起编译,project test须要junit的编译,第二种写法group:name:version
dependencies {
    compile 'org.hibernate:hibernate-core:3.6.7.Final'
}

像这些外部的库,gradle是通过repository来找到的

eg:Usage of a remote Maven repository

build.gradle

repositories {
    maven {
        url "http://repo.mycompany.com/maven2"
    }
}

eg:Usage of a local Ivy directory

build.gradle

repositories {
    ivy {
        // URL can refer to a local directory
        url "../local-repo"
    }
}
(2)版本号公布

eg:公布project
uploadArchives {
    repositories {
        ivy {
            credentials {
                username "username"
                password "pw"
            }
            url "http://repo.mycompany.com"
        }
    }
}

(3) 差异管理
比方app生成不同版本号(免费。收费)。适配特殊机型,多渠道等须要发多个包。终于能编译出的apk的数量是由productflavor与BuildType决定的,BuildType默认有debug和release两种
eg:免费与收费设置不同包名
 productFlavors {
        pay {
            applicationId "me.ghui.gradledemo.pay"
        }
        free {}
    }

eg:buildTypes 相应的不同版本号
android {
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }

        jnidebug {
            initWith(buildTypes.debug)
            packageNameSuffix ".jnidebug"
            jniDebuggable true
        }
    }
}
对于每一种buildTypes 会创建对应的ssemble<BuildTypeName>任务,比方debug会自己主动创建assembleDebug任务

eg:多渠道打包


4 gradle wrapper

gradlew通过包裹了gradle,然后通过配置中url下载。存储到用户文件夹 $USER_HOME/.gradle/wrapper/dists.

 启动图形界面

gradle --gui





附录 gradle脚本基础
1 project和task
task表示构建的一次原子操作,包括编译类、创建jar包、公布到repository、生成javadoc等,而project通常包括多个task
task hello {
    doLast {
        println 'Hello world!'
    }
}
这个脚本定义了一个叫做hello的task,而且加入了一个action,这个action实际上是由groovy语言编写的闭包。更简洁的写法
task hello << {
    println 'Hello world!'
}

> gradle -q hello

Hello world!

2 task依赖
project('projectA') {
    task taskX(dependsOn: ':projectB:taskY') << {
        println 'taskX'
    }
}

project('projectB') {
    task taskY << {
        println 'taskY'
    }
}

3 能够动态创建task,并通过api操作task
4.times { counter ->
    task "task$counter" << {
        println "I'm task number $counter"
    }
}
task0.dependsOn task2, task3
4 task能够定义属性
task myTask {
    ext.myProperty = "myValue"
}

task printTaskProperties << {
    println myTask.myProperty
}
5 设置默认
defaultTasks 'clean', 'run'

task clean << {
    println 'Default Cleaning!'
}

task run << {
    println 'Default Running!'
}

task other << {
    println "I'm not a default task!"
}
6 能够加入HOOK
task distribution << {
    println "We build the zip with version=$version"
}

task release(dependsOn: 'distribution') << {
    println 'We release now'
}

gradle.taskGraph.whenReady {taskGraph ->
    if (taskGraph.hasTask(release)) {
        version = '1.0'
    } else {
        version = '1.0-SNAPSHOT'
    }
}


7 定位task
1)使用task的名字
println hello.name
2)使用tasks collection
println tasks.hello.name
3)使用project:task路径定位
tasks.getByPath(':projectA:hello').path

8 配置task

Configuring a task - with closure


task copy(type: Copy) {
   description 'Copies the resource directory to the target directory.'
   from 'resources'
   into 'target'
   include('**/*.txt', '**/*.xml', '**/*.properties')
}

參考:
posted @ 2017-08-01 18:13  zsychanpin  阅读(726)  评论(0编辑  收藏  举报