使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android Gradle 插件 3.6.0(2020 年 2 月)
此版本的 Android 插件需要使用以下工具:
|
最低版本 |
默认版本 |
备注 |
Gradle |
5.6.4 |
5.6.4 |
如需了解详情,请参阅更新 Gradle。 |
SDK Build Tools |
28.0.3 |
28.0.3 |
安装或配置 SDK Build Tools。 |
新功能
此版本的 Android Gradle 插件包含以下新功能。
视图绑定
在代码中引用视图时,视图绑定可确保编译时安全性。您现在可以将 findViewById()
替换为自动生成的绑定类引用。如需开始使用视图绑定,请在每个模块的 build.gradle
文件中添加以下代码:
android {
viewBinding.enabled = true
}
android {
viewBinding.enabled = true
}
如需了解详情,请参阅视图绑定文档。
支持 Maven Publish 插件
Android Gradle 插件支持 Maven Publish Gradle 插件,可让您将 build 工件发布到 Apache Maven 制品库。Android Gradle 插件会为应用或库模块中的每个 build 变体工件创建一个组件,您可以使用它来自定义要发布到 Maven 制品库的发布内容。
如需了解详情,请访问介绍如何使用 Maven Publish 插件的页面。
新的默认打包工具
在构建应用的调试版本时,该插件会使用一个新的打包工具 zipflinger 来构建 APK。这一新工具应该能够提高构建速度。如果新的打包工具无法正常运行,请报告 bug。您可以通过在 gradle.properties
文件中添加以下代码来恢复使用旧的打包工具:
android.useNewApkCreator=false
原生构建属性
您现在可以确定 Clang 构建和链接项目中的每个 C/C++ 文件所需的时间。Gradle 可以生成包含这些编译器事件时间戳的 Chrome 跟踪记录,以便您更清楚地了解构建项目所需的时间。如需生成此构建属性文件,请执行以下操作:
-
在运行 Gradle build 时添加标记 -Pandroid.enableProfileJson=true
。例如:
gradlew assembleDebug -Pandroid.enableProfileJson=true
-
打开 Chrome 浏览器,然后在搜索栏中输入 chrome://tracing
。
-
点击 Load 按钮并导航到 <var>project-root</var>/build/android-profile
以查找文件。该文件的名称为 profile-<var>timestamp</var>.json.gz
。
您可以在查看器顶部附近查看原生构建属性数据:

行为变更
使用此版本的插件时,您可能会遇到以下行为方面的变更。
在构建应用时,插件现在默认会将 extractNativeLibs
设置为 "false"
。也就是说,您的原生库将保持页面对齐状态并以未压缩的形式打包。虽然这样会增加上传大小,但您的用户会从以下优势中受益:
- 减小应用安装大小,因为平台可以直接从已安装的 APK 访问原生库,而无需创建库的副本。
- 减小下载文件大小,因为在 APK 或 Android App Bundle 中添加未压缩的原生库通常可提高 Play 商店的压缩率。
如果您希望 Android Gradle 插件改为打包压缩后的原生库,请在应用的清单中添加以下代码:
<application
android:extractNativeLibs="true"
... >
</application>
注意:extractNativeLibs
清单属性已被 useLegacyPackaging
DSL 选项取代。如需了解详情,请参阅版本说明使用 DSL 打包压缩的原生库。
默认 NDK 版本
现在,如果您下载了多个版本的 NDK,Android Gradle 插件会选择一个默认版本来编译源代码文件。以前,该插件选择的是最新下载的 NDK 版本。使用模块的 build.gradle
文件中的 android.ndkVersion
属性可替换插件选择的默认版本。
简化了 R 类的生成过程
Android Gradle 插件通过仅为项目中的每个库模块生成一个 R 类并与其他模块依赖项共享这些 R 类,简化了编译类路径。这项优化应该会加快构建速度,但您需要注意以下事项:
- 由于编译器与上游模块依赖项共享 R 类,因此项目中的每个模块都必须使用独一无二的软件包名称。
- 库的 R 类对其他项目依赖项的可见性取决于用于将库添加为依赖项的配置。例如,如果库 A 将库 B 添加为“api”依赖项,则库 A 和其他依赖于库 A 的库都可以访问库 B 的 R 类。不过,如果库 A 使用
implementation
依赖项配置,其他库可能就无权访问库 B 的 R 类。如需了解详情,请参阅依赖项配置。
移除默认配置中缺少的资源
对于库模块,如果您添加了未包含在默认资源集内的语言资源(例如,将 hello_world
作为字符串资源添加到 /values-es/strings.xml
中,但未在 /values/strings.xml
中定义该资源),Android Gradle 插件在编译项目时将不会再添加该资源。这种行为变更应该会减少 Resource Not Found
运行时异常,并提高构建速度。
D8 现在遵循注解的类保留政策
现在,在编译应用时,D8 会遵循注解应用的类保留政策,并且这些注解在运行时不再可用。将应用的目标 SDK 设置为 API 级别 23 时,也存在此行为;此前,在使用旧版本的 Android Gradle 插件和 D8 编译应用时,会允许在运行时访问这些注解。
其他行为变更
bug 修复
此版本的 Android Gradle 插件包含以下 bug 修复:
- 现在,使用数据绑定的库模块支持 Robolectric 单元测试。如需了解详情,请参阅问题 #126775542。
- 现在,您可在启用 Gradle 的并行执行模式的情况下跨多个模块运行
connectedAndroidTest
任务。
已知问题
本部分介绍 Android Gradle 插件 3.6.0 中存在的已知问题。
Android lint 由于解析基础架构性能降低,导致在某些代码构造中对 lambda 的推断类型的计算速度变慢,因此可能需要更长时间才能完成某些项目。
此问题会报告为 IDEA bug,并将在 Android Gradle 插件 4.0 中修复。
缺少清单类 {:#agp-missing-manifest}
如果您的应用在其清单中定义自定义权限,Android Gradle 插件通常会生成 Manifest.java
类,用于以字符串常量的形式添加您的自定义权限。该插件会将此类与您的应用打包在一起,以便于您在运行时更轻松地引用这些权限。
在 Android Gradle 插件 3.6.0 中无法生成清单类。如果您使用此版本的插件构建应用,并且该应用引用了清单类,您可能会看到 ClassNotFoundException
异常。如需解决此问题,请执行以下某项操作:
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):2025-08-30。"],[],[],null,["\u003cbr /\u003e\n\nAndroid Gradle Plugin 3.6.0 (February 2020)\n\nThis version of the Android plugin requires the following:\n\n| | Minimum version | Default version | Notes |\n|----------------:|:---------------:|:---------------:|:----------------------------------------------------------------------------------------------------------:|\n| Gradle | 5.6.4 | 5.6.4 | To learn more, see [updating Gradle](/build/releases/gradle-plugin?buildsystem=ndk-build#updating-gradle). |\n| SDK Build Tools | 28.0.3 | 28.0.3 | [Install](/studio/intro/update#sdk-manager) or [configure](/tools/releases/build-tools) SDK Build Tools. |\n\n**3.6.4 (July 2020)**\n\n\nThis minor update supports compatibility with new default settings and\nfeatures for\n[package visibility\nin Android 11](/about/versions/11/privacy/package-visibility).\n\n\nSee the [4.0.1 release notes](#4.0.1) for details.\n\nNew features\n\nThis version of the Android Gradle plugin includes the following new\nfeatures.\n\nView Binding\n\nView binding provides compile-time safety when referencing views in\nyour code. You can now replace `findViewById()` with the\nauto-generated binding class reference. To start using View binding,\ninclude the following in each module's `build.gradle` file: \n\n```groovy\n android {\n viewBinding.enabled = true\n }\n \n``` \n\n```kotlin\n android {\n viewBinding.enabled = true\n }\n \n```\n\nTo learn more, read the [View\nBinding documentation](/topic/libraries/view-binding).\n\nSupport for the Maven Publish plugin\n\nThe Android Gradle plugin includes support for the\n[Maven\nPublish Gradle plugin](https://docs.gradle.org/current/userguide/publishing_maven.html), which allows you to publish build artifacts to\nan Apache Maven repository. The Android Gradle plugin creates a\n[*component*](https://docs.gradle.org/current/userguide/dependency_management_terminology.html#sub:terminology_component)\nfor each build variant artifact in your app or library module that you can\nuse to customize a\n[*publication*](https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:publications)\nto a Maven repository.\n\nTo learn more, go to the page about how to\n[use the Maven Publish\nplugin](/studio/build/maven-publish-plugin).\n\nNew default packaging tool\n\nWhen building the debug version of your app, the plugin uses a new\npackaging tool, called *zipflinger* , to build your APK. This new\ntool should provide build speed improvements. If the new packaging tool\ndoesn't work as you expect,\nplease [report a bug](/studio/report-bugs). You can revert to\nusing the old packaging tool by including the following in your\n`gradle.properties` file: \n\n android.useNewApkCreator=false\n \nNative build attribution\n\nYou can now determine the length of time it takes Clang to build and\nlink each C/C++ file in your project. Gradle can output a Chrome trace\nthat contains timestamps for these compiler events so you can better\nunderstand the time required to build your project. To output this build\nattribution file, do the following:\n\n1. Add the flag `-Pandroid.enableProfileJson=true` when\n running a Gradle build. For example:\n\n `gradlew assembleDebug -Pandroid.enableProfileJson=true`\n2. Open the Chrome browser and type `chrome://tracing` in\n the search bar.\n\n3. Click the **Load** button and navigate to\n `\u003cvar\u003eproject-root\u003c/var\u003e/build/android-profile`\n to find the file. The file is named\n `profile-\u003cvar\u003etimestamp\u003c/var\u003e.json.gz`.\n\nYou can see the native build attribution data near the top of the\nviewer:\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nBehavior changes\n\nWhen using this version of the plugin, you might encounter the following\nchanges in behavior.\n\nNative libraries packaged uncompressed by\ndefault\n\nWhen you build your app, the plugin now sets\n`extractNativeLibs` to `\"false\"` by\ndefault. That is, your native libraries are page aligned and packaged\nuncompressed. While this results in a larger upload size, your users\nbenefit from the following:\n\n- Smaller app install size because the platform can access the native libraries directly from the installed APK, without creating a copy of the libraries.\n- Smaller download size because Play Store compression is typically better when you include uncompressed native libraries in your APK or Android App Bundle.\n\nIf you want the Android Gradle plugin to instead package compressed\nnative libraries, include the following in your app's manifest: \n\n \u003capplication\n android:extractNativeLibs=\"true\"\n ... \u003e\n \u003c/application\u003e\n \n \n**Note:** The `extractNativeLibs` manifest\nattribute has been replaced by the `useLegacyPackaging` DSL\noption. For more information, see the release note\n[Use the DSL to package compressed\nnative libraries](#compress-native-libs-dsl).\n\nDefault NDK version\n\nIf you download multiple versions of the NDK, the Android Gradle plugin\nnow selects a default version to use in compiling your source code files.\nPreviously, the plugin selected the latest downloaded version of the NDK.\nUse the `android.ndkVersion` property in the module's\n`build.gradle` file to override the plugin-selected default.\n\nSimplified R class generation\n\nThe Android Gradle plugin simplifies the compile classpath by\ngenerating only one R class for each library module in your project and\nsharing those R classes with other module dependencies. This optimization\nshould result in faster builds, but it requires that you keep the\nfollowing in mind:\n\n- Because the compiler shares R classes with upstream module dependencies, it's important that each module in your project uses a unique package name.\n- The visibility of a library's R class to other project dependencies is determined by the configuration used to include the library as a dependency. For example, if Library A includes Library B as an 'api' dependency, Library A and other libraries that depend on Library A have access to Library B's R class. However, other libraries might not have access to Library B's R class. If Library A uses the `implementation` dependency configuration. To learn more, read about [dependency\n configurations](/studio/build/dependencies#dependency_configurations).\n\nRemove resources missing from default\nconfiguration\n\nFor Library modules, if you include a resource for a language that you\ndo not include in the default set of resources---for example, if you include\n`hello_world` as a string resource in\n`/values-es/strings.xml` but you don't define that resource in\n`/values/strings.xml`---the Android Gradle plugin no longer\nincludes that resource when compiling your project. This behavior change\nshould result in fewer `Resource Not Found` runtime exceptions\nand improved build speed.\n\nD8 now respects CLASS retention policy\nfor annotations\n\nWhen compiling your app, D8 now respects when annotations apply a CLASS\nretention policy, and those annotations are no longer available at\nruntime. This behavior also exists when setting the app's target SDK to\nAPI level 23, which previously allowed access to these annotations during\nruntime when compiling your app using older versions of the Android Gradle\nplugin and D8.\n\nOther behavior changes\n\n- `aaptOptions.noCompress` is no longer case sensitive on all platforms (for both APK and bundles) and respects paths that use uppercase characters.\n- Data binding is now incremental by default. To learn more, see\n [issue #110061530](https://issuetracker.google.com/110061530).\n\n- All unit tests, including Roboelectric unit tests, are now fully\n cacheable. To learn more, see\n [issue #115873047](https://issuetracker.google.com/115873047).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nBug fixes\n\nThis version of the Android Gradle plugin includes the following bug\nfixes:\n\n- Robolectric unit tests are now supported in library modules that use data binding. To learn more, see [issue #126775542](https://issuetracker.google.com/126775542).\n- You can now run `connectedAndroidTest` tasks across multiple modules while Gradle's [parallel\n execution mode](https://guides.gradle.org/performance/#parallel_execution) is enabled.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nKnown issues\n\nThis section describes known issues that exist in Android Gradle plugin\n3.6.0.\n\nSlow performance of Android Lint task\n\nAndroid Lint can take much longer to complete on some projects due to a\nregression in its parsing infrastructure, resulting in slower computation\nof inferred types for lambdas in certain code constructs.\n\nThe issue is reported as\n[a bug in IDEA](https://youtrack.jetbrains.com/issue/IDEA-229655)\nand will be fixed in Android Gradle Plugin 4.0.\n\nMissing Manifest class {:#agp-missing-manifest}\n\nIf your app defines custom permissions in its manifest, the Android\nGradle plugin typically generates a `Manifest.java` class that\nincludes your custom permissions as string constants. The plugin packages\nthis class with your app, so you can more easily reference those\npermissions at runtime.\n\nGenerating the manifest class is broken in Android Gradle plugin 3.6.0.\nIf you build your app with this version of the plugin, and it references\nthe manifest class, you might see a `ClassNotFoundException`\nexception. To resolve this issue, do one of the following:\n\n- Reference your custom permissions by their fully-qualified name.\n For example,\n `\"com.example.myapp.permission.DEADLY_ACTIVITY\"`.\n\n- Define your own constants, as shown below:\n\n public final class CustomPermissions {\n public static final class permission {\n public static final String DEADLY_ACTIVITY=\"com.example.myapp.permission.DEADLY_ACTIVITY\";\n }\n }\n \n \n\u003cbr /\u003e"]]