透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android Gradle 外掛程式 2.2.0 版 (2016 年 9 月)
- 依附元件:
|
最低版本 |
預設版本 |
附註 |
Gradle |
2.14.1 |
2.14.1 |
詳情請參閱「更新 Gradle」一節。 |
SDK 版本工具 |
23.0.2 |
23.0.2 |
安裝或設定 SDK 版本工具。 |
- 新功能:
-
- 使用 Gradle 2.14.1 版,當中包含多項效能改善項目和新功能,並修正在本機使用 Gradle Daemon 時允許權限提升的安全漏洞。詳情請參閱 Gradle 版本資訊。
- Gradle 現在會使用
externalNativeBuild {}
DSL,讓您連結至原生來源並透過 CMake 或 ndk-build 編譯原生資料庫。建構原生資料庫後,Gradle 會將其封裝至 APK。如要進一步瞭解如何搭配 Gradle 使用 CMake 和 ndk-build,請參閱在專案中新增 C 和 C++ 程式碼相關說明。
- 現在 Gradle 會在您透過指令列執行建構作業時,嘗試自動下載任何缺少的 SDK 元件或專案依附的更新項目。詳情請參閱「透過 Gradle 自動下載缺少的套件」一節。
- Gradle 提供新的實驗性快取功能,透過 DEX 前置處理、儲存以及重新利用經過 DEX 前置處理的程式庫版本,加快建構時間。如要進一步瞭解如何使用這項實驗功能,請參閱建構快取指南。
- 採用新的預設封裝管道,用單一工作即可控制壓縮、簽署及 zipalign 處理,藉此改善建構效能。如要改回使用舊版封裝工具,只要在
gradle.properties
檔案中加入 android.useOldPackaging=true
即可。使用新的封裝工具時,無法執行 zipalignDebug
工作。不過,您可以呼叫 createZipAlignTask(String taskName, File inputFile, File
outputFile)
方法,自行建立這項工作。
- 除了傳統 JAR 簽署外,APK 簽署現在也會使用 APK Signature Scheme v2。所有 Android 平台都會接受最終產生的 APK。如果在簽署後修改這些 APK 的任何內容,便會使其 v2 簽署失效,也無法安裝在裝置上。如要停用這項功能,請在模組層級的
build.gradle
檔案中加入以下內容:
Groovy
android {
...
signingConfigs {
config {
...
v2SigningEnabled false
}
}
}
Kotlin
android {
...
signingConfigs {
create("config") {
...
v2SigningEnabled = false
}
}
}
- 您現在可以針對 multidex 建構作業使用 ProGuard 規則,決定 Gradle 要將哪些類別編譯至應用程式的「主要」DEX 檔案。由於 Android 系統會在啟動應用程式時先載入主要 DEX 檔案,因此您可以將特定類別編譯至主要 DEX 檔案,藉此在啟動應用程式時優先執行這些類別。建立主要 DEX 檔案專用的 ProGuard 設定檔之後,請透過
buildTypes.multiDexKeepProguard
將設定檔的路徑傳送給 Gradle。這個 DSL 和 buildTypes.proguardFiles
的使用方式不同,後者可為應用程式提供一般的 ProGuard 規則,且不會為主要 DEX 檔案指定類別。
- 新增對
android:extractNativeLibs
旗標的支援功能,可縮減應用程式在裝置上的安裝大小。只要在應用程式資訊清單的 <application>
元素中將此標記設為 false
,Gradle 就會將未壓縮及相對應的原生資料庫版本封裝至 APK。這種做法不僅可防止 PackageManager
在安裝期間將原生資料庫從 APK 複製到裝置的檔案系統,還有縮減應用程式差異更新檔案大小的額外優勢。
- 您現在可以為變種版本指定
versionNameSuffix
及 applicationIdSuffix
。(問題 59614)
-
變更內容:
-
-
getDefaultProguardFile
現在會傳回 Gradle 適用的 Android 外掛程式提供的預設 ProGuard 檔案,不再使用 Android SDK 中的檔案。
- 改善 Jack 編譯器的效能和功能。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-30 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-30 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nAndroid Gradle Plugin 2.2.0 (September 2016)\n\nDependencies:\nNew:\n:\n - Uses Gradle 2.14.1, which includes performance improvements and new features, and fixes a security vulnerability that allows local privilege escalation when using the Gradle daemon. For more details, see the [Gradle release notes](https://docs.gradle.org/2.14.1/release-notes).\n - Using the [`externalNativeBuild {}`](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ExternalNativeBuild.html) DSL, Gradle now lets you link to your native sources and compile native libraries using CMake or ndk-build. After building your native libraries, Gradle packages them into your APK. To learn more about using CMake and ndk-build with Gradle, read [Add C and C++ Code to Your\n Project](/studio/projects/add-native-code).\n - When you [run a\n build from the command line](/studio/build/building-cmdline), Gradle now attempts to auto-download any missing SDK components or updates that your project depends on. To learn more, read [Auto-download\n missing packages with Gradle](/studio/intro/update#download-with-gradle).\n - A new experimental caching feature lets Gradle speed up build times by pre-dexing, storing, and reusing the pre-dexed versions of your libraries. To learn more about using this experimental feature, read the [Build\n Cache](/studio/build/build-cache) guide.\n - Improves build performance by adopting a new default packaging pipeline which handles zipping, signing, and [zipaligning](/studio/command-line/zipalign) in one task. You can revert to using the older packaging tools by adding `android.useOldPackaging=true` to your `gradle.properties` file. While using the new packaging tool, the `zipalignDebug` task is not available. However, you can create one yourself by calling the `createZipAlignTask(String taskName, File inputFile, File\n outputFile)` method.\n - APK signing now uses [APK Signature Scheme\n v2](/about/versions/nougat/android-7.0#apk_signature_v2) in addition to traditional JAR signing. All Android platforms accept the resulting APKs. Any modification to these APKs after signing invalidates their v2 signatures and prevents installation on a device. To disable this feature, add the following to your module-level `build.gradle` file: \n\n Groovy \n\n ```groovy\n android {\n ...\n signingConfigs {\n config {\n ...\n v2SigningEnabled false\n }\n }\n }\n \n ```\n\n Kotlin \n\n ```kotlin\n android {\n ...\n signingConfigs {\n create(\"config\") {\n ...\n v2SigningEnabled = false\n }\n }\n }\n \n ```\n - For multidex builds, you can now use ProGuard rules to determine which classes Gradle should compile into your app's *main* DEX file. Because the Android system loads the main DEX file first when starting your app, you can prioritize certain classes at startup by compiling them into the main DEX file. After you create a ProGuard configuration file specifically for your main DEX file, pass the configuration file's path to Gradle using [buildTypes.multiDexKeepProguard](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.BuildType.html#com.android.build.gradle.internal.dsl.BuildType:multiDexKeepProguard). Using this DSL is different from using [`buildTypes.proguardFiles`](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.BuildType.html#com.android.build.gradle.internal.dsl.BuildType:proguardFiles(java.lang.Object[])), which provides general ProGuard rules for your app and does not specify classes for the main DEX file.\n - Adds support for the `android:extractNativeLibs` flag, which can reduce the size of your app when you install it on a device. When you set this flag to `false` in the [`\u003capplication\u003e`](/guide/topics/manifest/application-element) element of your app manifest, Gradle packages uncompressed and aligned versions of your native libraries with your APK. This prevents [`PackageManager`](/reference/android/content/pm/PackageManager) from copying out your native libraries from the APK to the device's file system during installation and has the added benefit of making delta updates of your app smaller.\n - You can now specify [`versionNameSuffix`](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ProductFlavor.html#com.android.build.gradle.internal.dsl.ProductFlavor:versionNameSuffix) and [`applicationIdSuffix`](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ProductFlavor.html#com.android.build.gradle.internal.dsl.ProductFlavor:applicationIdSuffix) for product flavors. ([Issue 59614](http://b.android.com/59614))\n\n\nChanges:\n:\n - `getDefaultProguardFile` now returns the default ProGuard files that Android plugin for Gradle provides and no longer uses the ones in the Android SDK.\n - Improved Jack compiler performance and features:\n - Jack now supports Jacoco test coverage when setting [testCoverageEnabled](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.BuildType.html#com.android.build.gradle.internal.dsl.BuildType:testCoverageEnabled) to `true`.\n - Improved support for annotation processors. Annotation processors on your classpath, such as any `compile` dependencies, are automatically applied to your build. You can also specify an annotation processor in your build and pass arguments by using the [`javaCompileOptions.annotationProcessorOptions {}`](https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.AnnotationProcessorOptions.html) DSL in your module-level `build.gradle` file: \n\n Groovy \n\n ```groovy\n android {\n ...\n defaultConfig {\n ...\n javaCompileOptions {\n annotationProcessorOptions {\n className 'com.example.MyProcessor'\n // Arguments are optional.\n arguments = [ foo : 'bar' ]\n }\n }\n }\n }\n \n ```\n\n Kotlin \n\n ```kotlin\n android {\n ...\n defaultConfig {\n ...\n javaCompileOptions {\n annotationProcessorOptions {\n className = \"com.example.MyProcessor\"\n // Arguments are optional.\n arguments(mapOf(foo to \"bar\"))\n }\n }\n }\n }\n \n ```\n\n\n If you want to apply an annotation processor at compile\n time but not include it in your APK, use the\n `annotationProcessor` dependency scope: \n\n Groovy \n\n ```groovy\n dependencies {\n compile 'com.google.dagger:dagger:2.0'\n annotationProcessor 'com.google.dagger:dagger-compiler:2.0'\n // or use buildVariantAnnotationProcessor to target a specific build variant\n }\n \n ```\n\n Kotlin \n\n ```kotlin\n dependencies {\n implementation(\"com.google.dagger:dagger:2.0\")\n annotationProcessor(\"com.google.dagger:dagger-compiler:2.0\")\n // or use buildVariantAnnotationProcessor to target a specific build variant\n }\n \n ```\n - For a list of parameters you can set, run the following from the command line: \n\n ```\n java -jar /build-tools/jack.jar --help-properties\n ```\n - By default, if the Gradle daemon's heap size is at least 1.5 GB, Jack now runs in the same process as Gradle. To adjust the daemon heap size, add the following to your `gradle.properties` file:\n\n \u003cbr /\u003e\n\n ```\n # This sets the daemon heap size to 1.5GB.\n org.gradle.jvmargs=-Xmx1536M\n ```\n\n \u003cbr /\u003e\n\n\n\u003cbr /\u003e"]]