編輯:關於 Android 開發
Android Studio 專案中有一個頂層構建檔,以及每個模組各自擁有的構建檔。這些檔案命名為 build.gradle,本質上是純文字檔,採用 Groovy 語法來配置 Android Gradle 外掛提供的元素。日常開發中,絕大多數時候你只需要編輯模組層級的構建檔。
以下是一個典型的 app 模組構建檔:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile project(":lib")
compile 'com.android.support:appcompat-v7:19.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
逐段拆解其中的關鍵配置:
apply plugin: 'com.android.application' 套用 Android Gradle 外掛,把 Android 專屬的構建任務加入頂層任務清單,並啟用 android { … } 區塊。
compileSdkVersion 指定編譯時的目標 API 級別。
buildToolsVersion 指定使用的構建工具版本,可透過 SDK Manager 安裝多個版本並存。官方建議:請一律使用主版本號不低於 compileSdkVersion 的構建工具。
defaultConfig 動態決定會寫入 AndroidManifest.xml 的各項設定。這裡設定的值會覆蓋 Manifest 中的對應值,並套用到所有構建變體,除非該變體另有配置。
buildTypes 控制應用如何被構建與打包。系統預設提供 debug 與 release 兩種:debug 會帶入除錯符號並使用 debug key 簽名;release 預設不簽名。上例中 release 已啟用 ProGuard。
dependencies 宣告模組依賴,位於 android 區塊之外、之後。
修改構建檔之後,Android Studio 會要求同步專案以載入新的配置,點擊黃色提示列上的 Sync Now 即可。
圖一 在 Android Studio 中同步專案。
上例中的 app 模組宣告了三種依賴:
dependencies {
// Module dependency
compile project(":lib")
// Remote binary dependency
compile 'com.android.support:appcompat-v7:19.0.1'
// Local binary dependency
compile fileTree(dir: 'libs', include: ['*.jar'])
}
構建系統會把所有 type 為 compile 的依賴加入編譯路徑,並最終打包進成品中。
模組依賴
app 模組依賴 lib 模組(MainActivity 需要登入 LibActivity)。compile project(":lib") 宣告了這層關係;構建 app 時,系統會一併把 lib 模組集合進來。
遠端二進位依賴
app 與 lib 都使用了 Android 支援庫中的 ActionBarActivity,因此兩者都宣告了對它的依賴。compile 'com.android.support:appcompat-v7:19.0.1' 以 Maven 座標指定支援庫 19.0.1 版。若 SDK Manager 中尚未安裝這個套件,需先補上。Android Studio 預設使用 Maven 中央倉庫,這項設定寫在專案頂層的構建檔中。
本機二進位依賴
若模組需要依賴本地 jar 檔,把檔案複製到 <moduleName>/libs 目錄下,再以 compile fileTree(dir: 'libs', include: ['*.jar']) 讓構建系統把該目錄底下所有 jar 納入編譯路徑與最終打包。
更完整的依賴管理說明,可參考 Gradle 使用者指南的 Dependency Management Basics。
構建系統會在打包過程中呼叫 ProGuard 進行程式碼混淆。以 release 構建啟用:
android {
...
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
getDefaultProguardFile('proguard-android.txt') 會從已安裝的 Android SDK 中取得預設規則;Android Studio 另外會把模組根目錄的 proguard-rules.pro 併入模組專屬規則。混淆時別忘了把需要保留的類別(例如被反射呼叫的模型類、第三方 SDK 的入口)寫進這份檔案。
在 Android 構建系統中,applicationId 是唯一標識發行套件的屬性,只在 build.gradle 的 android 節點中設定。
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.1"
defaultConfig {
applicationId "com.example.my.app"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
}
注意:applicationId 只能在 build.gradle 中指定,寫在 AndroidManifest.xml 裡是無效的。
使用 build variants 時,你可以為每個 product flavor 與 build type 分別指定標識。build type 中的 applicationIdSuffix 會作為後綴附加:
productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
而 Manifest 中的 package 名稱仍必須保留,因為原始碼要靠它來定位 R 類別,並解析 activity/service 的註冊:
package="com.example.app"
若專案中存在多份 Manifest(例如某個 flavor 與某個 build type 各有一份),這些檔案中的 package 是可省的;一旦寫了,就必須與 src/main 底下的 Manifest 保持一致。
debug 與 release 版本在簽名與可除錯性上有明顯差異。構建系統會以預設 key 自動簽名 debug 版本,並使用已知憑證以避免構建過程跳出密碼提示;release 版本則不會自動簽名,除非你明確定義 signingConfigs。若還沒有 release 用的金鑰,可依 Signing your Applications 的說明自行產生。
Build variants 的機制,讓你能在同一個專案中產出同一個應用的多個版本。這在需要同時維護「免費版/付費版」,或要針對不同硬體配置上架多個 APK 時特別有用。
構建系統以 product flavors 產生不同版本,每個版本可以有各自的特性與裝置需求;再將 build types 套用到各個構建上,決定打包配置。product flavor 與 build type 的每一種組合,就構成一個 build variant,系統會為每個 variant 產出獨立 APK。
本專案範例包含兩個預設 build types(debug、release)與兩個 product flavors(demo、full)。
Product Flavors 的三個步驟
範例中,demo 與 full 兩個 flavor 共用 MainActivity,兩者都有一顆按鈕可跳轉到 SecondActivity;而 SecondActivity 在兩個 flavor 中內容不同——full 版的功能比 demo 版更豐富。練習完成後,你會得到兩支內容有別的 APK。
在構建檔中定義 product flavors
android {
...
defaultConfig {...}
signingConfigs {...}
buildTypes {...}
productFlavors {
demo {
applicationId "com.buildsystemexample.app.demo"
versionName "1.0-demo"
}
full {
applicationId "com.buildsystemexample.app.full"
versionName "1.0-full"
}
}
}
flavor 的設定會與 defaultConfig 合併,共同點寫在 defaultConfig,各 flavor 只需覆寫差異處。上例用 applicationId 為兩個 flavor 分配不同套件名——既然它們是兩個不同的應用,套件名自然應該不同。
補充:若希望在 Google Play 上以多 APK 形式上架,應讓所有 variant 使用相同套件名,並給予不同的 versionCode;反之,若要在 Google Play 中作為彼此獨立的應用並列,就應該分配不同套件名。
為每個 flavor 建立額外原始碼目錄
圖一 demo flavor 的目錄結構。
為不同 flavor 加入不同的 Activity
接著把 SecondActivity 的布局與資源檔也搬進 demo flavor:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">This is the full version!</string>
</resources>
從這一刻起,你可以為每個 flavor 獨立開發 SecondActivity,例如替 full 版的頁面加上更多功能。
要讓指定 flavor 的原始碼生效,點擊 IDE 左下方的 Build Variants,選擇要使用的 flavor。Android Studio 此時可能對其他 flavor 顯示錯誤提示,但這不影響目前變體的構建結果。
圖 2 Build Variants 面板。
由於 SecondActivity 在所有 flavor 中使用相同套件名,可以在 MainActivity 中統一登入。首先編輯 activity_main.xml 加入按鈕:
<LinearLayout ...>
...
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2"
android:onClick="onButton2Clicked"/>
</LinearLayout>
接著補上按鈕文字與 onButton2Clicked 事件,並在 MainActivity 中實作:
public void onButton2Clicked(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
最後在 Manifest 中註冊這個 Activity:
<manifest ...>
<application ...>
...
<activity
android:name="com.buildsystemexample.app.SecondActivity"
android:label="@string/title_activity_second">
</activity>
</application>
</manifest>
build types 描述的是每個 app 套件的構建版本屬性,系統預設提供 debug 與 release:
android {
...
defaultConfig {...}
signingConfigs {...}
productFlavors {...}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}
要注意:即使 build.gradle 中只寫了 release,debug 與 release 兩種構建類型仍會被套用到每一次構建流程。
在本例中,product flavors 與 build types 交叉組合後,共產生四個 build variants:
demoDebug、demoRelease、fullDebug、fullRelease
要執行構建,可以點擊 Android Studio 的 Build 選單,或在命令列執行 assemble 命令。另外提醒:Build > Make Project 只編譯專案中的所有原始碼;Build > Rebuild Project 則會完整重新編譯一次。不同 build variant 會輸出到各自獨立的目錄中,彼此不會互相覆蓋。
Android Studio 多渠道打包實戰:基於友盟統計的 productFlavors 方案
Android Studio 多渠道打包實戰:基於友盟統計的 productFlavors 方案 一款 App 上架時往往需要針對不同應用市場產出多個渠道包,以便統計各渠
Android Drawable 與 XML 資源應用指南
Android Drawable 與 XML 資源應用指南 Drawable 是 Android 中用來描述可繪製內容的抽象概念。它可以是圖片、顏色、形狀、圖層組合、裁剪
Android 布局技巧:用 weightSum 與 layout_weight 讓按鈕精準居中並佔據一半寬度
一個屬性組合,解決「居中 + 佔半」的經典需求 本文原創地址:https://www.cnblogs.com/wuyudong/p/5898403.html,轉載請標註來
Android Studio 安裝 Genymotion 插件與常見問題
Android Studio 安裝 Genymotion 插件 Genymotion 是常用的 Android 模擬器之一,效能與操作體驗通常優於部分內建模擬器。本文說明