android studio+grade配置構建

來源:互聯網
上載者:User

標籤:多個   x11   螢幕   contain   詳細   規則   開發人員   however   替換   

  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 應用模組的構建流程通常依循下列步驟:
  
  編譯器將您的原始碼轉換成 DEX(Dalvik Executable) 檔案(其中包括運行在 Android 裝置上的位元組碼),將所有其他內容轉換成已編譯資源。
  
  APK 打包器將 DEX 檔案和已編譯資源合并成單個 APK。不過,必須先簽署 APK,才能將應用安裝並部署到 Android 裝置上。
  
  APK 打包器使用調試或發布密鑰庫簽署您的 APK:
  
  如果您構建的是調試版本的應用(即專用於測試和分析的應用),打包器會使用調試密鑰庫簽署您的應用。Android Studio 自動使用調試密鑰庫配置新項目。
  
  如果您構建的是打算向外發布的發布版本應用,打包器會使用發布密鑰庫簽署您的應用。要建立發布密鑰庫,請閱讀在 Android Studio 中籤署您的應用。
  
  在產生最終 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 www.hbwfjx.cn/ 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 www.lieqibiji.com 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 www.120xh.cn 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 {www.yszx11.cn} block available to specify
  
  * Android-specific build options.
  
  */
  
  apply plugin: ‘com.android. www.baqist.cn/ application‘
  
  /**
  
  * The android {} block is where you www.hsl85.cn/ 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 使用同一優先順序,這樣每個構建變體都能在最終清單中定義不同的組件或許可權。如需瞭解有關建立自訂來源集的更多資訊,請轉至建立用於構建變體的源集。

android studio+grade配置構建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.