Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 多渠道打包實戰:用 Gradle 與 Android Studio 一鍵出包

編輯:關於 Android 程式設計

Gradle 初看讓人頭痛,但在 Android 開發裡,我們其實只需要掌握「它大概怎麼用」以及「配置寫在哪裡」,就足以應付日常打包需求。

一、build.gradle 的基本結構

Android Studio 建立工程後,根目錄會生成一個 build.gradle,每個 module 下也各自生成一個。本文聚焦於 module 級別的 build.gradle。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.stone.testbuild"
        minSdkVersion 8
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

二、Android Gradle DSL 速覽

android { } 這個閉包是配置的核心,內部可包含以下常見節點:

  • defaultConfig { }:預設配置,ProductFlavor 類型,會被其他 flavor 共用。
  • sourceSets { }:源碼目錄設定,AndroidSourceSet 類型。
  • buildTypes { }:構建類型,BuildType 類型。
  • signingConfigs { }:簽名配置,SigningConfig 類型。
  • productFlavors { }:產品風格配置,ProductFlavor 類型。
  • testOptions { }、aaptOptions { }、lintOptions { }、dexOptions { }、compileOptions { }、packagingOptions { }、jacoco { }、splits { } 等,分別對應測試、aapt、代碼檢查、dex、編譯、打包、覆蓋率與分包配置。

三、多渠道打包配置

目標很明確:自動完成簽名、混淆、打包,並把不同渠道的標識注入到 apk 中。

apply plugin: 'com.android.application'

// 用 def 聲明時返回類型自動推斷,也可以顯式寫成 String
def String computeVersionName() {
    return "8.8.8"
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.stone.myapplication"
        minSdkVersion 8
        targetSdkVersion 22
        versionCode 1
        versionName computeVersionName()

        // manifest 中的佔位符,形如 android:name="${YOUR_APP_KEY}"
        manifestPlaceholders = [YOUR_APP_KEY: "友盟後台的appkey"]
    }

    signingConfigs {
        myConfig {
            storeFile file("stone.keystore")
            storePassword "mypasswd"
            keyAlias "stone"
            keyPassword "mypasswd"
        }
    }

    buildTypes {
        release {
            minifyEnabled true              // 啟用混淆
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.myConfig
            zipAlignEnabled true
        }
        debug {
            debuggable true
        }
        aabbcc {                            // 自訂類型,未配簽名,產物為未簽名 apk
            multiDexEnabled true
        }
    }

    productFlavors {
        // productFlavors 中的配置會與 defaultConfig、buildTypes 形成類似 full join 的組合關係
        // 執行 gradle build 後會產出 module-flavor1-release.apk、module-flavor1-debug.apk 等一系列包
        flavor1 {
            proguardFiles 'proguard-rules.pro'
            applicationId "com.stone.myapplication.pro"   // 專業版
            manifestPlaceholders = [channelID: "百度應用平台"]
        }
        flavor2 {
            proguardFile 'proguard-rules.pro'
            applicationId "com.stone.myapplication.free"  // 免費版
            manifestPlaceholders = [channelID: "豌豆夾"]
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])       // 本地 jar
    compile project(':eventbuslib')                          // 依賴 library module
    compile 'com.android.support:appcompat-v7:22.0.0'        // 遠程倉庫依賴
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.android.support:cardview-v7:22.0.0'
    compile 'com.android.support:recyclerview-v7:22.0.0'
}

所謂多渠道,本質上就是定義多個 flavor,再用 manifestPlaceholders 去替換 AndroidManifest.xml 中的 ${} 佔位符。

<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.stone.myapplication">

    <meta-data android:name="UMENG_APPKEY" android:value="${YOUR_APP_KEY}" />
    <meta-data android:name="channelName" android:value="${channelID}" />
</manifest>

最後執行 gradle build,各渠道的 apk 便會整齊地躺在 build 目錄裡。

補充一個引用遠程 jar 包的示意:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved