使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android Gradle 插件 2.2.0(2016 年 9 月)
- 依赖项:
|
最低版本 |
默认版本 |
备注 |
Gradle |
2.14.1 |
2.14.1 |
如需了解详情,请参阅更新 Gradle。 |
SDK Build Tools |
23.0.2 |
23.0.2 |
安装或配置 SDK Build Tools。 |
- 新功能:
-
- 使用 Gradle 2.14.1,其中加入了一些性能改进和新功能,并修复了在使用 Gradle 守护程序时允许获取本地特权的安全漏洞。如需了解详情,请参阅 Gradle 版本说明。
- 利用
externalNativeBuild {}
DSL,Gradle 现在可让您链接到自己的原生源代码,并使用 CMake 或 ndk-build 编译原生库。构建原生库后,Gradle 会将它们打包到您的 APK 中。如需详细了解如何在 Gradle 中使用 CMake 和 ndk-build,请参阅向您的项目添加 C 和 C++ 代码一文。
- 当您从命令行运行 build 时,Gradle 现在会尝试自动下载项目依赖但缺失的所有 SDK 组件或更新。如需了解详情,请参阅 Gradle 自动下载缺失软件包部分。
- 新的实验性缓存功能可让 Gradle 通过 dex 预处理、存储和重复使用库的 dex 预处理版本来缩短构建时间。如需详细了解如何使用此实验性功能,请参阅构建缓存指南。
- 通过采用在一个任务中处理压缩、签名和压缩对齐操作的新默认打包管道,提升了构建性能。您可以通过在
gradle.properties
文件中添加 android.useOldPackaging=true
来恢复使用旧的打包工具。在使用新的打包工具时,zipalignDebug
任务将不可用。不过,您可以调用 createZipAlignTask(String taskName, File inputFile, File
outputFile)
方法来自己创建一个。
- 除了传统的 JAR 签名,APK 签名现在还会使用 APK 签名方案 v2。所有 Android 平台都接受生成的 APK。如果在签名后修改这些 APK,那么它们的 v2 签名便会失效,并会阻止在设备上安装。如需停用此功能,请将以下代码添加到模块级
build.gradle
文件中:
Groovy
android {
...
signingConfigs {
config {
...
v2SigningEnabled false
}
}
}
Kotlin
android {
...
signingConfigs {
create("config") {
...
v2SigningEnabled = false
}
}
}
- 对于 MultiDex build,您现在可以使用 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
现在会返回 Android Plugin for Gradle 提供的默认 ProGuard 文件,并且不再使用 Android SDK 中的那些文件。
- 改进了 Jack 编译器的性能和功能:
- 在将
testCoverageEnabled
设置为 true
后,Jack 现在支持 Jacoco 测试覆盖率。
- 改进了对注解处理器的支持。类路径上的注解处理器(例如任意
compile
依赖项)会自动应用于 build。您还可以在 build 中指定注解处理器,并通过在模块级 build.gradle
文件中使用 javaCompileOptions.annotationProcessorOptions {}
DSL 来传递参数:
Groovy
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
className 'com.example.MyProcessor'
// Arguments are optional.
arguments = [ foo : 'bar' ]
}
}
}
}
Kotlin
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
className = "com.example.MyProcessor"
// Arguments are optional.
arguments(mapOf(foo to "bar"))
}
}
}
}
如果要在编译时应用注解处理器,但不将其包含在 APK 中,请使用 annotationProcessor
依赖项范围:
Groovy
dependencies {
compile 'com.google.dagger:dagger:2.0'
annotationProcessor 'com.google.dagger:dagger-compiler:2.0'
// or use buildVariantAnnotationProcessor to target a specific build variant
}
Kotlin
dependencies {
implementation("com.google.dagger:dagger:2.0")
annotationProcessor("com.google.dagger:dagger-compiler:2.0")
// or use buildVariantAnnotationProcessor to target a specific build variant
}
如需查看您可以设置的参数的列表,请从命令行中运行以下命令:
java -jar /build-tools/jack.jar --help-properties
- 默认情况下,如果 Gradle 守护程序的堆大小至少为 1.5 GB,那么 Jack 现在会与 Gradle 在同一进程中运行。如需调整守护程序的堆大小,请将以下代码添加到
gradle.properties
文件中:
# This sets the daemon heap size to 1.5GB.
org.gradle.jvmargs=-Xmx1536M
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[],[],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"]]