行為變更:所有應用程式

Android 16 平台包含可能對應用程式造成影響的行為變更。無論 targetSdkVersion 為何,當應用程式在 Android 16 上執行時,下列行為變更將會套用至所有應用程式。您應測試應用程式,並視需要修改,以便在適當情況下支援這些變更。

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

核心功能

Android 16 (API 級別 36) 包含下列變更,可修改或擴充 Android 系統的各種核心功能。

JobScheduler 配額最佳化

從 Android 16 開始,我們會根據下列因素調整一般和快速工作執行的執行階段配額:

  • 應用程式所屬的應用程式待機值區:在 Android 16 中,系統會開始透過充足的執行階段配額,強制執行有效待機值區。
  • 如果工作在應用程式處於頂層狀態時開始執行:在 Android 16 中,如果工作是在應用程式向使用者顯示時啟動,且在應用程式隱藏後繼續執行,則會遵守工作執行時間配額。
  • 如果工作在執行前景服務時執行:在 Android 16 中,與前景服務並行執行的工作會遵守工作執行階段配額。如果您使用工作進行使用者啟動的資料移轉,請考慮改用使用者啟動的資料移轉工作

這項異動會影響使用 WorkManager、JobScheduler 和 DownloadManager 安排的工作。如要偵錯工作停止的原因,建議您呼叫 WorkInfo.getStopReason() (如果是 JobScheduler 工作,請呼叫 JobParameters.getStopReason()),記錄工作停止的原因。

如要瞭解應用程式狀態如何影響可使用的資源,請參閱「電源管理資源限制」。如要進一步瞭解如何以最佳方式節省電量,請參閱「針對工作排程 API 最佳化電池用量」指南。

此外,我們建議您運用 Android 16 中推出的新版 JobScheduler#getPendingJobReasonsHistory API,瞭解工作未執行的原因。

測試

如要測試應用程式的行為,只要應用程式在 Android 16 裝置上執行,您就可以啟用特定工作配額最佳化功能的覆寫功能。

如要停用「頂端狀態會遵守工作執行階段配額」的強制執行,請執行下列 adb 指令:

adb shell am compat enable OVERRIDE_QUOTA_ENFORCEMENT_TO_TOP_STARTED_JOBS APP_PACKAGE_NAME

如要停用「與前景服務同時執行的作業會遵守作業執行階段配額」的強制執行功能,請執行下列 adb 指令:

adb shell am compat enable OVERRIDE_QUOTA_ENFORCEMENT_TO_FGS_JOBS APP_PACKAGE_NAME

如要測試特定應用程式待命值區行為,可以使用下列 adb 指令設定應用程式的應用程式待命值區:

adb shell am set-standby-bucket APP_PACKAGE_NAME active|working_set|frequent|rare|restricted

如要瞭解應用程式所屬的應用程式待命值區,可以使用下列 adb 指令取得應用程式的應用程式待命值區:

adb shell am get-standby-bucket APP_PACKAGE_NAME

因工作空白而停止的原因

當與工作相關聯的 JobParameters 物件已收集為垃圾,但未呼叫 JobService#jobFinished(JobParameters, boolean) 來傳送工作完成信號時,就會發生遺棄的工作。這表示工作可能在未經應用程式察覺的情況下執行及重新排程。

依賴 JobScheduler 的應用程式不會維持對 JobParameters 物件的強參照,且超時現在會授予新的 STOP_REASON_TIMEOUT_ABANDONED 工作停止原因,而非 STOP_REASON_TIMEOUT

如果新棄站原因經常發生,系統會採取緩解措施來減少工作頻率。

應用程式應使用新的停止原因來偵測並減少遺失的工作。

如果您使用 WorkManager、AsyncTask 或 DownloadManager,則不會受到影響,因為這些 API 會代表應用程式管理工作生命週期。

完全淘汰 JobInfo#setImportantWhileForeground

JobInfo.Builder#setImportantWhileForeground(boolean) 方法會在排程應用程式處於前景或暫時豁免背景限制時,指出工作的重要性。

自 Android 12 (API 級別 31) 起,此方法已淘汰。自 Android 16 起,這項方法不再有效,系統會忽略呼叫此方法的行為。

這項功能移除作業也適用於 JobInfo#isImportantWhileForeground()。自 Android 16 起,如果呼叫該方法,該方法會傳回 false

已排序的廣播優先順序範圍不再是全域

Android 應用程式可定義廣播接收器的優先順序,藉此控制接收器接收及處理廣播的順序。對於資訊清單宣告的接收器,應用程式可以使用 android:priority 屬性來定義優先順序,而對於內容註冊的接收器,應用程式可以使用 IntentFilter#setPriority() API 來定義優先順序。傳送廣播訊息時,系統會依優先順序 (由高至低) 將訊息傳送給接收器。

在 Android 16 中,系統無法保證使用 android:priority 屬性或在不同程序中使用 IntentFilter#setPriority() 的廣播傳送順序。廣播優先順序只會在相同的應用程式程序中受到尊重,而不會跨所有程序。

此外,廣播優先順序會自動限制在 (SYSTEM_LOW_PRIORITY + 1, SYSTEM_HIGH_PRIORITY - 1) 的範圍內。只有系統元件可將 SYSTEM_LOW_PRIORITYSYSTEM_HIGH_PRIORITY 設為廣播優先順序。

如果您的應用程式執行下列任一操作,可能會受到影響:

  1. 您的應用程式已宣告多個使用相同廣播意圖的程序,並且預期會按照優先順序接收這些意圖。
  2. 您的應用程式程序會與其他程序互動,並預期以特定順序接收廣播意圖。

如果程序需要相互協調,則應使用其他協調管道進行通訊。

ART 內部變更

Android 16 includes the latest updates to the Android Runtime (ART) that improve the Android Runtime's (ART's) performance and provide support for additional Java features. Through Google Play System updates, these improvements are also available to over a billion devices running Android 12 (API level 31) and higher.

As these changes are released, libraries and app code that rely on internal structures of ART might not work correctly on devices running Android 16, along with earlier Android versions that update the ART module through Google Play system updates.

Relying on internal structures (such as non-SDK interfaces) can always lead to compatibility problems, but it's particularly important to avoid relying on code (or libraries containing code) that leverages internal ART structures, since ART changes aren't tied to the platform version the device is running on and they go out to over a billion devices through Google Play system updates.

All developers should check whether their app is impacted by testing their apps thoroughly on Android 16. In addition, check the known issues to see if your app depends on any libraries that we've identified that rely on internal ART structures. If you do have app code or library dependencies that are affected, seek public API alternatives whenever possible and request public APIs for new use cases by creating a feature request in our issue tracker.

16 KB 頁面大小相容模式

Android 15 推出了 16 KB 記憶體分頁支援功能,以提升平台效能。Android 16 新增了相容模式,可讓為 4 KB 記憶體分頁建構的部分應用程式,在針對 16 KB 記憶體分頁設定的裝置上執行。

當應用程式在搭載 Android 16 以上版本的裝置上執行時,如果 Android 偵測到應用程式有 4 KB 對齊的記憶體分頁,就會自動使用相容性模式,並向使用者顯示通知對話方塊。在 AndroidManifest.xml 中設定 android:pageSizeCompat 屬性以啟用向後相容性模式,可防止應用程式啟動時顯示對話方塊。如要使用 android:pageSizeCompat 屬性,請使用 Android 16 SDK 編譯應用程式。

為獲得最佳效能、可靠性和穩定性,應用程式仍應以 16 KB 對齊。如要進一步瞭解如何更新應用程式以支援 16 KB 記憶體分頁,請參閱近期的網誌文章

系統偵測到 4 KB 對齊的應用程式在 16 KB 對齊的情況下執行時,可獲得更佳的效能時,會顯示相容模式對話方塊。

使用者體驗和系統 UI

Android 16 (API 級別 36) 包含下列異動,旨在打造更一致、直觀的使用者體驗。

淘汰令人混淆的無障礙公告

Android 16 deprecates accessibility announcements, characterized by the use of announceForAccessibility or the dispatch of TYPE_ANNOUNCEMENT accessibility events. These can create inconsistent user experiences for users of TalkBack and Android's screen reader, and alternatives better serve a broader range of user needs across a variety of Android's assistive technologies.

Examples of alternatives:

The reference documentation for the deprecated announceForAccessibility API includes more details about suggested alternatives.

支援三按鈕操作模式

Android 16 為已正確遷移至預測返回功能的應用程式,提供 3 鍵導覽的預測返回功能支援。長按返回按鈕會啟動預測返回動畫,讓您預覽返回滑動手勢會帶您前往的位置。

這項行為適用於系統中所有支援預測返回動畫的區域,包括系統動畫 (返回首頁、跨工作和跨活動)。

在 3 個按鈕操作模式中,預測返回動畫。

自動套用主題色至應用程式圖示

Beginning with Android 16 QPR 2, Android automatically applies themes to app icons to create a cohesive home screen experience. This occurs if an app does not provide its own themed app icon. Apps can control the design of their themed app icon by including a monochrome layer within their adaptive icon and previewing what their app icon will look like in Android Studio.

裝置板型規格

Android 16 (API 級別 36) 包含以下變更,適用於虛擬裝置擁有者將應用程式投放到螢幕時。

虛擬裝置擁有者覆寫

虛擬裝置擁有者是可信任或具備特殊權限的應用程式,可建立及管理虛擬裝置。虛擬裝置擁有者可以在虛擬裝置上執行應用程式,然後將應用程式投影到遠端裝置的螢幕,例如個人電腦、虛擬實境裝置或車輛資訊娛樂系統。虛擬裝置擁有者使用本機裝置,例如手機。

手機上的虛擬裝置擁有者建立虛擬裝置,將應用程式投放到遠端螢幕。

個別應用程式覆寫值

在搭載 Android 16 (API 級別 36) 的裝置上,虛擬裝置擁有者可以覆寫所管理特定虛擬裝置的應用程式設定。舉例來說,為了改善應用程式版面配置,虛擬裝置擁有者在將應用程式投影到外部螢幕時,可以忽略方向、長寬比和可調整大小的限制。

常見的破壞性變更

Android 16 的行為可能會影響應用程式在大型螢幕 (例如車輛螢幕或 Chromebook) 上的 UI,尤其是專為直向小螢幕設計的版面配置。如要瞭解如何讓應用程式自動調整版面配置,以配合所有裝置板型規格,請參閱「關於自動調整版面配置」。

參考資料

隨附應用程式串流

安全性

Android 16 (API 級別 36) 包含多項異動,可提升系統安全性,協助保護應用程式和使用者免受惡意應用程式侵害。

安全性提高,避免意圖重新導向攻擊

Android 16 provides default security against general Intent redirection attacks, with minimum compatibility and developer changes required.

We are introducing by-default security hardening solutions to Intent redirection exploits. In most cases, apps that use intents normally won't experience any compatibility issues; we've gathered metrics throughout our development process to monitor which apps might experience breakages.

Intent redirection in Android occurs when an attacker can partly or fully control the contents of an intent used to launch a new component in the context of a vulnerable app, while the victim app launches an untrusted sub-level intent in an extras field of an ("top-level") Intent. This can lead to the attacker app launching private components in the context of the victim app, triggering privileged actions, or gaining URI access to sensitive data, potentially leading to data theft and arbitrary code execution.

Opt out of Intent redirection handling

Android 16 introduces a new API that allows apps to opt out of launch security protections. This might be necessary in specific cases where the default security behavior interferes with legitimate app use cases.

For applications compiling against Android 16 (API level 36) SDK or higher

You can directly use the removeLaunchSecurityProtection() method on the Intent object.

val i = intent
val iSublevel: Intent? = i.getParcelableExtra("sub_intent")
iSublevel?.removeLaunchSecurityProtection() // Opt out from hardening
iSublevel?.let { startActivity(it) }
For applications compiling against Android 15 (API level 35) or lower

While not recommended, you can use reflection to access the removeLaunchSecurityProtection() method.

val i = intent
val iSublevel: Intent? = i.getParcelableExtra("sub_intent", Intent::class.java)
try {
    val removeLaunchSecurityProtection = Intent::class.java.getDeclaredMethod("removeLaunchSecurityProtection")
    removeLaunchSecurityProtection.invoke(iSublevel)
} catch (e: Exception) {
    // Handle the exception, e.g., log it
} // Opt-out from the security hardening using reflection
iSublevel?.let { startActivity(it) }

隨附應用程式不再收到探索逾時通知

Android 16 在配件裝置配對流程中導入了新行為,以保護使用者的所在位置隱私,避免遭到惡意應用程式竊取。所有在 Android 16 上執行的隨附應用程式,都不再直接透過 RESULT_DISCOVERY_TIMEOUT 收到發現逾時的通知。而是透過視覺對話方塊通知使用者逾時事件。當使用者關閉對話方塊時,應用程式會收到關於 RESULT_USER_REJECTED 關聯失敗的警示。

搜尋時間也從原本的 20 秒延長,使用者可在搜尋期間的任何時間停止裝置探索。如果在開始搜尋的頭 20 秒內發現至少一台裝置,CDM 就會停止搜尋其他裝置。

連線能力

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

改善債券損失處理方式

Starting in Android 16, the Bluetooth stack has been updated to improve security and user experience when a remote bond loss is detected. Previously, the system would automatically remove the bond and initiate a new pairing process, which could lead to unintentional re-pairing. We have seen in many instances apps not taking care of the bond loss event in a consistent way.

To unify the experience, Android 16 improved the bond loss handling to the system. If a previously bonded Bluetooth device could not be authenticated upon reconnection, the system will disconnect the link, retain local bond information, and display a system dialog informing users of the bond loss and directing them to re-pair.