行为变更:以 Android 16 或更高版本为目标平台的应用

与早期版本一样,Android 16 包含一些行为变更,这些变更可能会影响您的应用。以下行为变更仅影响以 Android 16 或更高版本为目标平台的应用。如果您的应用以 Android 16 或更高版本为目标平台,您应该修改自己的应用以支持这些行为(如果适用)。

请务必查看对 Android 16 上运行的所有应用都有影响的行为变更列表(无论应用的 targetSdkVersion 如何)。

用户体验和系统界面

Android 16(API 级别 36)进行了以下更改,旨在打造更一致、更直观的用户体验。

无边框设计停用退出选项

Android 15 enforced edge-to-edge for apps targeting Android 15 (API level 35), but your app could opt-out by setting R.attr#windowOptOutEdgeToEdgeEnforcement to true. For apps targeting Android 16 (API level 36), R.attr#windowOptOutEdgeToEdgeEnforcement is deprecated and disabled, and your app can't opt-out of going edge-to-edge.

  • If your app targets Android 16 (API level 36) and is running on an Android 15 device, R.attr#windowOptOutEdgeToEdgeEnforcement continues to work.
  • If your app targets Android 16 (API level 36) and is running on an Android 16 device, R.attr#windowOptOutEdgeToEdgeEnforcement is disabled.

For testing in Android 16 Beta 3, ensure your app supports edge-to-edge and remove any use of R.attr#windowOptOutEdgeToEdgeEnforcement so that your app also supports edge-to-edge on an Android 15 device. To support edge-to-edge, see the Compose and Views guidance.

需要迁移或停用预测性返回

For apps targeting Android 16 (API level 36) or higher and running on an Android 16 or higher device, the predictive back system animations (back-to-home, cross-task, and cross-activity) are enabled by default. Additionally, onBackPressed is not called and KeyEvent.KEYCODE_BACK is not dispatched anymore.

If your app intercepts the back event and you haven't migrated to predictive back yet, update your app to use supported back navigation APIs. or temporarily opt out by setting the android:enableOnBackInvokedCallback attribute to false in the <application> or <activity> tag of your app's AndroidManifest.xml file.

The predictive back-to-home animation.
The predictive cross-activity animation.
The predictive cross-task animation.

优雅字体 API 已废弃并停用

以 Android 15(API 级别 35)为目标平台的应用的 elegantTextHeight TextView 属性默认设置为 true,从而将紧凑字体替换为更易于阅读的字体。您可以通过将 elegantTextHeight 属性设置为 false 来替换此设置。

Android 16 弃用了 elegantTextHeight 属性,并且在您的应用以 Android 16 为目标平台后,系统会忽略该属性。这些 API 控制的“界面字体”即将停用,因此您应调整所有布局,以确保以阿拉伯语、老挝语、缅甸语、泰米尔语、古吉拉特语、卡纳达语、马拉雅拉姆语、奥里亚语、泰卢固语或泰语呈现一致且可持续的文字。

对于以 Android 14(API 级别 34)及更低版本为目标平台的应用,或者对于以 Android 15(API 级别 35)为目标平台且通过将 elegantTextHeight 属性设为 false 替换了默认值的应用,elegantTextHeight 行为。
对于以 Android 16 为目标平台的应用,或者对于未通过将 elegantTextHeight 属性设置为 false 来替换默认值的以 Android 15(API 级别 35)为目标平台的应用,elegantTextHeight 行为。

核心功能

Android 16(API 级别 36)包含以下更改,用于修改或扩展 Android 系统的各种核心功能。

固定费率工作调度优化

Prior to targeting Android 16, when scheduleAtFixedRate missed a task execution due to being outside a valid process lifecycle, all missed executions immediately execute when the app returns to a valid lifecycle.

When targeting Android 16, at most one missed execution of scheduleAtFixedRate is immediately executed when the app returns to a valid lifecycle. This behavior change is expected to improve app performance. Test this behavior in your app to check if your app is impacted. You can also test by using the app compatibility framework and enabling the STPE_SKIP_MULTIPLE_MISSED_PERIODIC_TASKS compat flag.

设备规格

Android 16(API 级别 36)对应用在大屏设备上显示时做出了以下更改。

自适应布局

现在,Android 应用可在各种设备(例如手机、平板电脑、可折叠设备、桌面设备、汽车和电视)上运行,并且在大屏设备上支持多种窗口模式(例如分屏和桌面窗口模式),因此开发者应构建能够适应任何屏幕和窗口大小的 Android 应用,无论设备屏幕方向如何。在当今的多设备时代,限制屏幕方向和尺寸调整等范式过于严格。

忽略屏幕方向、尺寸可调整性和宽高比限制

对于以 Android 16(API 级别 36)为目标平台的应用,Android 16 对系统管理屏幕方向、尺寸调整能力和宽高比限制的方式进行了更改。在最小宽度大于等于 600dp 的显示屏上,这些限制不再适用。无论宽高比或用户的首选屏幕方向如何,应用都会填满整个显示窗口,并且不会使用信箱模式。

此变更引入了新的标准平台行为。Android 正在朝着一种模式发展,即应用应适应各种屏幕方向、显示大小和宽高比。固定屏幕方向或尺寸可调整性受限等限制会妨碍应用自适应,因此我们建议让应用自适应,以提供尽可能出色的用户体验。

您还可以使用应用兼容性框架并启用 UNIVERSAL_RESIZABLE_BY_DEFAULT 兼容性标志来测试此行为。

常见的破坏性更改

忽略屏幕方向、可调整大小和宽高比限制可能会影响应用在某些设备上的界面,尤其是专为锁定在纵向模式的小布局设计的元素:例如,拉伸的布局、屏幕外动画和组件等问题。对宽高比或屏幕方向做出的任何假设都可能会导致应用出现视觉问题。详细了解如何避免这些问题并改进应用的自适应行为。

允许设备旋转会导致重新创建更多 activity,如果未正确保留用户状态,可能会导致丢失用户状态。如需了解如何正确保存界面状态,请参阅保存界面状态

实现细节

在大屏设备上,以下清单属性和运行时 API 会在全屏和多窗口模式下被忽略:

系统会忽略 screenOrientationsetRequestedOrientation()getRequestedOrientation() 的以下值:

  • portrait
  • reversePortrait
  • sensorPortrait
  • userPortrait
  • landscape
  • reverseLandscape
  • sensorLandscape
  • userLandscape

关于显示屏可调整大小,android:resizeableActivity="false"android:minAspectRatioandroid:maxAspectRatio 没有任何影响。

对于以 Android 16(API 级别 36)为目标平台的应用,系统会默认在大屏设备上忽略应用屏幕方向、可调整大小和宽高比限制,但所有尚未完全准备就绪的应用都可以通过选择停用此功能来暂时替换此行为(这会导致之前将应用置于兼容模式的行为)。

异常

在以下情况下,Android 16 的屏幕方向、尺寸调整能力和宽高比限制不适用:

  • 游戏(基于 android:appCategory 标志)
  • 用户在设备的宽高比设置中明确选择启用应用的默认行为
  • 小于 sw600dp 的屏幕

暂时停用

如需停用特定 activity,请声明 PROPERTY_COMPAT_ALLOW_RESTRICTED_RESIZABILITY 清单属性:

<activity ...>
  <property android:name="android.window.PROPERTY_COMPAT_ALLOW_RESTRICTED_RESIZABILITY" android:value="true" />
  ...
</activity>

如果应用的太多部分不支持 Android 16,您可以在应用级别应用相同的属性,以完全停用该功能:

<application ...>
  <property android:name="android.window.PROPERTY_COMPAT_ALLOW_RESTRICTED_RESIZABILITY" android:value="true" />
</application>

健康与健身

Android 16(API 级别 36)包含与健康与健身数据相关的以下变更。

健康与健身权限

对于以 Android 16(API 级别 36)或更高版本为目标平台的应用,BODY_SENSORS 权限会使用 android.permissions.health 下的更精细权限,Health Connect 也会使用这些权限。从 Android 16 开始,凡是以前需要具有 BODY_SENSORSBODY_SENSORS_BACKGROUND 权限的 API,现在都需要获取相应的 android.permissions.health 权限。这会影响以下数据类型、API 和前台服务类型:

如果您的应用使用这些 API,则应请求相应的精细权限:

这些权限与用于保护对 Health Connect(用于存储健康、健身和身心健康数据的 Android 数据存储区)读取数据的权限相同。

移动应用

迁移到使用 READ_HEART_RATE 和其他精细权限的移动应用还必须声明 activity 以显示应用的隐私权政策。这与 Health Connect 的要求相同。

连接

Android 16(API 级别 36)在蓝牙堆栈中进行了以下更改,以改善与外围设备的连接。

用于处理键绑定丢失和加密更改的新 intent

As part of the Improved bond loss handling, Android 16 also introduces 2 new intents to provide apps with greater awareness of bond loss and encryption changes.

Apps targeting Android 16 can now:

  • Receive an ACTION_KEY_MISSING intent when remote bond loss is detected, allowing them to provide more informative user feedback and take appropriate actions.
  • Receive an ACTION_ENCRYPTION_CHANGE intent whenever encryption status of the link changes. This includes encryption status change, encryption algorithm change, and encryption key size change. Apps must consider the bond restored if the link is successfully encrypted upon receiving ACTION_ENCRYPTION_CHANGE intent later.

Adapting to varying OEM implementations

While Android 16 introduces these new intents, their implementation and broadcasting can vary across different device manufacturers (OEMs). To ensure your app provides a consistent and reliable experience across all devices, developers should design their bond loss handling to gracefully adapt to these potential variations.

We recommend the following app behaviors:

  • If the ACTION_KEY_MISSING intent is broadcast:

    The ACL (Asynchronous Connection-Less) link will be disconnected by the system, but the bond information for the device will be retained (as described here).

    Your app should use this intent as the primary signal for bond loss detection and guiding the user to confirm the remote device is in range before initiating device forgetting or re-pairing.

    If a device disconnects after ACTION_KEY_MISSING is received, your app should be cautious about reconnecting, as the device may no longer be bonded with the system.

  • If the ACTION_KEY_MISSING intent is NOT broadcast:

    The ACL link will remain connected, and the bond information for the device will be removed by the system, same to behavior in Android 15.

    In this scenario, your app should continue its existing bond loss handling mechanisms as in previous Android releases, to detect and manage bond loss events.

移除蓝牙配对的新方法

All apps targeting Android 16 are now able to unpair bluetooth devices using a public API in CompanionDeviceManager. If a companion device is being managed as a CDM association, then the app can trigger bluetooth bond removal by using the new removeBond(int) API on the associated device. The app can monitor the bond state changes by listening to the bluetooth device broadcast event ACTION_BOND_STATE_CHANGED.

安全

Android 16(API 级别 36)包含安全方面的以下变更。

MediaStore 版本锁定

For apps targeting Android 16 or higher, MediaStore#getVersion() will now be unique to each app. This eliminates identifying properties from the version string to prevent abuse and usage for fingerprinting techniques. Apps shouldn't make any assumptions around the format of this version. Apps should already handle version changes when using this API and in most cases shouldn't need to change their current behavior, unless the developer has attempted to infer additional information that is beyond the intended scope of this API.

更安全的 intent

The Safer Intents feature is a multi-phase security initiative designed to improve the security of Android's intent resolution mechanism. The goal is to protect apps from malicious actions by adding checks during intent processing and filtering intents that don't meet specific criteria.

In Android 15 the feature focused on the sending app, now with Android 16, shifts control to the receiving app, allowing developers to opt-in to strict intent resolution using their app manifest.

Two key changes are being implemented:

  1. Explicit Intents Must Match the Target Component's Intent Filter: If an intent explicitly targets a component, it should match that component's intent filter.

  2. Intents Without an Action Cannot Match any Intent Filter: Intents that don't have an action specified shouldn't be resolved to any intent filter.

These changes only apply when multiple apps are involved and don't affect intent handling within a single app.

Impact

The opt-in nature means that developers must explicitly enable it in their app manifest for it to take effect. As a result, the feature's impact will be limited to apps whose developers:

  • Are aware of the Safer Intents feature and its benefits.
  • Actively choose to incorporate stricter intent handling practices into their apps.

This opt-in approach minimizes the risk of breaking existing apps that may rely on the current less-secure intent resolution behavior.

While the initial impact in Android 16 may be limited, the Safer Intents initiative has a roadmap for broader impact in future Android releases. The plan is to eventually make strict intent resolution the default behavior.

The Safer Intents feature has the potential to significantly enhance the security of the Android ecosystem by making it more difficult for malicious apps to exploit vulnerabilities in the intent resolution mechanism.

However, the transition to opt-out and mandatory enforcement must be carefully managed to address potential compatibility issues with existing apps.

Implementation

Developers need to explicitly enable stricter intent matching using the intentMatchingFlags attribute in their app manifest. Here is an example where the feature is opt-in for the entire app, but disabled/opt-out on a receiver:

<application android:intentMatchingFlags="enforceIntentFilter">
    <receiver android:name=".MyBroadcastReceiver" android:exported="true" android:intentMatchingFlags="none">
        <intent-filter>
            <action android:name="com.example.MY_CUSTOM_ACTION" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.example.MY_ANOTHER_CUSTOM_ACTION" />
        </intent-filter>
    </receiver>
</application>

More on the supported flags:

Flag Name Description
enforceIntentFilter Enforces stricter matching for incoming intents
none Disables all special matching rules for incoming intents. When specifying multiple flags, conflicting values are resolved by giving precedence to the "none" flag
allowNullAction Relaxes the matching rules to allow intents without an action to match. This flag to be used in conjunction with "enforceIntentFilter" to achieve a specific behavior

Testing and Debugging

When the enforcement is active, apps should function correctly if the intent caller has properly populated the intent. However, blocked intents will trigger warning log messages like "Intent does not match component's intent filter:" and "Access blocked:" with the tag "PackageManager." This indicates a potential issue that could impact the app and requires attention.

Logcat filter:

tag=:PackageManager & (message:"Intent does not match component's intent filter:" | message: "Access blocked:")

隐私设置

Android 16(API 级别 36)包含以下隐私权变更。

本地网络权限

具有 INTERNET 权限的任何应用都可以访问 LAN 上的设备。这样一来,应用便可以轻松连接到本地设备,但也存在隐私问题,例如形成用户的指纹,以及充当位置信息的代理。

本地网络保护项目旨在通过将对本地网络的访问权限置于新的运行时权限后面来保护用户的隐私。

发布计划

这项更改将在两个版本(分别为 25Q2 和 TBD)之间部署。开发者必须遵循 25Q2 的相关指南并分享反馈,因为这些保护措施将在较新的 Android 版本中强制执行。此外,他们需要按照以下指南更新依赖于隐式本地网络访问权限的场景,并为用户拒绝和撤消新权限做好准备。

影响

目前,LNP 是一项用户可选择启用的功能,这意味着只有选择启用该功能的应用会受到影响。选择启用阶段的目标是让应用开发者了解其应用的哪些部分依赖于隐式本地网络访问权限,以便他们为下一个版本做好权限保护准备。

如果应用使用以下方式访问用户的本地网络,则会受到影响:

  • 在本地网络地址上直接或通过库使用原始套接字(例如 mDNS 或 SSDP 服务发现协议)
  • 使用访问本地网络的框架级类(例如 NsdManager)

本地网络地址发送的流量需要本地网络访问权限。下表列出了一些常见用例:

应用低级网络操作 需要本地网络权限
建立出站 TCP 连接
接受传入的 TCP 连接
发送 UDP 单播、多播、广播
接收传入的 UDP 单播、多播、广播

这些限制在网络堆栈深处实现,因此适用于所有网络 API。这包括在原生代码或受管理代码中创建的套接字、Cronet 和 OkHttp 等网络库,以及在这些库之上实现的任何 API。尝试解析本地网络上的服务(即带有 .local 后缀的服务)需要本地网络权限。

上述规则的例外情况:

  • 如果设备的 DNS 服务器位于本地网络中,则向其发送或从其接收的流量(在端口 53 上)不需要本地网络访问权限。
  • 使用输出切换器作为应用内选择器的应用无需本地网络权限(更多指南将于 2025 年第 4 季度发布)。

开发者指南(用户选择启用)

如需选择启用本地网络限制,请执行以下操作:

  1. 将设备刷写为搭载 25Q2 Beta 版 3 或更高版本的 build。
  2. 安装要测试的应用。
  3. 在 adb 中切换 Appcompat 标志:

    adb shell am compat enable RESTRICT_LOCAL_NETWORK <package_name>
    
  4. 重启设备

现在,您的应用对本地网络的访问权限受到限制,任何尝试访问本地网络的操作都会导致套接字错误。如果您使用的 API 在应用进程之外执行本地网络操作(例如 NsdManager),则在用户选择启用该功能期间,这些 API 不会受到影响。

如需恢复访问权限,您必须向应用授予 NEARBY_WIFI_DEVICES 权限。

  1. 确保应用在其清单中声明了 NEARBY_WIFI_DEVICES 权限。
  2. 依次前往设置 > 应用 > [应用名称] > 权限 > 附近的设备 > 允许

现在,您的应用对本地网络的访问权限应该已恢复,并且所有场景都应像在应用选择启用之前一样正常运行。

本地网络保护功能开始强制执行后,应用网络流量将受到以下影响。

权限 出站 LAN 请求 出站/入站互联网请求 入站 LAN 请求
已授予 Works Works Works
未授予 最差排行榜 Works 最差排行榜

使用以下命令关闭 App-Compat 标志

adb shell am compat disable RESTRICT_LOCAL_NETWORK <package_name>

错误

每当调用套接字向本地网络地址调用 send 或 send 变体时,系统都会将因这些限制而产生的错误返回给调用套接字。

错误示例:

sendto failed: EPERM (Operation not permitted)

sendto failed: ECONNABORTED (Operation not permitted)

本地网络定义

本项目中的本地网络是指使用支持广播的网络接口(例如 Wi-Fi 或以太网)的 IP 网络,但不包括移动网络 (WWAN) 或 VPN 连接。

以下网络被视为本地网络:

IPv4

  • 169.254.0.0/16 // 链路本地
  • 100.64.0.0/10 // CGNAT
  • 10.0.0.0/8 // RFC1918
  • 172.16.0.0/12 // RFC1918
  • 192.168.0.0/16 // RFC1918

IPv6

  • 链路本地
  • 直接连接的路线
  • Thread 等桩网络
  • 多个子网(待定)

此外,多播地址 (224.0.0.0/4、ff00::/8) 和 IPv4 广播地址 (255.255.255.255) 都被归类为本地网络地址。

应用拥有的照片

When prompted for photo and video permissions by an app targeting SDK 36 or higher on devices running Android 16 or higher, users who choose to limit access to selected media will see any photos owned by the app pre-selected in the photo picker. Users can deselect any of these pre-selected items, which will revoke the app's access to those photos and videos.