快速掌握 Android Studio 中 Gradle 的使用方法

來源:互聯網
上載者:User

標籤:

Gradle是可以用於Android開發的新一代的 Build System, 也是 Android Studio預設的build工具。

Gradle指令碼是基於一種JVM語言 -- Groovy,再加上DSL(領特定領域語言)組成的。

因為Groovy是JVM語言,所以可以使用大部分的Java語言庫。所謂DSL就是專門針對Android開發的外掛程式,比如標準Gradle之外的一些新的方法(Method)、閉包(Closure)等。

由於Gradle的文法足夠簡潔,而且可以使用大部分的java包,當之無愧地成為新一代 Build System。

使用Android Studio建立一個工程後,預設會產生兩個build.gralde檔案,一個位於工程根目錄,一個位於app目錄下。還有另外一個檔案 --settings.gradle。

根目錄下的指令檔是針對module的全域配置,它的作用閾所包含的所有 module 是通過settings.gradle來配置。

app檔案夾就是一個module,如果在當前工程中添加了一個新的module -- lib,就需要在settings.gralde檔案中包含這個新的module。

 

gradle指令碼的基本結構

用我現在的工程舉例來說,根目錄的build.gradle的內容如下所示:

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositories {        jcenter()    }    dependencies {        classpath ‘com.android.tools.build:gradle:1.0.0-rc4‘        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()        maven {            url ‘http://mvnrepo.xxx.com/mvn/repository‘        }    }}

classpath ‘com.android.tools.build:gradle:1.0.0-rc4‘就是Android特有的外掛程式,maven倉庫位於通過方法jCenter() 擷取,這也是預設的maven倉庫。當然也可以添加額外的maven倉庫地址,例如以上檔案中的

maven {    url ‘http://mvnrepo.xxx.com/mvn/repository‘}

然後是 settings.gradle 檔案:

include ‘:app‘

app就是項目包含的一個module,如果有多個module,可以在為 include 方法添加多個參數。

最後是app/build.gradle:

apply plugin: ‘com.android.application‘android {    compileSdkVersion 21    buildToolsVersion "21.1.1"    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_7        targetCompatibility JavaVersion.VERSION_1_7    }    defaultConfig {        applicationId "your.application.id"        minSdkVersion 14        targetSdkVersion 21        versionCode 2        versionName "2.0.0"    }    signingConfigs {        release {            storeFile file(‘release.keystore‘)            storePassword "yourstorepassword"            keyAlias "yourkeyalias"            keyPassword "yourkeypassword"        }        debug {            storeFile file(‘debug.keystore‘)        }    }    buildTypes {        release {            minifyEnabled true            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘            signingConfig signingConfigs.release        }        debug {            signingConfig signingConfigs.debug        }    }    productFlavors {        inner {            applicationId "your.application.inner.id"            versionName "2.0.0"        }        market {        }    }}repositories {    flatDir {        dirs ‘libs‘    }}dependencies {    // 通用    compile name: ‘volley‘, ext: ‘aar‘    compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.3‘    compile ‘com.alibaba:fastjson:latest.integration‘    // 項目相關(已刪除)}
Groovy 的基本文法方法調用
apply plugin: ‘com.android.application‘

以上語句中的apply是一個方法,給它傳遞了一個參數plugin,plugin 的值是‘com.android.application‘。

如果有多個參數,則以逗號隔開,例如

<code>compile name: ‘volley‘, ext: ‘aar‘</code>
閉包

Groovy中花括弧包含的部分成為一個閉包(Closure)。例如下面的代碼

compileOptions {    sourceCompatibility JavaVersion.VERSION_1_7    targetCompatibility JavaVersion.VERSION_1_7}

compileOptions 是一個 Method, 它的參數是一個閉包,這個閉包內依次執行了兩個方法 -- sourceCompatibility 和targetCompatibility, 參數都是JavaVersion.VERSION17。

閉包也可以嵌套包含

repositories {    flatDir {        dirs ‘libs‘    }}
常見使用方法包依賴(aar)


使用aar時可以分為兩種情況

① aar位於本地目錄

1.首先在 android 的參數閉包中添加調用方法 repositories

repositories {    flatDir {        dirs ‘libs‘    }}

2.然後在 dependencies 的參數閉包中添加

compile name: ‘volley‘, ext: ‘aar‘
② aar位於遠程倉庫

這裡以maven為例,當然也可以使用其他類型的倉庫,例如 Ivy。

只需要在jar包引用方式後面添加一個@aar就可以了

compile ‘com.alibaba:fastjson:[email protected]‘
包依賴(jar)
compile group: ‘com.alibaba‘, module: ‘fastjson‘, version: ‘latest.integration‘

可以簡寫成

compile ‘com.alibaba:fastjson:latest.integration‘

latest.integration可以替換成具體的版本號碼,這裡是擷取伺服器上的最新版本。

去掉重複依賴
compile ‘com.alibaba.fastjson.latest.integration‘ {    exclude module: ‘annotations‘, group: ‘com.google.android‘}
使用 Java7
compileOptions {    sourceCompatibility JavaVersion.VERSION_1_7    targetCompatibility JavaVersion.VERSION_1_7}
productFlavors

針對不同的APP分發渠道,我們可以定義不同的 product flavor。也可以定義組建和外部版本,組建中包含了一些調試代碼,這些代碼在發布時並不會被編譯進最後的APP中。而且可以分別為組建和外部版本指定不同的ApplicationId,這樣在同一個裝置上可以同時安裝兩個版本以方便調試。

命令列執行Gradle指令碼

在Android工程根目錄下會自動產生一個shell指令碼 - gradlew,執行之前記得加上x屬性 - chomod +x gradlew

gradle指令碼中包含了很多 task,可以通過task名來指定需要執行的task。

  • ./gradlew build
  • ./gradlew assemble
  • ./gradlew assembleInnderDebug
總結

不得不說,Gradle實在太好用了!雖然 Gradle 可以與 Ant 或 maven 配合使用,但是其簡潔和功能性遠遠超過其他兩個。我現在開發的項目普遍使用的是 maven,不知道什麼原因,使用Gradle時經常會遇到一些無法擷取遠程依賴包的問題,最簡單的解決辦法就是把依賴包下載的本地。

快速掌握 Android Studio 中 Gradle 的使用方法(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.