文章摘要
1执泰、 Gradle 構(gòu)建流程
2香追、自定義構(gòu)建配置
3、構(gòu)建配置文件
4坦胶、Gradle 設(shè)置文件
5、頂級構(gòu)建文件
6晴楔、模塊級構(gòu)建文件
7顿苇、Gradle 屬性文件
8、將項(xiàng)目與 Gradle 文件同步
9税弃、源集
Android 構(gòu)建系統(tǒng)編譯應(yīng)用資源和源代碼纪岁,然后將它們打包成可供您測試、部署则果、簽署和分發(fā)的 APK幔翰。Android Studio 使用 Gradle 這一高級構(gòu)建工具包來自動(dòng)化執(zhí)行和管理構(gòu)建流程,同時(shí)也允許您定義靈活的自定義構(gòu)建配置西壮。每個(gè)構(gòu)建配置均可自行定義一組代碼和資源遗增,同時(shí)對所有應(yīng)用版本共有的部分加以重復(fù)利用。Android Plugin for Gradle 與這個(gè)構(gòu)建工具包協(xié)作款青,共同提供專用于構(gòu)建和測試 Android 應(yīng)用的流程和可配置設(shè)置做修。
Gradle 和 Android 插件獨(dú)立于 Android Studio 運(yùn)行。這意味著抡草,您可以在 Android Studio 內(nèi)饰及、使用計(jì)算機(jī)上的命令行工具或在未安裝 Android Studio 的計(jì)算機(jī)(例如持續(xù)性集成服務(wù)器)上構(gòu)建 Android 應(yīng)用。如果您不使用 Android Studio康震,可以學(xué)習(xí)如何從命令行構(gòu)建和運(yùn)行您的應(yīng)用燎含。無論您是從命令行、在遠(yuǎn)程計(jì)算機(jī)上還是使用 Android Studio 構(gòu)建項(xiàng)目腿短,構(gòu)建的輸出都相同屏箍。
一绘梦、構(gòu)建流程
構(gòu)建流程涉及許多將您的項(xiàng)目轉(zhuǎn)換成 Android 應(yīng)用軟件包 (APK) 的工具和流程。構(gòu)建流程非常靈活铣除,因此了解它的一些底層工作原理會(huì)很有幫助谚咬。
- 圖 1. 典型 Android 應(yīng)用模塊的構(gòu)建流程。
如圖 1 所示尚粘,典型 Android 應(yīng)用模塊的構(gòu)建流程通常依循下列步驟:
1择卦、編譯器將您的源代碼轉(zhuǎn)換成 DEX(Dalvik Executable) 文件(其中包括運(yùn)行在 Android 設(shè)備上的字節(jié)碼),將所有其他內(nèi)容轉(zhuǎn)換成已編譯資源郎嫁。
2秉继、APK 打包器將 DEX 文件和已編譯資源合并成單個(gè) APK。不過泽铛,必須先簽署 APK尚辑,才能將應(yīng)用安裝并部署到 Android 設(shè)備上。
-
3盔腔、APK 打包器使用調(diào)試或發(fā)布密鑰庫簽署您的 APK:
- 如果您構(gòu)建的是調(diào)試版本的應(yīng)用(即專用于測試和分析的應(yīng)用)杠茬,打包器會(huì)使用調(diào)試密鑰庫簽署您的應(yīng)用。Android Studio 自動(dòng)使用調(diào)試密鑰庫配置新項(xiàng)目弛随。
- 如果您構(gòu)建的是打算向外發(fā)布的發(fā)布版本應(yīng)用瓢喉,打包器會(huì)使用發(fā)布密鑰庫簽署您的應(yīng)用。要?jiǎng)?chuàng)建發(fā)布密鑰庫舀透,請閱讀在 Android Studio 中簽署您的應(yīng)用栓票。
4、在生成最終 APK 之前愕够,打包器會(huì)使用 zipalign 工具對應(yīng)用進(jìn)行優(yōu)化走贪,減少其在設(shè)備上運(yùn)行時(shí)的內(nèi)存占用。
構(gòu)建流程結(jié)束時(shí)惑芭,您將獲得可用來進(jìn)行部署坠狡、測試的調(diào)試 APK,或者可用來發(fā)布給外部用戶的發(fā)布 APK遂跟。
二擦秽、自定義構(gòu)建配置
Gradle 和 Android 插件可幫助您完成以下方面的構(gòu)建配置:
- 1、構(gòu)建類型
構(gòu)建類型定義 Gradle 在構(gòu)建和打包您的應(yīng)用時(shí)使用的某些屬性漩勤,通常針對開發(fā)生命周期的不同階段進(jìn)行配置感挥。例如,調(diào)試構(gòu)建類型支持調(diào)試選項(xiàng)越败,使用調(diào)試密鑰簽署 APK触幼;而發(fā)布構(gòu)建類型則可壓縮、混淆 APK 以及使用發(fā)布密鑰簽署 APK 進(jìn)行分發(fā)究飞。您必須至少定義一個(gè)構(gòu)建類型才能構(gòu)建應(yīng)用 - Android Studio 默認(rèn)情況下會(huì)創(chuàng)建調(diào)試和發(fā)布構(gòu)建類型置谦。要開始為應(yīng)用自定義打包設(shè)置堂鲤,請學(xué)習(xí)如何配置構(gòu)建類型。 - 2媒峡、產(chǎn)品風(fēng)味
產(chǎn)品風(fēng)味代表您可以發(fā)布給用戶的不同應(yīng)用版本瘟栖,例如免費(fèi)和付費(fèi)的應(yīng)用版本。您可以將產(chǎn)品風(fēng)味自定義為使用不同的代碼和資源谅阿,同時(shí)對所有應(yīng)用版本共有的部分加以共享和重復(fù)利用半哟。產(chǎn)品風(fēng)味是可選項(xiàng)签餐,并且您必須手動(dòng)創(chuàng)建。要開始創(chuàng)建不同的應(yīng)用版本氯檐,請學(xué)習(xí)如何配置產(chǎn)品風(fēng)味。 - 3冠摄、構(gòu)建變體
構(gòu)建變體是構(gòu)建類型與產(chǎn)品風(fēng)味的交叉產(chǎn)物,是 Gradle 在構(gòu)建應(yīng)用時(shí)使用的配置拇颅。您可以利用構(gòu)建變體在開發(fā)時(shí)構(gòu)建產(chǎn)品風(fēng)味的調(diào)試版本,或者構(gòu)建已簽署的產(chǎn)品風(fēng)味發(fā)布版本進(jìn)行分發(fā)。您并不直接配置構(gòu)建變體韵洋,而是配置組成變體的構(gòu)建類型和產(chǎn)品風(fēng)味。創(chuàng)建附加構(gòu)建類型或產(chǎn)品風(fēng)味也會(huì)創(chuàng)建附加構(gòu)建變體搪缨。要了解如何創(chuàng)建和管理構(gòu)建變體,請閱讀配置構(gòu)建變體概覽副编。 - 4、清單條目
您可以為構(gòu)建變體配置中清單文件的一些屬性指定值痹届。這些構(gòu)建值會(huì)替換清單文件中的現(xiàn)有值。如果您想為模塊生成多個(gè) APK队腐,讓每一個(gè) APK 文件都具有不同的應(yīng)用名稱、最低 SDK 版本或目標(biāo) SDK 版本柴淘,便可運(yùn)用這一技巧迫淹。存在多個(gè)清單時(shí)秘通,Gradle 會(huì)合并清單設(shè)置。 - 5敛熬、依賴項(xiàng)
構(gòu)建系統(tǒng)管理來自您的本地文件系統(tǒng)以及來自遠(yuǎn)程存儲(chǔ)區(qū)的項(xiàng)目依賴項(xiàng)肺稀。這樣一來,您就不必手動(dòng)搜索应民、下載依賴項(xiàng)的二進(jìn)制文件包以及將它們復(fù)制到項(xiàng)目目錄內(nèi)话原。要了解更多信息,請學(xué)習(xí)如何聲明依賴項(xiàng)瑞妇。 - 6稿静、簽署
構(gòu)建系統(tǒng)讓您能夠在構(gòu)建配置中指定簽署設(shè)置,并可在構(gòu)建過程中自動(dòng)簽署您的 APK辕狰。構(gòu)建系統(tǒng)通過使用已知憑據(jù)的默認(rèn)密鑰和證書簽署調(diào)試版本改备,以避免在構(gòu)建時(shí)提示密碼。除非您為此構(gòu)建顯式定義簽署配置蔓倍,否則悬钳,構(gòu)建系統(tǒng)不會(huì)簽署發(fā)布版本。如果您沒有發(fā)布密鑰偶翅,可以按簽署您的應(yīng)用中所述生成一個(gè)默勾。 - 7、ProGuard
構(gòu)建系統(tǒng)讓您能夠?yàn)槊總€(gè)構(gòu)建變體指定不同的 ProGuard 規(guī)則文件聚谁。構(gòu)建系統(tǒng)可在構(gòu)建過程中運(yùn)行 ProGuard 對類進(jìn)行壓縮和混淆處理母剥。 - 8、APK 拆分
構(gòu)建系統(tǒng)讓您能夠自動(dòng)構(gòu)建不同的 APK形导,并且每個(gè) APK 只包含特定屏幕密度或應(yīng)用二進(jìn)制界面 (ABI) 所需的代碼和資源环疼。如需了解詳細(xì)信息,請參閱配置 APK 拆分朵耕。
三炫隶、構(gòu)建配置文件
創(chuàng)建自定義構(gòu)建配置需要您對一個(gè)或多個(gè)構(gòu)建配置文件(或 build.gradle
文件)進(jìn)行更改。這些純文本文件使用域特定語言 (DSL) 以 Groovy 語言描述和操作構(gòu)建邏輯阎曹,后者是一種適用于 Java 虛擬機(jī) (JVM) 的動(dòng)態(tài)語言伪阶。您無需了解 Groovy 便可開始配置構(gòu)建,因?yàn)?Android Plugin for Gradle 引入了您需要的大多數(shù) DSL 元素栅贴。如需了解有關(guān) Android 插件 DSL 的更多信息筹误,請閱讀 DSL 參考文檔癣缅。
開始新項(xiàng)目時(shí),Android Studio 會(huì)自動(dòng)為您創(chuàng)建其中的部分文件(如圖 2 所示)祷膳,并為它們填充合理的默認(rèn)值。
- 圖 2. Android 應(yīng)用模塊的默認(rèn)項(xiàng)目結(jié)構(gòu)搀军。
有幾個(gè) Gradle 構(gòu)建配置文件是 Android 應(yīng)用標(biāo)準(zhǔn)項(xiàng)目結(jié)構(gòu)的組成部分罩句。您必須了解其中每一個(gè)文件的范圍和用途及其應(yīng)定義的基本 DSL 元素敛摘,才能著手配置構(gòu)建。
四兄淫、Gradle 設(shè)置文件
settings.gradle 文件位于項(xiàng)目根目錄,用于指示 Gradle 在構(gòu)建應(yīng)用時(shí)應(yīng)將哪些模塊包括在內(nèi)捕虽。對大多數(shù)項(xiàng)目而言,該文件很簡單房揭,只包括以下內(nèi)容:
include ‘:app’
不過捅暴,多模塊項(xiàng)目需要指定應(yīng)包括在最終構(gòu)建之中的每個(gè)模塊斩松。
五、頂級構(gòu)建文件
頂級 build.gradle 文件位于項(xiàng)目根目錄惧盹,用于定義適用于項(xiàng)目中所有模塊的構(gòu)建配置瞪讼。默認(rèn)情況下符欠,這個(gè)頂級構(gòu)建文件使用 buildscript {} 代碼塊來定義項(xiàng)目中所有模塊共用的 Gradle 存儲(chǔ)區(qū)和依賴項(xiàng)希柿。以下代碼示例描述的默認(rèn)設(shè)置和 DSL 元素可在新建項(xiàng)目后的頂級 build.gradle 文件中找到养筒。
/**
* The buildscript {} block is where you configure the repositories and
* dependencies for Gradle itself--meaning, you should not include dependencies
* for your modules here. For example, this block includes the Android plugin for
* Gradle as a dependency because it provides the additional instructions Gradle
* needs to build Android app modules.
*/
buildscript {
/**
* The repositories {} block configures the repositories Gradle uses to
* search or download the dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use local
* repositories or define your own remote repositories. The code below defines
* JCenter as the repository Gradle should use to look for its dependencies.
*/
repositories {
jcenter()
}
/**
* The dependencies {} block configures the dependencies Gradle needs to use
* to build your project. The following line adds Android Plugin for Gradle
* version 2.3.3 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
/**
* The allprojects {} block is where you configure the repositories and
* dependencies used by all modules in your project, such as third-party plugins
* or libraries. Dependencies that are not required by all the modules in the
* project should be configured in module-level build.gradle files. For new
* projects, Android Studio configures JCenter as the default repository, but it
* does not configure any dependencies.
*/
allprojects {
repositories {
jcenter()
}
}
六、模塊級構(gòu)建文件
模塊級 build.gradle 文件位于每個(gè) <project>/<module>/ 目錄渐裸,用于配置適用于其所在模塊的構(gòu)建設(shè)置。您可以通過配置這些構(gòu)建設(shè)置來提供自定義打包選項(xiàng)(例如附加構(gòu)建類型和產(chǎn)品風(fēng)味)尚氛,以及替換 main/ 應(yīng)用清單或頂級 build.gradle 文件中的設(shè)置阅嘶。
以下這個(gè)示例 Android 應(yīng)用模塊 build.gradle 文件概述了您應(yīng)該了解的部分基本 DSL 元素和設(shè)置奈懒。
/**
* The first line in the build configuration applies the Android plugin for
* Gradle to this build and makes the android {} block available to specify
* Android-specific build options.
*/
apply plugin: 'com.android.application'
/**
* The android {} block is where you configure all your Android-specific
* build options.
*/
android {
/**
* compileSdkVersion specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*
* buildToolsVersion specifies the version of the SDK build tools, command-line
* utilities, and compiler that Gradle should use to build your app. You need to
* download the build tools using the SDK Manager.
*/
compileSdkVersion 26
buildToolsVersion "26.0.0"
/**
* The defaultConfig {} block encapsulates default settings and entries for all
* build variants, and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
/**
* applicationId uniquely identifies the package for publishing.
* However, your source code should still reference the package name
* defined by the package attribute in the main/AndroidManifest.xml file.
*/
applicationId 'com.example.myapp'
// Defines the minimum API level required to run the app.
minSdkVersion 15
// Specifies the API level used to test the app.
targetSdkVersion 26
// Defines the version number of your app.
versionCode 1
// Defines a user-friendly version name for your app.
versionName "1.0"
}
/**
* The buildTypes {} block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies Proguard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the Proguard settings file.
*/
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/**
* The productFlavors {} block is where you can configure multiple product
* flavors. This allows you to create different versions of your app that can
* override defaultConfig {} with their own settings. Product flavors are
* optional, and the build system does not create them by default. This example
* creates a free and paid product flavor. Each product flavor then specifies
* its own application ID, so that they can exist on the Google Play Store, or
* an Android device, simultaneously.
*/
productFlavors {
free {
applicationId 'com.example.myapp.free'
}
paid {
applicationId 'com.example.myapp.paid'
}
}
/**
* The splits {} block is where you can configure different APK builds that
* each contain only code and resources for a supported screen density or
* ABI. You'll also need to configure your build so that each APK has a
* different versionCode.
*/
splits {
// Screen density split settings
density {
// Enable or disable the density split mechanism
enable false
// Exclude these densities from splits
exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
}
}
}
/**
* The dependencies {} block in the module-level build configuration file
* only specifies dependencies required to build the module itself.
*/
dependencies {
compile project(":lib")
compile 'com.android.support:appcompat-v7:26.0.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
七捏卓、Gradle 屬性文件
Gradle 還包括兩個(gè)屬性文件,位于項(xiàng)目根目錄遥金,可用于指定適用于 Gradle 構(gòu)建工具包本身的設(shè)置:
gradle.properties
您可以在其中配置項(xiàng)目范圍 Gradle 設(shè)置稿械,例如 Gradle 后臺進(jìn)程的最大堆大小美莫。如需了解詳細(xì)信息梯捕,請參閱構(gòu)建環(huán)境。local.properties
為構(gòu)建系統(tǒng)配置本地環(huán)境屬性襟铭,例如 SDK 安裝路徑寒砖。由于該文件的內(nèi)容由 Android Studio 自動(dòng)生成并且專用于本地開發(fā)者環(huán)境赐劣,因此您不應(yīng)手動(dòng)修改該文件隆豹,或?qū)⑵浼{入您的版本控制系統(tǒng)璃赡。
八碉考、將項(xiàng)目與 Gradle 文件同步
當(dāng)您在項(xiàng)目中對構(gòu)建配置文件進(jìn)行更改時(shí)侯谁,Android Studio 會(huì)要求您同步項(xiàng)目文件章钾,以便其導(dǎo)入您的構(gòu)建配置更改并執(zhí)行一些檢查來確保您的配置不會(huì)造成構(gòu)建錯(cuò)誤贱傀。
要同步項(xiàng)目文件,您可以點(diǎn)擊做出更改后出現(xiàn)的通知欄中的 Sync Now(如圖 3 所示)魁衙,或者點(diǎn)擊菜單欄中的 Sync Project 剖淀。如果 Android Studio 通知配置出現(xiàn)錯(cuò)誤纤房,例如:您的源代碼使用了只有在 compileSdkVersion 以上的 API 級別中才會(huì)提供的 API 功能,會(huì)顯示 Messages 窗口捌刮,具體描述該問題剑令。
- 圖 3. 在 Android Studio 中將項(xiàng)目與構(gòu)建配置文件同步吁津。
九梭依、源集
Android Studio 按邏輯關(guān)系將每個(gè)模塊的源代碼和資源分組為源集典尾。模塊的 main/ 源集包括其所有構(gòu)建變體共用的代碼和資源钾埂。其他源集目錄為可選項(xiàng)褥紫,在您配置新的構(gòu)建變體時(shí),Android Studio 不會(huì)自動(dòng)為您創(chuàng)建這些目錄部念。不過儡炼,創(chuàng)建類似于 main/ 的源集有助于讓 Gradle 只應(yīng)在構(gòu)建特定應(yīng)用版本時(shí)使用的文件和資源井然有序:
- src/main/
此源集包括所有構(gòu)建變體共用的代碼和資源乌询。 - src/<buildType>/
創(chuàng)建此源集可加入特定構(gòu)建類型專用的代碼和資源奶是。 - src/<productFlavor>/
創(chuàng)建此源集可加入特定產(chǎn)品風(fēng)味專用的代碼和資源聂沙。 - src/<productFlavorBuildType>/
創(chuàng)建此源集可加入特定構(gòu)建變體專用的代碼和資源沮趣。
例如,要生成應(yīng)用的“完整調(diào)試”版本房铭,構(gòu)建系統(tǒng)需要合并來自以下源集的代碼缸匪、設(shè)置和資源:
- src/fullDebug/(構(gòu)建變體源集)
- src/debug/(構(gòu)建類型源集)
- src/full/(產(chǎn)品風(fēng)味源集)
- src/main/(主源集)
注:當(dāng)您在 Android Studio 中使用 File > New 菜單選項(xiàng)新建文件或目錄時(shí)凌蔬,可以針對特定源集進(jìn)行創(chuàng)建⌒复剩可供您選擇的源集取決于您的構(gòu)建配置辩诞,如果所需目錄尚不存在译暂,Android Studio 會(huì)自動(dòng)創(chuàng)建外永。
如果不同源集包含同一文件的不同版本象迎,Gradle 將按以下優(yōu)先順序決定使用哪一個(gè)文件(左側(cè)源集替換右側(cè)源集的文件和設(shè)置):
構(gòu)建變體 > 構(gòu)建類型 > 產(chǎn)品風(fēng)味 > 主源集 > 庫依賴項(xiàng)
這樣一來砾淌,Gradle 便可使用專用于您試圖構(gòu)建的構(gòu)建變體的文件,同時(shí)對與其他應(yīng)用版本共用的 Activity赃春、應(yīng)用邏輯和資源加以重復(fù)利用织中。在合并多個(gè)清單時(shí)狭吼,Gradle 使用同一優(yōu)先順序殖妇,這樣每個(gè)構(gòu)建變體都能在最終清單中定義不同的組件或權(quán)限刁笙。