将应用迁移到 Android 11
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
每次发布新的 Android 版本时,我们都会推出一些全新的功能并引入一些行为变更,目的就在于提高 Android 的实用性、安全性和性能。在许多情况下,您的应用都可以直接使用并完全按预期运行;而在其他的一些情况下,您可能需要对应用进行更改以适应这些平台变化。
源代码发布到 AOSP(Android 开源项目)后,用户随之就可能开始使用新平台。因此,应用必须做好准备,让用户能够正常使用,最好还能利用新的功能和 API 发挥新平台的最大优势。
本文档对典型的开发和测试阶段进行了简要介绍,以帮助您制定与平台发布时间表保持一致的准备计划,并确保您的用户在 Android 11 上获得良好的体验。
典型的迁移包含两个阶段,这两个阶段可以同时进行:
- 确保应用兼容性(在 Android 11 最终发布前)
- 针对新平台的功能和 API 调整应用(最终发布后尽快进行)
本页概述了其中的每一个阶段的一般步骤。当您准备好开始后,请阅读获取 Android 11。
确保与 Android 11 兼容
您必须测试现有应用在 Android 11 上的运行情况,以确保更新到最新版 Android 的用户获得良好的体验。有些平台变更可能会影响应用的行为方式,因此,必须尽早进行全面测试并对应用进行任何必要的调整。
通常,您可以调整应用并发布更新,而无需更改应用的 targetSdkVersion
。同样,您应该也不需要使用新的 API 或更改应用的 compileSdkVersion
,但是这一点可能要取决于应用的构建方式及其所使用的平台功能。以下各部分概述了相关步骤。
即使您无需更改应用的 targetSdkVersion
,在开始之前也务必要熟悉可能影响应用的行为变更。

执行兼容性测试
在大多数情况下,测试应用与 Android 11 的兼容性与您在准备发布应用时所执行的测试类似。这时有必要回顾一下核心应用质量指南和测试最佳做法。
只需在搭载 Android 11 的设备上安装您当前发布的应用,并完成所有流程和功能即可查找问题。为帮助您确定测试重点,请查看 Android 11 中引入的行为变更,这些变更会影响应用的功能或导致应用崩溃。尤其务必查看重要的隐私权变更,并测试为适应这些变更而实施的修复。
此外,请务必查看并测试受限非 SDK 接口的使用,并改为使用公共 SDK 或 NDK 等效项。留意突出显示这些访问权限的 logcat 警告,并使用 StrictMode 方法 detectNonSdkApiUsage()
以编程方式捕获它们。
最后,请务必完整测试应用中的库和 SDK,确保它们在 Android 11 上按预期运行,并遵循隐私权、性能、用户体验、数据处理和权限方面的最佳做法。如果您遇到问题,请尝试更新到最新版本的 SDK,或联系 SDK 开发者寻求帮助。
当您完成测试并进行更新后,我们建议您立即发布兼容的应用。这样可以尽早让您的用户测试应用,并帮助用户顺利过渡到 Android 11。
更新应用的目标平台并使用新 API 进行构建
按照上述说明发布应用的兼容版本后,下一步是通过更新 targetSdkVersion
并利用 Android 11 的新 API 和功能来添加对 Android 11 的全面支持。在准备就绪后,您就可以立即执行这些操作,并牢记针对新平台的 Google Play 要求。
当您计划全面支持 Android 11 时,请先查看适用于以 Android 11 为目标平台的应用的行为变更。这些针对性的行为变更可能会导致需要解决的功能问题。在某些情况下,可能需要进行大量开发工作,因此最好尽早了解。为帮助您评估影响,您还可以在启用所选变更的情况下,使用兼容性切换开关来测试当前应用。
以下步骤介绍了如何全面支持 Android 11。

获取 SDK,更改目标平台,使用新 API 进行构建
如需开始全面支持 Android 11,请先将 Android 11 SDK(以及所需的任何其他工具)下载到 Android Studio 中。接下来,将应用的 targetSdkVersion
和 compileSdkVersion
更改为 "30"
,然后重新编译应用。有关详情,请参阅设置指南。
测试 Android 11 应用
编译应用并将其安装到搭载 Android 11 的设备上后,请开始测试,以确保应用能够在 Android 11 上正常运行。某些行为变更仅在应用以新平台为目标平台时才适用,因此您需要在开始之前查看这些变更。
与基本兼容性测试一样,完成所有流程和功能以查找问题。将测试重点放在以 Android 11 为目标平台的应用的行为变更上。尤其务必查看隐私权变更,并测试为适应这些变更而实施的修复。您还可以根据核心应用质量指南和测试最佳实践检查您的应用。
请务必查看并测试可能适用的受限非 SDK 接口的使用。留意突出显示这些访问权限的 logcat 警告,并使用 StrictMode 方法 detectNonSdkApiUsage()
以编程方式捕获它们。
最后,请务必完整测试应用中的库和 SDK,确保它们在 Android 11 上按预期运行,并遵循隐私权、性能、用户体验、数据处理和权限方面的最佳做法。如果您遇到问题,请尝试更新到最新版本的 SDK,或联系 SDK 开发者寻求帮助。
使用应用兼容性切换开关进行测试
Android 11 为开发者引入了一项新功能,可让您更轻松地测试应用的针对性行为变更。对于可调试的应用,切换开关可让您:
- 在不实际更改应用的 targetSdkVersion 的情况下测试有针对性的更改。您可以使用切换开关强制启用特定的针对性行为变更,以评估对现有应用的影响。
- 仅针对特定变更进行测试。您可以使用切换开关停用除要测试的变更之外的所有针对性变更,而不必一次处理所有针对性变更。
- 通过 adb 管理切换开关。您可以使用 adb 命令在自动测试环境中启用和停用可切换的变更。
- 使用标准变更 ID 更快地进行调试。每个可切换的变更都具有唯一 ID 和名称,可用于在日志输出中快速调试根本原因。
在您准备更改应用的目标平台时,或者在您积极开发以便支持 Android 11 时,切换开关将十分有用。如需了解详情,请参阅相应文档。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[[["易于理解","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-27。"],[],[],null,["With each release of Android, we introduce new features as well as behavior\nchanges aimed at making the Android more helpful, more secure, and better\nperforming. In many cases your app will work exactly as expected out of the box,\nwhile in other cases you might need to make changes to your app to adapt to the\nplatform changes.\n\nSince users can start receiving the new platform as soon as the source code is\nreleased to AOSP (Android Open Source Project), it's important for apps to be\nready, performing as expected for users and ideally taking advantage of new\nfeatures and APIs to get the most out of the new platform.\n\nThis document offers a high-level view of typical development and testing phases\nthat can help you make a plan for readiness that's well-aligned with the\nplatform release timeline and ensures a great experience for your users on\nAndroid 11.\n\nA typical migration has two phases, which can be concurrent:\n\n- Ensuring app compatibility (by Android 11 final release)\n- Targeting the new platform features and APIs (as soon as possible after final release)\n\nThis page outlines the general steps for each of those phases. When you're ready\nto get started, read [Get Android 11](/about/versions/11/get).\n\nEnsure compatibility with Android 11\n\nIt's important to test the functionality of your existing app against\nAndroid 11 to ensure a great experience for users updating to the\nlatest version of Android. Some platform changes can affect the way your app\nbehaves, so it's important to test early and thoroughly, then make any needed\nadjustments to your app.\n\nTypically you can adjust your app and publish an update without needing to\nchange the app's `targetSdkVersion`. Similarly, you shouldn't need to use new\nAPIs or change the app's `compileSdkVersion`, although this can depend on the\nway your app is built and the platform functionality it's using. The following\nsections outline the steps.\n\nBefore you get started, make sure to familiarize yourself with the [behavior\nchanges](/about/versions/11/behavior-changes-all) that might affect your app, even if you don't change its\n`targetSdkVersion`.\n\nPerform compatibility testing\n\nFor the most part, testing compatibility with Android 11 is\nsimilar to the type of testing you otherwise perform when preparing to release\nyour app. This is a good time to review the [core app quality guidelines](/develop/quality-guidelines/core-app-quality) and\n[best practices for testing](/training/testing).\n\nJust install your current published app on a device running\nAndroid 11 and work through all the flows and functionality\nlooking for issues. To help you focus your testing, **review the [behavior\nchanges](/about/versions/11/behavior-changes-all)** introduced in Android 11 that can affect your app's\nfunction or cause the app to crash. In particular, make sure to review the [key\nprivacy changes](/about/versions/11/privacy) and test any fixes that you implement to accommodate the\nchanges.\n\nAlso make sure to review and **test for uses of restricted non-SDK interfaces**\nand move to public SDK or NDK equivalents instead. Watch for logcat warnings\nthat highlight these accesses and use the StrictMode method\n[`detectNonSdkApiUsage()`](/reference/android/os/StrictMode.VmPolicy.Builder#detectNonSdkApiUsage()) to catch them programmatically.\n\nLast, make sure to fully **test the libraries and SDKs in your app** to make\nsure they work as expected on Android 11 and follow best\npractices for privacy, performance, UX, data handling, and permissions. If you\nfind an issue, try updating to the latest version of the SDK, or reach out to\nthe SDK developer for help.\n\nWhen you've finished your testing and made any updates, we recommend publishing\nyour compatible app right away. This lets your users test the app early, and\nhelps you to deliver a smooth transition to users as they update to\nAndroid 11.\n\nUpdate the app's targeting and build with new APIs\n\nOnce you've published the compatible version of your app as described\npreviously, the next step is to add full support for Android 11\nby updating its `targetSdkVersion` and taking advantage of new aPIs and\ncapabilities Android 11. You can do these as soon as you are\nready, keeping in mind the [Google Play requirement](https://support.google.com/googleplay/android-developer/answer/113469#targetsdk) for targeting the new\nplatform.\n\nAs you plan your work to fully support Android 11, a good place\nto start is reviewing the [behavior changes that apply to apps targeting\nAndroid 11](/about/versions/11/behavior-changes-11). These *targeted behavior changes* may cause\nfunctional issues that you may need to address. In some cases, they can require\nsignificant development, so it's best to learn about them early. To help you\nassess the impacts, you can also use the [compatibility toggles](/about/versions/11/migration#using_app_compatibility_toggles) to test\nyour current app with selected changes enabled.\n\nThe following steps describe how to fully support Android 11.\n\nGet the SDK, change targeting, build with new APIs\n\nTo get started with full Android 11 support, first download the\nAndroid 11 SDK (and any other tools needed) into Android Studio.\nNext change the app's `targetSdkVersion` and `compileSdkVersion` to `\"30\"`and\nre-compile the app. See the [setup guide](/about/versions/11/setup-sdk) for details.\n\nTest your Android 11 app\n\nOnce you've compiled the app and installed it on a device running\nAndroid 11, begin testing to ensure that the app works properly\nwhen targeting Android 11. Some behavior changes apply only when\nyour app is targeting the new platform, so you'll want to [review those\nchanges](/about/versions/11/behavior-changes-11) before getting started.\n\nAs with basic compatibility testing, work through all the flows and\nfunctionality looking for issues. Focus your testing on the\n**[behavior changes for apps targeting Android 11](/about/versions/11/behavior-changes-11)** . In\nparticular, make sure to review the [privacy changes](/about/versions/11/privacy) and test any fixes that\nyou implement to accommodate the changes. It's also a good time to check your\napp against the [core app quality guidelines](/develop/quality-guidelines/core-app-quality) and [best practices for\ntesting](/training/testing).\n\nMake sure to review and **test for uses of [restricted non-SDK interfaces](/about/versions/11/non-sdk-11)**\nthat may apply. Watch for logcat warnings that highlight these accesses and use\nthe StrictMode method [`detectNonSdkApiUsage()`](/reference/android/os/StrictMode.VmPolicy.Builder#detectNonSdkApiUsage()) to catch them\nprogrammatically.\n\nLast, make sure to fully **test the libraries and SDKs in your app** to make\nsure they work as expected on Android 11 and follow best\npractices for privacy, performance, UX, data handling, and permissions. If you\nfind an issue, try updating to the latest version of the SDK, or reach out to\nthe SDK developer for help.\n\nTest using app compatibility toggles\n\nAndroid 11 introduces a new feature for developers that makes it easier to test\nyour app with targeted behavior changes. For a debuggable app, the toggles let\nyou:\n\n- **Test targeted changes without actually changing the app's\n targetSdkVersion**. You can use the toggles to force-enable specific targeted behavior changes to evaluate the impact on your existing app.\n- **Focus your testing on specific changes only**. Rather than having to address all targeted changes at once, the toggles let you disable all targeted changes except the ones you want to test against.\n- **Manage toggles through adb**. You can use adb commands to enable and disable the toggleable changes in your automated test environment.\n- **Debug faster using standard change IDs**. Toggleable changes each have a unique ID and name that you can use to quickly debug root cause in log output.\n\nAs you prepare for changing your app's targeting, or while you in active\ndevelopment for Android 11 support, the toggles can help. See the [corresponding\ndocumentation for more details](/guide/app-compatibility/test-debug)."]]