無標題文章

##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"

}

這段腳本中,有三個主要區(qū)塊,其意思是:

**buildscript { ... }**

build時候的環(huán)境配置,聲明了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 (執(zhí)行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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容