1. 程式人生 > >android studio+grade配置構建

android studio+grade配置構建

Android 構建系統編譯應用資源和原始碼,然後將它們打包成可供您測試、部署、簽署和分發的 APK。Android Studio 使用 Gradle 這一高階構建工具包來自動化執行和管理構建流程,同時也允許您定義靈活的自定義構建配置。每個構建配置均可自行定義一組程式碼和資源,同時對所有應用版本共有的部分加以重複利用。Android Plugin for Gradle 與這個構建工具包協作,共同提供專用於構建和測試 Android 應用的流程和可配置設定。

Gradle 和 Android 外掛獨立於 Android Studio 執行。這意味著,您可以在 Android Studio 內、使用計算機上的命令列工具或在未安裝 Android Studio 的計算機(例如持續性整合伺服器)上構建 Android 應用。如果您不使用 Android Studio,可以學習如何

從命令列構建和執行您的應用。無論您是從命令列、在遠端計算機上還是使用 Android Studio 構建專案,構建的輸出都相同。

:由於 Gradle 和 Android 外掛獨立於 Android Studio 執行,您需要單獨更新構建工具。請閱讀版本說明,瞭解如何更新 Gradle 和 Android 外掛

Android 構建系統非常靈活,讓您能夠在不修改應用核心原始檔的情況下執行自定義構建配置。本章幫助您瞭解 Android 構建系統的工作原理,以及它如何幫助您對多個構建配置進行自定義和自動化處理。如果您只想瞭解有關部署應用的更多資訊,請參閱在 Android Studio 中構建和執行專案

。要立即開始使用 Android Studio 建立自定義構建配置,請參閱配置構建變體

構建流程

構建流程涉及許多將您的專案轉換成 Android 應用軟體包 (APK) 的工具和流程。構建流程非常靈活,因此瞭解它的一些底層工作原理會很有幫助。

圖 1. 典型 Android 應用模組的構建流程。

如圖 1 所示,典型 Android 應用模組的構建流程通常依循下列步驟:

  1. 編譯器將您的原始碼轉換成 DEX(Dalvik Executable) 檔案(其中包括執行在 Android 裝置上的位元組碼),將所有其他內容轉換成已編譯資源。
  2. APK 打包器將 DEX 檔案和已編譯資源合併成單個 APK。不過,必須先簽署 APK,才能將應用安裝並部署到 Android 裝置上。
  3. APK 打包器使用除錯或釋出金鑰庫簽署您的 APK:
    1. 如果您構建的是除錯版本的應用(即專用於測試和分析的應用),打包器會使用除錯金鑰庫簽署您的應用。Android Studio 自動使用除錯金鑰庫配置新專案。
    2. 如果您構建的是打算向外釋出的釋出版本應用,打包器會使用釋出金鑰庫簽署您的應用。要建立釋出金鑰庫,請閱讀在 Android Studio 中籤署您的應用
  4. 在生成最終 APK 之前,打包器會使用 zipalign 工具對應用進行優化,減少其在裝置上執行時的記憶體佔用。

構建流程結束時,您將獲得可用來進行部署、測試的除錯 APK,或者可用來發布給外部使用者的釋出 APK。

自定義構建配置

Gradle 和 Android 外掛可幫助您完成以下方面的構建配置:

構建型別
構建型別定義 Gradle 在構建和打包您的應用時使用的某些屬性,通常針對開發生命週期的不同階段進行配置。例如,除錯構建型別支援除錯選項,使用除錯金鑰簽署 APK;而釋出構建型別則可壓縮、混淆 APK 以及使用釋出金鑰簽署 APK 進行分發。您必須至少定義一個構建型別才能構建應用 - Android Studio 預設情況下會建立除錯和釋出構建型別。要開始為應用自定義打包設定,請學習如何配置構建型別
產品風味
產品風味代表您可以釋出給使用者的不同應用版本,例如免費和付費的應用版本。您可以將產品風味自定義為使用不同的程式碼和資源,同時對所有應用版本共有的部分加以共享和重複利用。產品風味是可選項,並且您必須手動建立。要開始建立不同的應用版本,請學習如何配置產品風味
構建變體
構建變體是構建型別與產品風味的交叉產物,是 Gradle 在構建應用時使用的配置。您可以利用構建變體在開發時構建產品風味的除錯版本,或者構建已簽署的產品風味釋出版本進行分發。您並不直接配置構建變體,而是配置組成變體的構建型別和產品風味。建立附加構建型別或產品風味也會建立附加構建變體。要了解如何建立和管理構建變體,請閱讀配置構建變體概覽。
清單條目
您可以為構建變體配置中清單檔案的一些屬性指定值。這些構建值會替換清單檔案中的現有值。如果您想為模組生成多個 APK,讓每一個 APK 檔案都具有不同的應用名稱、最低 SDK 版本或目標 SDK 版本,便可運用這一技巧。存在多個清單時,Gradle 會合併清單設定
依賴項
構建系統管理來自您的本地檔案系統以及來自遠端儲存區的專案依賴項。這樣一來,您就不必手動搜尋、下載依賴項的二進位制檔案包以及將它們複製到專案目錄內。要了解更多資訊,請學習如何宣告依賴項
簽署
構建系統讓您能夠在構建配置中指定簽署設定,並可在構建過程中自動簽署您的 APK。構建系統通過使用已知憑據的預設金鑰和證書籤署除錯版本,以避免在構建時提示密碼。除非您為此構建顯式定義簽署配置,否則,構建系統不會簽署釋出版本。如果您沒有釋出金鑰,可以按簽署您的應用中所述生成一個。
ProGuard
構建系統讓您能夠為每個構建變體指定不同的 ProGuard 規則檔案。構建系統可在構建過程中執行 ProGuard 對類進行壓縮和混淆處理。
APK 拆分
構建系統讓您能夠自動構建不同的 APK,並且每個 APK 只包含特定螢幕密度或應用二進位制介面 (ABI) 所需的程式碼和資源。如需瞭解詳細資訊,請參閱配置 APK 拆分

構建配置檔案

建立自定義構建配置需要您對一個或多個構建配置檔案(或 build.gradle 檔案)進行更改。這些純文字檔案使用域特定語言 (DSL) 以 Groovy 語言描述和操作構建邏輯,後者是一種適用於 Java 虛擬機器 (JVM) 的動態語言。您無需瞭解 Groovy 便可開始配置構建,因為 Android Plugin for Gradle 引入了您需要的大多數 DSL 元素。如需瞭解有關 Android 外掛 DSL 的更多資訊,請閱讀 DSL 參考文件

開始新專案時,Android Studio 會自動為您建立其中的部分檔案(如圖 2 所示),併為它們填充合理的預設值

圖 2. Android 應用模組的預設專案結構。

有幾個 Gradle 構建配置檔案是 Android 應用標準專案結構的組成部分。您必須瞭解其中每一個檔案的範圍和用途及其應定義的基本 DSL 元素,才能著手配置構建。

Gradle 設定檔案

settings.gradle 檔案位於專案根目錄,用於指示 Gradle 在構建應用時應將哪些模組包括在內。對大多數專案而言,該檔案很簡單,只包括以下內容:

include ‘:app

不過,多模組專案需要指定應包括在最終構建之中的每個模組。

頂級構建檔案

頂級 build.gradle 檔案位於專案根目錄,用於定義適用於專案中所有模組的構建配置。預設情況下,這個頂級構建檔案使用 buildscript {} 程式碼塊來定義專案中所有模組共用的 Gradle 儲存區依賴項。以下程式碼示例描述的預設設定和 DSL 元素可在新建專案後的頂級 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.2 as a classpath dependency.
     */

    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'}}/**
 * 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()}}

模組級構建檔案

模組級 build.gradle 檔案位於每個 <project>/<module>/ 目錄,用於配置適用於其所在模組的構建設定。您可以通過配置這些構建設定來提供自定義打包選項(例如附加構建型別和產品風味),以及替換 main/ 應用清單或頂級 build.gradle 檔案中的設定。

以下這個示例 Android 應用模組 build.gradle 檔案概述了您應該瞭解的部分基本 DSL 元素和設定。

/**
 * 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 25
  buildToolsVersion "25.0.3"/**
   * 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 25// 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:25.3.1'
    compile fileTree(dir:'libs', include:['*.jar'])}

Gradle 屬性檔案

Gradle 還包括兩個屬性檔案,位於專案根目錄,可用於指定適用於 Gradle 構建工具包本身的設定:

gradle.properties
您可以在其中配置專案範圍 Gradle 設定,例如 Gradle 後臺程序的最大堆大小。如需瞭解詳細資訊,請參閱構建環境
local.properties
為構建系統配置本地環境屬性,例如 SDK 安裝路徑。由於該檔案的內容由 Android Studio 自動生成並且專用於本地開發者環境,因此您不應手動修改該檔案,或將其納入您的版本控制系統。

將專案與 Gradle 檔案同步

當您在專案中對構建配置檔案進行更改時,Android Studio 會要求您同步專案檔案,以便其匯入您的構建配置更改並執行一些檢查來確保您的配置不會造成構建錯誤。

要同步專案檔案,您可以點選做出更改後出現的通知欄中的 Sync Now(如圖 3 所示),或者點選選單欄中的 Sync Project 。如果 Android Studio 通知配置出現錯誤,例如:您的原始碼使用了只有在 compileSdkVersion 以上的 API 級別中才會提供的 API 功能,會顯示 Messages 視窗,具體描述該問題。

圖 3. 在 Android Studio 中將專案與構建配置檔案同步。

源集

Android Studio 按邏輯關係將每個模組的原始碼和資源分組為源集。模組的 main/ 源集包括其所有構建變體共用的程式碼和資源。其他源集目錄為可選項,在您配置新的構建變體時,Android Studio 不會自動為您建立這些目錄。不過,建立類似於 main/ 的源集有助於讓 Gradle 只應在構建特定應用版本時使用的檔案和資源井然有序:

src/main/
此源集包括所有構建變體共用的程式碼和資源。
src/<buildType>/
建立此源集可加入特定構建型別專用的程式碼和資源。
src/<productFlavor>/
建立此源集可加入特定產品風味專用的程式碼和資源。
src/<productFlavorBuildType>/
建立此源集可加入特定構建變體專用的程式碼和資源。

例如,要生成應用的“完整除錯”版本,構建系統需要合併來自以下源集的程式碼、設定和資源:

  • src/fullDebug/(構建變體源集)
  • src/debug/(構建型別源集)
  • src/full/(產品風味源集)
  • src/main/(主源集)

:當您在 Android Studio 中使用 File > New 選單選項新建檔案或目錄時,可以針對特定源集進行建立。可供您選擇的源集取決於您的構建配置,如果所需目錄尚不存在,Android Studio 會自動建立。

如果不同源集包含同一檔案的不同版本,Gradle 將按以下優先順序決定使用哪一個檔案(左側源集替換右側源集的檔案和設定):

構建變體 > 構建型別 > 產品風味 > 主源集 > 庫依賴項

這樣一來,Gradle 便可使用專用於您試圖構建的構建變體的檔案,同時對與其他應用版本共用的 Activity、應用邏輯和資源加以重複利用。在合併多個清單時,Gradle 使用同一優先順序,這樣每個構建變體都能在最終清單中定義不同的元件或許可權。如需瞭解有關建立自定義源集的更多資訊,請轉至建立用於構建變體的源集