无标题文章

##Gradle指南

---

####为什么选择Gradle

Gradle官方给出的原因:

> * Domain Specific Language (DSL) to describe and manipulate the build logic.

> * Build files are Groovy based and allow mixing of declarative elements through the DSL and using code to manipulate the DSL elements to provide custom logic.

> * Built-in dependency management through Maven and/or Ivy.

> * Very flexible. Allows using best practices but doesn’t force its own way of doing things.

> * Plugins can expose their own DSL and their own API for build files to use.

> * Good Tooling API allowing IDE integration.

####基本的工程

最简单的纯Java的Gradle脚本:

apply plugin: 'java'

最简单的Android的Gradle脚本:

buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath 'com.android.tools.build:gradle:0.11.1'

}

}

apply plugin: 'android'

android {

compileSdkVersion 19

buildToolsVersion "19.0.0"

}

这段脚本中,有三个主要区块,其意思是:

**buildscript { ... }**

build时候的环境配置,声明了Maven配置和Gradle版本等,这个只在代码build时候产生影响,对于整个工程的repositories的配置还需要在后面单独声明,这个将会被覆盖。

**apply plugin**

和上面java的一样

**android { ... }**

用于配置Android build时候所需要的所有参数,默认的时候只需要两个参数:**compileSdkVersion**和**buildToolsVersion**

####配置结构

这段脚本用于将主要代码旧的目录结构重新映射到新的tests目录下面。

android {

sourceSets {

main {

manifest.srcFile 'AndroidManifest.xml'

java.srcDirs = ['src']

resources.srcDirs = ['src']

aidl.srcDirs = ['src']

renderscript.srcDirs = ['src']

res.srcDirs = ['res']

assets.srcDirs = ['assets']

}

androidTest.setRoot('tests')

}

}

注意:**setRoot()**将整个sourceSets目录以及其目录移动到新的文件夹,这里将** src/androidTest/* ** 移动到 **tests/* **

####build任务

基本的命令:

* **assemble**

The task to assemble the output(s) of the project(输出一个项目文件,android就是打包apk)

* **check**

The task to run all the checks.(运行检查,检查程序的错误,语法,等等)

* **build**

This task does both assemble and check (执行assemble和check)

* **clean**

This task cleans the output of the project(清理项目输出文件)

一个Android Project至少有两个输出: **a debug APK** and **a release APK**,每个都有自己的task,可以将他们分离:

* assemble

* assembleDebug

* assembleRelease

命令支持简写,如:

gradle aR 等同于 gradle assembleRelease

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容