行為變更:所有應用程式

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

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

核心功能

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

應用程式記憶體限制

Android 17 會根據裝置的總 RAM 導入應用程式記憶體限制,為應用程式和 Android 使用者打造更穩定且可預測的環境。在 Android 17 中,系統會保守地設定限制,建立系統基準,以極端的記憶體流失和其他離群值為目標,避免這些情況觸發全系統不穩定,導致 UI 延遲、電池耗電量增加和應用程式遭終止。我們預期絕大多數應用程式工作階段受到的影響極小,但仍建議您遵循下列記憶體最佳做法,包括建立記憶體基準。

您可以在 ApplicationExitInfo 中呼叫 getDescription,判斷應用程式工作階段是否受到影響;如果受到影響,結束原因會是 REASON_OTHER,說明則會包含 "MemoryLimiter:AnonSwap" 字串和其他資訊。您也可以搭配使用觸發式剖析TRIGGER_TYPE_ANOMALY,在達到記憶體上限時收集堆積傾印。

Android Studio Profiler 中的 LeakCanary 工作。

為協助您找出記憶體流失問題,Android Studio Panda 直接在 Android Studio 分析器中新增 LeakCanary 整合功能,做為專用工作,並在 IDE 中提供相關資訊,且完全整合至您的原始碼。

隱私權

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

簡訊動態密碼防護

Beginning with Android 17, Android is expanding its protection for SMS messages containing one-time passwords (OTP).

In previous versions of Android, this protection was primarily focused on the SMS Retriever format. Delivery of messages containing an SMS retriever hash was delayed for most apps for three hours. However, certain certain apps (like the default SMS handler) were exempt from the delay, and the app that owned the hash was also exempted.

Beginning with Android 17, the protection is also applied to WebOTP format messages. If an app has permission to read SMS messages but is not the intended recipient of a WebOTP message (as determined by domain verification), the message is not accessible to the app until three hours after the message's receipt. This change is intended to improve user security by ensuring that only apps associated with the domain mentioned in the message can programmatically read the verification code.

During this three hour delay, the SMS_RECEIVED_ACTION broadcast is withheld and SMS provider database queries are filtered. The SMS message is available to these apps after the delay. This change applies to all apps, regardless of their target API level.

Certain apps such as the default SMS assistant app, connected device companion apps, etc., are exempted from this delay. All apps that rely on reading SMS messages for OTP extraction should transition to using SMS Retriever or SMS User Consent APIs to ensure continued functionality.

安全性

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

usesClearTraffic 淘汰計畫

我們計畫在日後推出的版本中淘汰 usesCleartextTraffic 元素。 如果應用程式需要建立未加密 (HTTP) 連線,請改用網路安全性設定檔,指定應用程式需要建立明文連線的網域。

請注意,網路安全性設定檔僅適用於 API 級別 24 以上版本。如果應用程式的最低 API 級別低於 24,請同時執行下列操作:

  • usesCleartextTraffic 屬性設為 true
  • 使用網路設定檔

如果應用程式的最低 API 級別為 24 以上,您可以使用網路設定檔,不必設定 usesCleartextTraffic

限制隱含 URI 授權

目前,如果應用程式啟動的意圖含有 URI,且動作為 ACTION_SENDACTION_SEND_MULTIPLEACTION_IMAGE_CAPTURE,系統會自動授予目標應用程式 URI 讀取和寫入權限。但從 Android 18 開始,系統將不再自動授予這些權限。因此,建議應用程式明確授予相關 URI 權限,而非依賴系統授予。

如要在應用程式中偵測這些意圖的使用情形,請搭配使用 StrictModedetectImplicitUriPermissionGrant(),觸發違規行為:

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_SENDACTION_SEND_MULTIPLE 意圖:

Kotlin

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

Java

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

針對 ACTION_IMAGE_CAPTURE 意圖,同時加入 FLAG_GRANT_READ_URI_PERMISSIONFLAG_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);

每個應用程式的 Keystore 限制

Apps should avoid creating excessive numbers of keys in Android Keystore, because it is a shared resource for all apps on the device. Beginning with Android 17, the system enforces a limit on the number of keys an app can own. The limit is 50,000 keys for non-system apps targeting Android 17 (API level 37) or higher, and 200,000 keys for all other apps. System apps have a limit of 200,000 keys, regardless of which API level they target.

If an app attempts to create keys beyond the limit, the creation fails with a KeyStoreException. The exception's message string contains information about the key limit. If the app calls getNumericErrorCode() on the exception, the return value depends on what API level the app targets:

  • Apps targeting Android 17 (API level 37) or higher: getNumericErrorCode() returns the new ERROR_TOO_MANY_KEYS value.
  • All other apps: getNumericErrorCode() returns ERROR_INCORRECT_USAGE.

封鎖跨設定檔迴路流量

從 Android 17 開始,系統預設不再允許跨設定檔迴路流量。同一設定檔內的迴路流量不受影響。無論應用程式指定哪個 API 級別,只要在 Android 17 以上版本上執行,都會受到這項變更影響。

使用者體驗和系統 UI

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 包含下列異動項目,這些變更會影響應用程式與鍵盤和觸控板等人工輸入裝置的互動方式。

在指標擷取期間,觸控板預設會傳送相對事件

從 Android 17 開始,如果應用程式使用 View.requestPointerCapture() 要求指標擷取,且使用者使用觸控板,系統會辨識使用者觸控時的指標移動和捲動手勢,並以與擷取滑鼠時指標和捲動滾輪移動相同的方式,將這些手勢回報給應用程式。在大多數情況下,支援擷取滑鼠的應用程式不必為觸控板新增特殊處理邏輯。詳情請參閱 View.POINTER_CAPTURE_MODE_RELATIVE 的說明文件。

先前,系統不會嘗試辨識觸控板的手勢,而是以類似觸控螢幕觸控的格式,將原始的絕對手指位置傳送至應用程式。如果應用程式仍需要這項絕對資料,應改為使用 View.POINTER_CAPTURE_MODE_ABSOLUTE 呼叫新的 View.requestPointerCapture(int) 方法。

媒體

Android 17 包含下列媒體行為變更。

背景音訊強化

Beginning with Android 17, the audio framework enforces restrictions on background audio interactions including audio playback, audio focus requests, and volume change APIs to ensure that these changes are started intentionally by the user.

If the app tries to call audio APIs while the app is not in a valid lifecycle, the audio playback and volume change APIs fail silently without throwing an exception or providing a failure message. The audio focus API fails with the result code AUDIOFOCUS_REQUEST_FAILED.

For more information, including mitigation strategies, see Background audio hardening.

連線能力

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