行為變更:指定 Android 16 以上版本的應用程式

和先前版本一樣,Android 16 也包含可能會影響應用程式的行為變更。以下行為變更僅適用於指定 Android 16 以上版本的應用程式。如果應用程式指定 Android 16 以上版本,建議您視情況修改應用程式,以支援這些行為。

此外,無論應用程式的 targetSdkVersion 為何,請務必查看對所有 Android 16 應用程式有影響的行為變更清單。

使用者體驗和系統 UI

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, 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.

Elegant 字型 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(API 级别 36)为目标平台的应用,或以 Android 15(API 级别 35)为目标平台但未通过将 elegantTextHeight 属性设置为 false 来替换默认值的应用,其
elegantTextHeight 行为。

核心功能

Android 16 (API 級別 36) 包含下列異動項目,這類變更會修改或擴充 Android 系統的各種核心功能。

固定費率工作排程最佳化

在以 Android 16 为目标平台之前,如果 scheduleAtFixedRate 因不在有效的进程生命周期内而错过了任务执行,则当应用返回到有效的生命周期时,所有错过的执行会立即执行。

以 Android 16 为目标平台时,当应用返回到有效的生命周期时,系统会立即执行最多 1 次未执行的 scheduleAtFixedRate 执行。此行为变更预计会提升应用性能。在您的应用中测试此行为,检查您的应用是否受到影响。您还可以使用应用兼容性框架并启用 STPE_SKIP_MULTIPLE_MISSED_PERIODIC_TASKS 兼容性标志进行测试。

裝置板型規格

Android 16 (API 級別 36) 包含下列變更,適用於在大螢幕裝置上顯示的應用程式。

自動調整式版面配置

Android 應用程式現在可在各種裝置 (例如手機、平板電腦、折疊式裝置、電腦、車輛和電視) 上執行,並在大螢幕上支援多種視窗模式 (例如分割畫面和電腦視窗模式),因此開發人員應建構可適應任何螢幕和視窗大小的 Android 應用程式,無論裝置方向為何。在現今的多裝置世界中,限制螢幕方向和大小調整等模式過於嚴格。

忽略螢幕方向、可調整大小和顯示比例限制

如果應用程式指定 Android 16 (API 級別 36),則在最小寬度 >= 600dp 的螢幕上,不再適用螢幕方向、大小調整和顯示比例限制。無論顯示比例或使用者偏好的螢幕方向為何,應用程式都會填滿整個顯示視窗,且不會使用左右黑邊。

這項異動會導入新的標準平台行為。Android 正在轉向一種模式,讓應用程式能適應各種螢幕方向、顯示大小和顯示比例。固定螢幕方向或限制大小調整等限制,會阻礙應用程式的適應性。讓應用程式具備自動調整功能,提供最佳使用者體驗。

您也可以使用應用程式相容性架構,並啟用 UNIVERSAL_RESIZABLE_BY_DEFAULT 相容性旗標,測試這項行為。

常見破壞性變更

忽略螢幕方向、大小調整和顯示比例限制,可能會影響應用程式在某些裝置上的 UI,尤其是專為鎖定直向的小型版面配置設計的元素,例如版面配置拉伸、動畫和元件超出螢幕範圍等問題。對顯示比例或螢幕方向的任何假設,都可能導致應用程式出現視覺問題。進一步瞭解如何避免這些問題,並改善應用程式的自動調整行為。

允許裝置旋轉會導致更多活動重新建立,如果未妥善保留,可能會遺失使用者狀態。如要瞭解如何正確儲存 UI 狀態,請參閱「儲存 UI 狀態」。

實作詳細資料

在全螢幕和多視窗模式下,大螢幕裝置會忽略下列資訊清單屬性和執行階段 API:

系統會忽略 screenOrientation、setRequestedOrientation() 和 getRequestedOrientation() 的下列值:

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

android:resizeableActivity="false"、android:minAspectRatio 和 android:maxAspectRatio 對顯示大小調整功能沒有影響。

如果應用程式指定 Android 16 (API 級別 36),系統預設會在大螢幕上忽略應用程式的方向、大小調整和顯示比例限制。如果應用程式尚未完全準備就緒,可以選擇停用這項功能,暫時覆寫這項行為,這樣就會像先前一樣,進入相容模式。

例外狀況

Android 16 的螢幕方向、大小調整和顯示比例限制不適用於下列情況:

  • 遊戲 (根據 android:appCategory 旗標)
  • 使用者在裝置的顯示比例設定中,明確選擇採用應用程式的預設行為
  • 小於 sw600dp 的螢幕

暫時退出

如要停用特定活動,請宣告 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) 包含下列與健康和運動資料相關的異動。

健康與健身權限

For apps targeting Android 16 (API level 36) or higher, BODY_SENSORS permissions use more granular permissions under android.permissions.health, which Health Connect also uses. As of Android 16, any API previously requiring BODY_SENSORS or BODY_SENSORS_BACKGROUND requires the corresponding android.permissions.health permission instead. This affects the following data types, APIs, and foreground service types:

If your app uses these APIs, it should request the respective granular permissions:

These permissions are the same as those that guard access to reading data from Health Connect, the Android datastore for health, fitness, and wellness data.

Mobile apps

Mobile apps migrating to use the READ_HEART_RATE and other granular permissions must also declare an activity to display the app's privacy policy. This is the same requirement as Health Connect.

連線能力

Android 16 (API 級別 36) 包含下列藍牙堆疊變更,可改善與周邊裝置的連線能力。

處理繫結遺失和加密變更的新意圖

作为改进了对键值对丢失的处理的一部分,Android 16 还引入了 2 个新 intent,以便应用更好地了解键值对丢失和加密更改。

以 Android 16 为目标平台的应用现在可以:

  • 在检测到远程键盘连接丢失时接收 ACTION_KEY_MISSING intent,以便提供更具信息量的用户反馈并采取适当的措施。
  • 每当链接的加密状态发生变化时,都会收到 ACTION_ENCRYPTION_CHANGE intent。这包括加密状态更改、加密算法更改和加密密钥大小更改。如果应用在稍后收到 ACTION_ENCRYPTION_CHANGE intent 时成功加密了链接,则必须将该绑定视为已恢复。

适应不同的 OEM 实现

虽然 Android 16 引入了这些新 intent,但其实现和广播可能会因不同的设备制造商 (OEM) 而异。为了确保您的应用在所有设备上都能提供一致且可靠的体验,开发者应设计其绑定丢失处理机制,以妥善适应这些潜在的变化。

我们建议您采用以下应用行为:

  • 如果广播 ACTION_KEY_MISSING intent:

    系统会断开 ACL(异步无连接)链接,但会保留设备的配对信息(如此处所述)。

    您的应用应将此 intent 用作检测配对丢失的主要信号,并在发起设备忘记或重新配对之前引导用户确认远程设备是否在范围内。

    如果设备在收到 ACTION_KEY_MISSING 后断开连接,您的应用应谨慎重新连接,因为设备可能已不再与系统绑定。

  • 如果未广播 ACTION_KEY_MISSING intent:

    ACL 链接将保持连接状态,系统会移除设备的配对信息,与 Android 15 中的行为相同。

    在这种情况下,您的应用应继续使用与之前的 Android 版本相同的现有配对丢失处理机制,以检测和管理配对丢失事件。

移除藍牙繫結的新方法

现在,以 Android 16 为目标平台的所有应用都可以使用 CompanionDeviceManager 中的公共 API 解除蓝牙设备配对。如果配套设备作为 CDM 关联进行管理,则应用可以在关联的设备上使用新的 removeBond(int) API 触发蓝牙配对的移除。该应用可以通过监听蓝牙设备广播事件 ACTION_BOND_STATE_CHANGED 来监控配对状态变化。

安全性

Android 16 (API 級別 36) 包含下列安全性異動。

MediaStore 版本鎖定

針對指定 Android 16 以上版本的應用程式,MediaStore#getVersion() 現已成為每個應用程式的專屬值。這麼做可移除版本字串中的識別屬性,以免遭到濫用,並防止用於指紋辨識技術。應用程式不應對此版本的格式做出任何假設。應用程式在使用此 API 時應已處理版本變更,且在大多數情況下,不必變更目前的行為,除非開發人員嘗試推斷超出此 API 預期範圍的其他資訊。

更安全的意圖

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, control shifts 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:")

GPU 系統呼叫篩選

為強化 Mali GPU 介面,正式版建構作業已封鎖已淘汰或僅用於 GPU 開發的 Mali GPU IOCTL。此外,用於 GPU 分析的 IOCTL 已限制為殼層程序或可進行偵錯的應用程式。如要進一步瞭解平台層級政策,請參閱 SAC 更新。

這項異動適用於使用 Mali GPU 的 Pixel 裝置 (Pixel 6-9)。Arm 已在 r54p2 版本的 Documentation/ioctl-categories.rst 中提供 IOCTL 的官方分類。日後推出的驅動程式版本會繼續維護這份清單。

這項異動不會影響支援的圖形 API (包括 Vulkan 和 OpenGL),預計也不會影響開發人員或現有應用程式。Streamline Performance Analyzer 和 Android GPU 檢查器等 GPU 分析工具則不受影響。

測試

如果看到類似下方的 SELinux 拒絕訊息,代表應用程式可能受到這項異動影響:

06-30 10:47:18.617 20360 20360 W roidJUnitRunner: type=1400 audit(0.0:85): avc:  denied  { ioctl }
for  path="/dev/mali0" dev="tmpfs" ino=1188 ioctlcmd=0x8023
scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:gpu_device:s0 tclass=chr_file
permissive=0 app=com.google.android.selinux.pts

如果應用程式需要使用遭封鎖的 IOCTL,請回報錯誤,並指派給 android-partner-security@google.com。

常見問題

  1. 這項政策異動是否適用於所有原始設備製造商? 這項變更可選擇加入,但任何想使用這種強化方法的原始設備製造商 (OEM) 都能使用。如需導入這項異動的操作說明,請參閱導入說明文件。

  2. 是否必須變更原始設備製造商 (OEM) 程式碼集才能實作這項功能,還是新版 AOSP 預設會提供這項功能?根據預設,平台層級的變更會隨新的 AOSP 版本推出。如果供應商想套用這項變更,可以選擇在程式碼集中加入。

  3. SoC 是否負責更新 IOCTL 清單?舉例來說,如果我的裝置使用 ARM Mali GPU,是否需要與 ARM 聯絡才能進行任何變更? 個別 SoC 必須在驅動程式發布時,更新每個裝置的 IOCTL 清單。舉例來說,ARM 會在驅動程式更新時,更新已發布的 IOCTL 清單。不過,原始設備製造商 (OEM) 應確保將更新內容納入 SEPolicy,並視需要將任何選取的自訂 IOCTL 新增至清單。

  4. 這項異動會自動套用至所有市售 Pixel 裝置嗎?還是使用者需要手動切換設定才能套用? 這項異動適用於所有使用 Mali GPU 的 Pixel 市售裝置 (Pixel 6-9)。使用者無須採取任何行動,即可套用這項變更。

  5. 使用這項政策是否會影響核心驅動程式的效能? 這項政策已在 Mali GPU 上使用 GFXBench 進行測試,並未觀察到 GPU 效能有任何可測量的變化。

  6. IOCTL 清單是否必須與目前的使用者空間和核心驅動程式版本一致? 是的,允許的 IOCTL 清單必須與使用者空間和核心驅動程式支援的 IOCTL 同步。如果使用者空間或核心驅動程式中的 IOCTL 有更新,SEPolicy IOCTL 清單也必須一併更新。

  7. ARM 已將 IOCTL 分類為「受限」/「檢測」,但我們想在實際工作環境用途中使用部分 IOCTL,和/或拒絕其他 IOCTL。 個別原始設備製造商 (OEM)/SoC 負責根據使用者空間 Mali 程式庫的設定,決定如何分類使用的 IOCTL。ARM 的清單可用於協助決定這些項目,但每個原始設備製造商 (OEM)/SoC 的用途可能不同。

隱私權

Android 16 (API 級別 36) 包含下列隱私權異動。

區域網路權限

任何拥有 INTERNET 权限的应用都可以访问 LAN 中的设备。 这让应用可以轻松连接到本地设备,但也会带来隐私问题,例如生成用户指纹,以及推断出位置信息。

本地网络保护项目旨在通过新的运行时权限控制对本地网络的访问,从而保护用户隐私。

发布计划

此项变更将在 25Q2 和 26Q2 这两个版本之间部署。 开发者必须在 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),则在选择启用阶段不会受到影响。

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

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

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

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

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

使用以下命令关闭应用兼容性标志

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) 也被归类为本地网络地址。

應用程式擁有的相片

当面向 SDK 36 或更高版本的应用在搭载 Android 16 或更高版本的设备上提示用户授予照片和视频权限时,如果用户选择限制对所选媒体的访问权限,则会在照片选择器中看到该应用拥有的所有照片。用户可以取消选择任何这些预选项,这会撤消该应用对这些照片和视频的访问权限。