關於Gradle配置的小結

前言

使用 Android Studio 來開發 Android 工程的過程中,接觸 Gradle 是不可避免的,比如配置簽名、引入依賴等。那麼 Gradle 到底是什麼東西呢? Gradle 是一個基於 Apache Ant 和 Apache Maven 概念的項目自動化建構工具。它使用一種基於 Groovy 的特定領域語言 (DSL) 來聲明項目設置,拋棄了基於 XML 的各種繁瑣配置 (此定義來自於百度百科-_- !) 。囉裏囉唆一堆,幸運的是,一般來說 Android 開發者只要會配置 Gradle 就可以了,並不需要深入瞭解。那麼下面我們就來揭開 Gradle 的面紗吧。

Gradle 配置

首先貼出一張自己項目的文件目錄結構圖:

文件目錄結構圖

從上圖中我們可以看到,與 Gradle 有關的文件基本上分爲四種:

  1. app 下的 build.gradle (當然其他 module 下也有);
  2. 根目錄下的 gradle 文件夾;
  3. 根目錄下的 build.gradle ;
  4. 根目錄下的 settings.gradle ;

也許有人會說根目錄下還有一個 config.gradle 文件呢,其實這是我自定義的 gradle 文件,自定義 Gradle 文件會在下面中講解,這裏先擱置一下。好了,那麼我們一個一個地來看看他們的作用吧。

app 下的 build.gradle

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23 // 編譯sdk版本
    buildToolsVersion "23.0.2" // 構建工具版本
    defaultConfig {
        applicationId "com.yuqirong.koku" // 應用包名
        minSdkVersion 15 // 最低適用sdk版本
        targetSdkVersion 23 // 目標sdk版本
        versionCode 1 // 版本號
        versionName "1.0" // 版本名稱
    }
    buildTypes {
        release {
            minifyEnabled true // 開啓混淆
            zipAlignEnabled true // 對齊zip
            shrinkResources false // 刪除無用資源
            debuggable false // 是否debug
            versionNameSuffix "_release" // 版本命名後綴
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // 混淆文件
        }

        debug {
            zipAlignEnabled false
            shrinkResources false
            minifyEnabled false
            versionNameSuffix "_debug"
            signingConfig signingConfigs.debug
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
}

第一句 apply plugin: 'com.android.application' 主要用來申明這是一個 Android 程序。而 dependencies 用於引入依賴,這個相信大家都比較瞭解了。其他的配置比較簡單都有註釋,就不展開講了。

當然除了上面的配置之外,還有很多配置也常常寫入到 app/build.gradle 中。我們慢慢往下看。

  • 簽名配置:
signingConfigs {
    
    release { // 正式版本的簽名
        storeFile file("../koku.jks") // 密鑰文件位置
        storePassword "xxxxxxxxx" // 密鑰密碼
        keyAlias "koku" // 密鑰別名
        keyPassword "xxxxxxxxx" // 別名密碼
    }
    
    debug { // debug版本的簽名
        // no keystore
    }
}

使用時只要在 buildTypes 的 release 中加一句 signingConfig signingConfigs.release 就好了。

如果你覺得把密鑰密碼和別名密碼放在 app/build.gradle 裏不安全,那麼可以把相關密碼放到不加入版本控制系統的 gradle.properties 文件:

KEYSTORE_PASSWORD=xxxxxxxxxx
KEY_PASSWORD=xxxxxxxxx

對應的 signingConfigs 配置:

signingConfigs {
    release {
        try {
            storeFile file("../koku.jks")
            storePassword KEYSTORE_PASSWORD
            keyAlias "koku"
            keyPassword KEY_PASSWORD
        }
        catch (ex) {
            throw new InvalidUserDataException("You should define KEYSTORE_PASSWORD and KEY_PASSWORD in gradle.properties.")
        }
    }
}
  • Java 編譯版本配置:
compileOptions { // java 版本
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

這裏需要注意下,如果 Java 編譯版本爲1.8的話,另外在 defaultConfig 裏要配置 Jack 編譯器:

jackOptions {
    enabled true
}
  • Lint 檢查配置:
lintOptions {
    abortOnError false // 是否忽略lint報錯
}
  • 多渠道信息配置:
productFlavors {
    xiaomi {}
    googleplay {}
    wandoujia {}
}

整個 app/build.gradle 文件配置如下所示:

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.android.compileSdkVersion
    buildToolsVersion rootProject.ext.android.buildToolsVersion
    defaultConfig {
        applicationId "com.yuqirong.koku" // 應用包名
        minSdkVersion 15 // 最低適用sdk版本
        targetSdkVersion 23 // 目標sdk版本
        versionCode 1 // 版本號
        versionName "1.0" // 版本名稱
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        // 默認是umeng的渠道
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
        jackOptions {
            enabled true
        }
    }
    java 版本
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    signingConfigs {
        release {
            storeFile file("../koku.jks")
            storePassword "xxxxxx"
            keyAlias "koku"
            keyPassword "xxxxxx"
        }
        debug {
            // no keystore
        }
    }
    buildTypes {
        release {
            // 開啓混淆
            minifyEnabled true
            // 對齊zip
            zipAlignEnabled true
            // 刪除無用資源
            shrinkResources false
            // 是否debug
            debuggable false
            // 命名後綴
            versionNameSuffix "_release"
            // 簽名
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 輸出apk名稱爲koku_v1.0_2015-01-15_wandoujia.apk
                        def fileName = "koku_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }

        debug {
            zipAlignEnabled false
            shrinkResources false
            minifyEnabled false
            versionNameSuffix "_debug"
            signingConfig signingConfigs.debug
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    productFlavors {
        xiaomi {}
        googleplay {}
        wandoujia {}
    }
    //針對很多渠道
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
}

def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

再多嘴一句,有了以上的 build.gradle 配置之後,如果想使用 Gradle 多渠道打包,需要在 AndroidManifest.xml 中申明:

<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />

最後使用命令 gradlew assembleRelease 打包即可。

根目錄下的 gradle 文件夾

gradle 文件夾中主要是 gradle-wrapper.properties 文件比較重要,主要用來聲明 Gradle 目錄以及 Gradle 下載路徑等:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

根目錄下的 build.gradle

根目錄下的 build.gradle 主要作用就是定義項目中公共屬性,比如有依賴倉庫、 Gradle 構建版本等:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

setting.gradle

setting.gradle 的作用就是一些模塊被包含後,會在這裏進行申明:

include ':app'

自定義 Gradle 文件

在上面我們留了一個懸念,就是如何添加我們自定義的 Gradle 文件。接下來我們就動手來實踐一下。在項目根目錄下創建文件 config.gradle 。然後在根目錄下的 build.gradle 開頭添加一句 apply from: "config.gradle"

apply from: "config.gradle"

buildscript {
    repositories {
        jcenter()
    }
    ...
}

...

這句話就代表着把 config.gradle 添加進來了。然後我們可以在 config.gradle 中申明一些配置:

ext {

    android = [
            compileSdkVersion: 23,
            buildToolsVersion: "23.0.3",
            applicationId    : "com.yuqirong.koku",
            minSdkVersion    : 14,
            targetSdkVersion : 23,
            versionCode      : 3,
            versionName      : "1.4"
    ]

    dependencies = [
            "appcompat-v7"            : 'com.android.support:appcompat-v7:23.0.1',
            "recyclerview-v7"         : 'com.android.support:recyclerview-v7:24.2.1',
            "design"                  : 'com.android.support:design:23.0.1'
    ]
}

最後在 app/build.gradle 中去使用:

android {
    compileSdkVersion rootProject.ext.android.compileSdkVersion
    buildToolsVersion rootProject.ext.android.buildToolsVersion
    defaultConfig {
        applicationId rootProject.ext.android.applicationId
        minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
        versionCode rootProject.ext.android.versionCode
        versionName rootProject.ext.android.versionName

        jackOptions {
            enabled true
        }
    }
    ...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile rootProject.ext.dependencies["appcompat-v7"]
    compile rootProject.ext.dependencies["recyclerview-v7"]
    compile rootProject.ext.dependencies["design"]
}

從上面可以看到,我們把一些固定的配置“拎”出來放到 config.gradle 中,這樣以後直接更改 config.gradle 就行了,方便多人協作開發。

結束

關於 Gradle 的平時經常使用方法基本上就上面這些了。其他的一些比如 buildConfigField 之類的可以自行百度,相信聰明的你很快就會了。但是 Gradle 並沒有以上講得那麼簡單,還需要童鞋們繼續努力學習了。

如果對本文有不明白的地方,歡迎留言。

Goodbye !



作者:俞其榮
鏈接:https://www.jianshu.com/p/03147709ea21
來源:簡書

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章