編輯:關於 Android 程式設計
Gradle 初看讓人頭痛,但在 Android 開發裡,我們其實只需要掌握「它大概怎麼用」以及「配置寫在哪裡」,就足以應付日常打包需求。
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 { } 這個閉包是配置的核心,內部可包含以下常見節點:
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 包的示意:

Android 網絡圖片異步加載實作解析
前言 運氣不錯,入職一週後就能跟著兩位師兄做 Android 開發。師兄們都是高手,身為新人的我唯有多看多學多努力。前陣子忙得沒時間整理,今天剛完成 Android 客戶
Android Activity 啟動模式探索:四種模式與 Intent Flag
Activity 的啟動模式決定了它如何進入 Task 棧,以及是否允許重複建立實例。Android 提供 standard、singleTop、singleTask 與
Android 手把手實作:打造一個透明背景的進度對話框
先來看看我們要做出來的「透明進度對話框」長什麼樣子——它沒有傳統對話框的邊框與背景,只有一個持續旋轉的加載動畫,乾淨又低調。 實作步驟 1、建立專案 用 Eclips
解決 Android 引用 Gson 時的 NoClassDefFoundError 問題
某個 Android 程式依賴了一個 Library 專案 B,而 B 中用到了 Gson 來格式化 JSON 字串,把壓縮的 JSON 轉成標準格式: public s