行為變更:所有應用程式

Android 17 平台包含可能對應用程式造成影響的行為變更。無論 targetSdkVersion 為何,凡是在 Android 17 上執行的應用程式,「一律」都會受到下列行為變更所影響。您必須測試自己的應用程式,並視需要進行修改,以便在適當情況下支援這些變更。

另請務必查看僅對指定 Android 17 為目標版本的應用程式造成影響的行為變更。

核心功能

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

應用程式記憶體限制

Android 17 引入了基于设备 RAM 总容量的应用内存限制,旨在为应用和 Android 用户打造更稳定、更确定的环境。这些限制侧重于在内存泄漏和其他异常值触发系统级不稳定(导致界面卡顿、耗电量增加和应用被终止)之前,对其进行检测。虽然我们预计对绝大多数应用会话的影响微乎其微,但我们建议您遵循以下内存最佳实践,包括建立内存基准。

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

管理应用的内存文档提供了相关信息,可帮助您诊断应用的内存问题并优化其资源消耗。

测试应用在内存限制下的行为

您可以使用 Android 调试桥 (adb) 调整或停用任何设备上的内存限制。shell 命令 am 提供了三个子命令来调整内存限制。(这些命令对不施加内存限制的设备没有影响。)

  • am memory-limiter ignore <uid>|none|all
  • am memory-limiter manual <pid> <limit>|max|none
  • am memory-limiter status
ignore

指示内存限制器忽略部分或全部进程。 传递 UID(Android 用户 ID)会指示内存限制器忽略对与该 UID 关联的所有进程的强制执行。您还可以传递 all(忽略所有应用)或 none(不忽略任何应用)。 传递 none 会覆盖之前对 am memory-limiter ignore 的任何调用。

如果您指示内存限制器忽略某个 UID,您仍然可以通过调用 am memory-limiter manual,对应用内的进程应用手动内存限制。

manual

指示系统对具有指定 PID(进程 ID)的进程施加内存限制。内存限制指定为整数 MB;例如,传递 30 指定进程限制为 30 MB 内存。传递 max 会移除该进程的所有内存限制。 传递 none 会移除对进程设置的所有手动限制,从而恢复系统的默认限制(如果有)。

status

报告内存限制器的当前状态。该状态包括对可见和不可见进程施加的内存限制。

隱私權

Android 17 包含下列異動,可提升使用者隱私。

簡訊動態密碼防護

從 Android 17 開始,Android 將擴大保護含有動態密碼 (OTP) 的簡訊。

在舊版 Android 中,這項保護措施主要著重於簡訊擷取器格式。對於大多數應用程式,含有簡訊擷取器雜湊的訊息傳送作業延遲了三小時。不過,某些應用程式 (例如預設簡訊處理常式) 和擁有雜湊的應用程式不受延遲影響。

從 Android 17 開始,這項保護措施也適用於 WebOTP 格式訊息。如果應用程式有權讀取簡訊,但不是 WebOTP 訊息的預定接收者 (由網域驗證決定),則在收到訊息後三小時內,應用程式無法存取該訊息。這項異動旨在提升使用者安全性,確保只有與訊息中提及的網域相關聯的應用程式,才能以程式輔助方式讀取驗證碼。

在這三小時的延遲期間,系統會暫緩 SMS_RECEIVED_ACTION 廣播,並篩除「簡訊供應商」資料庫查詢。延遲時間過後,這些應用程式就能取得簡訊。這項異動適用於所有應用程式,無論目標 API 級別為何。

某些應用程式 (例如預設簡訊小幫手應用程式、連結裝置隨附應用程式等) 不受這項延遲限制。所有需要讀取簡訊來擷取動態密碼的應用程式,都應改用 SMS Retriever 或 SMS User Consent API,確保功能持續運作。

安全性

Android 17 包含下列裝置和應用程式安全性改善項目。

usesClearTraffic 淘汰計畫

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

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

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

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

限制隱含 URI 授權

目前,如果应用启动具有操作 ACTION_SEND、ACTION_SEND_MULTIPLE 或 ACTION_IMAGE_CAPTURE 的 URI 的 intent,系统会自动向目标应用授予读取和写入 URI 权限。从 Android 18 开始,系统将不再自动授予这些权限。因此,我们建议应用明确授予相关 URI 权限,而不是依赖系统来授予这些权限。

如需检测应用中这些 intent 的使用情况,请将 StrictMode 与 detectImplicitUriPermissionGrant() 搭配使用,以触发违规行为:

Kotlin

val policy = StrictMode.VmPolicy.Builder()
    .detectImplicitUriPermissionGrant()
    .penaltyLog()
    .build()
StrictMode.setVmPolicy(policy)

Java

StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
    .detectImplicitUriPermissionGrant()
    .penaltyLog()
    .build();
StrictMode.setVmPolicy(policy);

或者,您也可以监控记录的异常,其中包含系统隐式设置授权时显示的消息 Please set the grant explicitly in the app。您可以使用以下 adb 命令监控这些日志:

adb logcat | grep "Please set the grant explicitly in the app"

如需明确授予必要的权限,请将 FLAG_GRANT_READ_URI_PERMISSION 标志添加到 ACTION_SEND 和 ACTION_SEND_MULTIPLE intent:

Kotlin

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

Java

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

为 ACTION_IMAGE_CAPTURE intent 添加 FLAG_GRANT_READ_URI_PERMISSION 和 FLAG_GRANT_WRITE_URI_PERMISSION 标志:

Kotlin

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

Java

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

個別應用程式金鑰庫限制

應用程式應避免在 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 起,系統預設不再允許跨設定檔回送流量。同一設定檔內的回送流量不受影響。無論應用程式指定的 API 級別為何,這項異動適用於在 Android 17 以上版本執行的所有應用程式。

使用者體驗和系統 UI

Android 17 包含下列異動,旨在打造更一致、直覺的使用者體驗。

裝置旋轉後自動恢復預設的 IME 版面空間

从 Android 17 开始,当设备的配置发生变化(例如,通过旋转)且应用本身未处理此变化时,系统不会恢复之前的 IME 可见性。

如果您的应用发生其无法处理的配置更改,并且应用需要在更改后显示键盘,您必须明确请求显示键盘。您可以通过以下任一方式提出此请求:

  • 将 android:windowSoftInputMode 属性设置为 stateAlwaysVisible。
  • 在 activity 的 onCreate() 方法中以编程方式请求软键盘,或添加 onConfigurationChanged() 方法。

手動輸入

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 introduces autonomous re-pairing, a system-level enhancement designed to automatically resolve Bluetooth bond loss.

Previously, if a bond was lost, users had to manually navigate to Settings to unpair and then re-pair the peripheral. This feature builds upon the security improvement of Android 16 by allowing the system to re-establish bonds in the background without requiring users to manually navigate to Settings to unpair and re-pair peripherals.

While most apps will not require code changes, developers should be aware of the following behavior changes in Bluetooth stack:

  • New pairing context: The ACTION_PAIRING_REQUEST now includes the EXTRA_PAIRING_CONTEXT extra which allows apps to distinguish between a standard pairing request and an autonomous system-initiated re-pairing attempt.
  • Conditional key updates: Existing security keys will only be replaced if the re-pairing is successful and new connection meets or exceeds the security level of the previous bond.
  • Modified intent timing: The ACTION_KEY_MISSING intent is now broadcast only if the autonomous re-pairing attempt fails. This reduces unnecessary error handling in the app if the system successfully recovers the bond in the background.
  • User notification: The system manages re-pairing via new UI notifications and dialogs. Users will be prompted to confirm the re-pairing attempt to ensure they are aware of the reconnection.

Peripheral device manufacturers and companion app developers should verify that hardware and app gracefully handle bond transitions. To test this behavior, simulate a remote bond loss using either of the following methods:

  • Manually remove the bond information from the peripheral device
  • Manually unpair the device in: Settings > Connected devices