Android Gradle 外掛程式 3.0.0 版 (2017 年 10 月)

Android Gradle 外掛程式 3.0.0 版內含多項變更,希望藉此解決大型專案的效能問題。

舉例來說,在最多含有 130 個模組以及大量外部依附元件 (但是沒有程式碼或資源) 的範例架構專案中,您可以獲得經過改善的效能體驗,與以下項目能夠達成的效能相似:

Android 外掛程式版本 + Gradle 版本 Android 外掛程式 2.2.0 + Gradle 2.14.1 Android 外掛程式 2.3.0 + Gradle 3.3 Android 外掛程式 3.0.0 + Gradle 4.1
設定 (如執行 ./gradlew --help) 最多 2 分鐘 最多 9 秒 最多 2.5 秒
單行 Java 變更 (實作變更) 最多 2 分鐘 15 秒 最多 29 秒 最多 6.4 秒

部分變更內容會使現有版本無法運作。如果您有意使用新的外掛程式,請務必先考量遷移專案所需的成本。

如果您並未感受到上述的效能改善情形,請回報錯誤,並透過 Gradle 分析器附上您的建構作業追蹤記錄。

這個版本的 Android 外掛程式須使用下列項目:

  • Gradle 4.1 以上版本。詳情請參閱「更新 Gradle」一節。
  • 建構工具 26.0.2 以上版本。本次更新後,您不再需要指定 SDK 建構工具的版本,外掛程式預設會使用最低需求版本。因此,您現在可以移除 android.buildToolsVersion 屬性了。

3.0.1 版 (2017 年 11 月)

這個次要更新版本支援 Android Studio 3.0.1 版,並提供多項一般錯誤修正和效能改善項目。

最佳化

  • 透過更精細的工作圖,為含有多個模組的專案提供更優異的平行處理能力。
  • 變更依附元件時,Gradle 不會重新編譯無法存取該依附元件 API 的模組,藉此提升建構速度。您應該使用以下的 Gradle 新依附元件設定,對哪些依附元件會流失 API 到其他模組設限:implementationapicompileOnlyruntimeOnly
  • 按照類別進行 DEX 處理,能夠加快漸進式建構速度。現在每個類別都會編譯到各自的 DEX 檔案中,只有經過修改的類別才會重新進行 DEX 處理。minSdkVersion 設為 20 以下且使用舊版 multidex 的應用程式,建構速度也應有所提升。
  • 透過最佳化特定工作以便使用快取輸出內容,藉此提升建構速度。您必須先啟用 Gradle 建構快取,才能受惠於這項最佳化功能。
  • 改善 AAPT2 漸進式資源處理,現在系統預設啟用此功能。如果您在使用 AAPT2 時遇到問題,請回報錯誤。您也可以停用 AAPT2,方法是在 gradle.properties 檔案中設定 android.enableAapt2=false,然後透過指令列執行 ./gradlew --stop,重新啟動 Gradle Daemon。

新功能

  • 可偵測變化版本的依附元件管理方式。現在建構特定模組的變化版本時,外掛程式會自動比對本機程式庫模組依附元件的變化版本與目前建構模組的變化版本。
  • 提供新的 Feature 模組外掛程式,以便支援 Android 免安裝應用程式和 Android 免安裝應用程式 SDK (您可以透過 SDK Manager 進行下載)。如要進一步瞭解如何使用新的外掛程式建立 Feature 模組,請參閱具有多功能的免安裝應用程式結構相關說明。
  • 為使用某些 Java 8 語言功能和 Java 8 程式庫提供內建支援功能。我們已淘汰且不再需要使用 Jack,請先停用 Jack,以便使用預設工具鏈內建的改良版 Java 8 支援功能。詳情請參閱使用 Java 8 語言功能相關說明。
  • 新增使用 Android Test Orchestrator 執行測試的支援功能,讓您能夠透過應用程式自身叫用的 Instrumentation,個別執行每項測試。由於每項測試都會在自己的 Instrumentation 執行個體中進行,因此在測試之間任何共用的狀態都不會累積在裝置的 CPU 或記憶體內。即使有一個測試停止運作,也只會終止自己的 Instrumentation 執行個體,其他測試依然可以正常執行。

    • 新增 testOptions.execution 以便決定是否要使用裝置端測試自動化調度管理。如果您想使用 Android Test Orchestrator,則必須指定 ANDROID_TEST_ORCHESTRATOR (如下所示)。根據預設,此屬性會設為 HOST,這樣會停用裝置端的自動化調度管理,也是執行測試的標準方法。

    Groovy

            android {
              testOptions {
                execution 'ANDROID_TEST_ORCHESTRATOR'
              }
            }
            

    Kotlin

            android {
              testOptions {
                execution = "ANDROID_TEST_ORCHESTRATOR"
              }
            }
            
  • 您可以利用新的 androidTestUtil 依附元件設定,在執行檢測設備測試之前安裝其他測試輔助工具 APK (例如 Android Test Orchestrator):

    Groovy

            dependencies {
              androidTestUtil 'com.android.support.test:orchestrator:1.0.0'
              ...
            }
            

    Kotlin

            dependencies {
              androidTestUtil("com.android.support.test:orchestrator:1.0.0")
              ...
            }
            
  • 新增 testOptions.unitTests.includeAndroidResources,用於支援需要使用 Android 資源 (例如 Robolectric) 的單元測試。將此屬性設為 true 後,外掛程式會在執行單元測試之前合併資源、資產及資訊清單。接著,測試就能檢查類別路徑內的 com/android/tools/test_config.properties,找出下列鍵:

    • android_merged_assets:合併的素材資源目錄絕對路徑。

      注意:程式庫模組的合併素材資源不含依附元件的素材資源 (請參閱問題 #65550419)。

    • android_merged_manifest:合併的資訊清單檔案絕對路徑。

    • android_merged_resources:合併的資源目錄絕對路徑,其中包含該模組及其所有依附元件的全部資源。

    • android_custom_package:最終 R 類別的套件名稱。如果您以動態方式修改應用程式 ID,則這個套件名稱可能會與應用程式資訊清單中的 package 屬性不相符。

  • 支援將字型當做資源,這是 Android 8.0 (API 級別 26) 中推出的新功能。
  • 支援 Android 免安裝應用程式 SDK 1.1 以上版本的語言特定 APK。
  • 您現在可以變更外部原生建構專案的輸出目錄,如下所示:

    Groovy

            android {
                ...
                externalNativeBuild {
                    // For ndk-build, instead use the ndkBuild block.
                    cmake {
                        ...
                        // Specifies a relative path for outputs from external native
                        // builds. You can specify any path that's not a subdirectory
                        // of your project's temporary build/ directory.
                        buildStagingDirectory "./outputs/cmake"
                    }
                }
            }
            

    Kotlin

            android {
                ...
                externalNativeBuild {
                    // For ndk-build, instead use the ndkBuild block.
                    cmake {
                        ...
                        // Specifies a relative path for outputs from external native
                        // builds. You can specify any path that's not a subdirectory
                        // of your project's temporary build/ directory.
                        buildStagingDirectory = "./outputs/cmake"
                    }
                }
            }
            
  • 現在透過 Android Studio 建構原生專案時可以使用 CMake 3.7 以上版本了。
  • 您可利用新的 lintChecks 依附元件設定,建構用於定義自訂 Lint 規則的 JAR,並將其封裝至 AAR 和 APK 專案。

    自訂程式碼檢查規則必須屬於另一個專案,該專案可輸出單一 JAR 且僅包含 compileOnly 依附元件。這樣一來,其他應用程式和程式庫模組就能透過 lintChecks 設定依附該 Lint 專案:

    Groovy

            dependencies {
                // This tells the Gradle plugin to build ':lint-checks' into a lint.jar file
                // and package it with your module. If the module is an Android library,
                // other projects that depend on it automatically use the lint checks.
                // If the module is an app, lint includes these rules when analyzing the app.
                lintChecks project(':lint-checks')
            }
            

    Kotlin

            dependencies {
                // This tells the Gradle plugin to build ':lint-checks' into a lint.jar file
                // and package it with your module. If the module is an Android library,
                // other projects that depend on it automatically use the lint checks.
                // If the module is an app, lint includes these rules when analyzing the app.
                lintChecks(project(":lint-checks"))
            }
            

行為變更

  • Android 外掛程式 3.0.0 版會移除某些 API,因此使用這些 API 的建構作業將會失敗。舉例來說,您無法再使用 Variants API 存取 outputFile() 物件,也無法使用 processManifest.manifestOutputFile() 取得各個變化版本的資訊清單檔案。詳情請參閱 API 變更
  • 您不再需要指定建構工具的版本,因此現在可以移除 android.buildToolsVersion 屬性了。根據預設,外掛程式會自動根據您使用的 Android 外掛程式版本,採用建構工具的最低需求版本。
  • 您現在可以透過 buildTypes 區塊啟用/停用 PNG 壓縮功能,如下所示。所有版本皆預設啟用 PNG 壓縮功能,但偵錯版本除外,這是因為如果專案內含大量 PNG 檔案,便會增加建構時間。因此,為了縮短其他建構類型的建構時間,您應停用 PNG 壓縮功能,或者將圖片轉換為 WebP

    Groovy

          android {
            buildTypes {
              release {
                // Disables PNG crunching for the release build type.
                crunchPngs false
              }
            }
          }
          

    Kotlin

          android {
            buildTypes {
              release {
                // Disables PNG crunching for the release build type.
                isCrunchPngs = false
              }
            }
          }
          
  • Android 外掛程式現在會自動建構設定在外部 CMake 專案中的執行檔目標。
  • 您現在必須使用 annotationProcessor 依附元件設定,在註解處理工具的類別路徑中新增註解處理工具
  • 使用已淘汰的 ndkCompile 現在會受到進一步限制。建議您改用 CMake 或 ndk-build 編譯要封裝至 APK 的原生程式碼。詳情請參閱從 ndkcompile 遷移相關說明。