行为变更:所有应用

Android 17 平台包含一些行为变更,这些变更可能会影响您的应用。 以下行为变更将影响在 Android 17 上运行的 所有应用, 无论其 targetSdkVersion 如何都不例外。您应该测试您的应用,然后根据需要酌情修改,以便支持这些变更。

此外,请务必查看仅影响以 Android 17 为目标平台的应用的行为变更列表 。

核心功能

Android 17(API 级别 37)包含以下变更,这些变更会修改或扩展 Android 系统的各种核心功能。

应用内存限制

Android 17 引入了基于设备总 RAM 的应用内存限制,以便为应用和 Android 用户打造更稳定、更确定的环境。在 Android 17 中,限制设置得较为保守,旨在建立系统基准,在极端内存泄漏和其他异常情况导致系统范围内的不稳定(导致界面卡顿、耗电过快和应用被终止)之前,就将其检测出来。虽然我们预计此变化对绝大多数应用会话的影响微乎其微,但我们建议您遵循以下内存最佳实践,包括建立内存基准。

您可以通过在 ApplicationExitInfo 中调用 getDescription 来确定应用会话是否受到影响;如果应用受到影响,退出原因将为 REASON_OTHER,说明将包含字符串 "MemoryLimiter:AnonSwap" 以及其他信息。您还可以将 TRIGGER_TYPE_ANOMALY基于触发器的分析搭配使用,以获取在达到内存限制时收集的堆转储。

Android Studio 性能分析器中的 LeakCanary 任务。

为了帮助您查找内存泄漏,Android Studio Panda 直接在 Android Studio 分析器中添加了 LeakCanary 集成,作为 IDE 中上下文相关的专用任务,并与您的源代码完全集成。

安全

Android 17 包含设备和应用安全方面的以下改进。

usesClearTraffic 弃用计划

我们计划在未来的版本中弃用 usesCleartextTraffic 元素。需要建立未加密 (HTTP) 连接的应用应迁移为使用网络安全配置文件,该文件可让您指定应用需要与哪些网域建立明文连接。

请注意,网络安全配置文件仅在 API 级别 24 及更高版本上受支持。如果应用的最低 API 级别低于 24,您应执行以下两项操作:

  • usesCleartextTraffic 属性设置为 true
  • 使用网络配置文件

如果应用的最低 API 级别为 24 或更高,您可以使用网络配置文件,而无需设置 usesCleartextTraffic

限制隐式 URI 授予

Currently, if an app launches an intent with a URI that has the action Send, SendMultiple, or ImageCapture, the system automatically grants the read and write URI permissions to the target app. We plan to change this behavior in Android 18. For this reason, we recommend that apps explicitly grant the relevant URI permissions instead of relying on the system to grant them.

每个应用的密钥库限制

应用应避免在 Android Keystore 中创建过多的密钥,因为它是设备上所有应用的共享资源。从 Android 17 开始,系统会对应用可拥有的密钥数量强制执行限制。对于以 Android 17(API 级别 37)或更高版本为目标平台的非系统应用,密钥数量上限为 50,000 个,对于所有其他应用,密钥数量上限为 200,000 个。 无论系统应用以哪个 API 级别为目标平台,密钥数量上限均为 200,000 个。

如果应用尝试创建超出上限的密钥,则创建会失败并显示 KeyStoreException。该异常的消息字符串包含有关密钥上限的信息。如果应用对该异常调用 getNumericErrorCode(),则返回值取决于应用以哪个 API 级别为目标平台:

  • 以 Android 17(API 级别 37)或更高版本为目标平台的应用:getNumericErrorCode() 会返回新的 ERROR_TOO_MANY_KEYS 值。
  • 所有其他应用:getNumericErrorCode() 会返回 ERROR_INCORRECT_USAGE

用户体验和系统界面

Android 17 包含以下变更,旨在打造更加一致、直观的用户体验。

旋转后恢复默认 IME 可见性

Beginning with Android 17, when the device's configuration changes (for example, through rotation), and this is not handled by the app itself, the previous IME visibility is not restored.

If your app undergoes a configuration change that it does not handle, and the app needs the keyboard to be visible after the change, you must explicitly request this. You can make this request in one of the following ways:

  • Set the android:windowSoftInputMode attribute to stateAlwaysVisible.
  • Programmatically request the soft keyboard in your activity's onCreate() method, or add the onConfigurationChanged() method.

人工干预

Android 17 包含以下变更,这些变更会影响应用与键盘和触控板等人工输入设备的互动方式。

在指针捕获期间,触控板默认提供相对事件

Beginning with Android 17, if an app requests pointer capture using View.requestPointerCapture() and the user uses a touchpad, the system recognizes pointer movement and scrolling gestures from the user's touches and reports them to the app in the same way as pointer and scroll wheel movements from a captured mouse. In most cases, this removes the need for apps that support captured mice to add special handling logic for touchpads. For more details, see the documentation for View.POINTER_CAPTURE_MODE_RELATIVE.

Previously, the system did not attempt to recognize gestures from the touchpad, and instead delivered the raw, absolute finger locations to the app in a similar format to touchscreen touches. If an app still requires this absolute data, it should call the new View.requestPointerCapture(int) method with View.POINTER_CAPTURE_MODE_ABSOLUTE instead.

媒体

Android 17 包含媒体行为方面的以下变更。

后台音频安全加固

从 Android 17 开始,音频框架会对后台音频互动(包括音频播放、音频焦点请求和音量更改 API)强制执行限制,以确保这些更改是由用户有意发起的。

如果应用尝试在应用未处于有效生命周期时调用音频 API,则音频播放和音量更改 API 会以静默方式失败,而不会抛出异常或提供失败消息。音频焦点 API 会失败,并返回结果代码 AUDIOFOCUS_REQUEST_FAILED

如需了解详情(包括缓解措施),请参阅后台音频安全加固

连接

Android 17 包含以下变更,旨在增强设备连接。

在蓝牙绑定丢失时自动重新配对

Android 17 引入了自主重新配对功能,这是一项系统级增强功能,旨在自动解决蓝牙配对信息丢失问题。

以前,如果配对信息丢失,用户必须手动前往“设置”取消配对,然后重新配对外围设备。此功能以 Android 16 的安全改进为基础,允许系统在后台重新建立配对信息,而无需用户手动前往“设置”取消配对并重新配对外围设备。

虽然大多数应用不需要更改代码,但开发者应注意蓝牙堆栈中的以下行为变更:

  • 新的配对上下文ACTION_PAIRING_REQUEST 现在包含 EXTRA_PAIRING_CONTEXT extra,允许应用区分 标准配对请求和自主系统发起的重新配对尝试。
  • 有条件的密钥更新:只有在重新配对成功且新连接达到或超过之前配对信息的安全级别时,才会替换现有安全密钥。
  • 修改后的 intent 时间:现在,只有在自主重新配对尝试失败时,才会广播 ACTION_KEY_MISSING intent。如果系统在后台成功恢复配对信息,则可以减少应用中不必要的错误处理。
  • 用户通知:系统通过新的界面通知和对话框管理重新配对。系统会提示用户确认重新配对尝试,以确保用户了解重新连接。

外围设备制造商和配套应用开发者应验证硬件和应用是否能妥善处理配对信息转换。如需测试此行为,请使用以下任一方法模拟远程配对信息丢失:

  • 从外围设备中手动移除配对信息
  • 在“设置”>“已连接的设备”中手动取消配对设备