Android 生態系統涵蓋多樣化的裝置,打造同時能充分利用新硬體或高端功能、又能在所有裝置上提供良好使用體驗的應用程序是一項挑戰。在 Android 12 中,我們引入了「媒體性能等級 (Media Performance Class, MPC)」標準,幫助開發者更好地了解裝置的能力並識別高性能裝置。如果您需要回顧什麼是 MPC,請參閱我們之前的部落格文章《使用性能等級優化您的用戶體驗》或查看性能等級的相關文件。
今年早些時候,我們發布了 Jetpack Core Performance 庫的首個穩定版本,這是我們推薦的解決方案,用於更可靠地獲取裝置的 MPC 等級。該庫特別引入了 PlayServicesDevicePerformance 類,這是一個 API,能夠查詢 Google Play Services 以獲取當前裝置和版本的最新 MPC 等級。稍後我會進一步探討技術細節,但讓我們先看看 Google 地圖如何藉助 MPC,根據不同裝置量身打造功能發布,以最佳方式適配各裝置。
Performance Class為 Google 地圖的高端體驗發布解除障礙
Google 地圖最近利用 Play Services 模組擴大了裝置覆蓋範圍,成功推進了一項功能的發布計劃。Google 地圖希望通過提升某些圖層的透明度來更新其用戶界面(UI)。然而,這意味著需要渲染更多地圖內容,結果導致許多裝置(特別是低端裝置)出現延遲增加的情況,最終不得不暫停推出該功能。為了解決這個問題,地圖團隊從現有的關鍵指標「UI 元素可見所需秒數」著手,按 MPC 等級進行細分,發現雖然所有裝置的延遲都有小幅增加,但沒有 MPC 等級的裝置出現了最大的延遲增幅。
基於這些結果,Google 地圖重新開始了功能的推出,但這次僅限於具備 MPC 等級的裝置上啟用該功能。隨著更多裝置更新並達到 MPC 的標準,這些裝置也將能夠使用更新後的 Google 地圖 UI。
全新的 Play Services 模組
MPC 等級的需求定義於 Android 相容性定義文件 (CDD) 中,隨後透過 Android 相容性測試套件 (CTS) 驗證裝置與 Android 版本是否符合這些需求。Jetpack Core Performance 庫的 Play Services 模組利用這些測試結果,不斷更新裝置的 MPC 等級報告,無需開發者額外操作。這也意味著,對於新推出的裝置,您可以立即獲取其 MPC 等級,而無需自行採購並測試,因為這些裝置已通過 CTS 驗證。若 Google Play Services 無法提供裝置的 MPC 等級,該庫會回退到 OEM 在建置時聲明的 MPC 等級常數。
截至目前,已有超過 1.9 億台市售裝置報告了 MPC 等級,涵蓋 500 多個型號,來自 40 多個品牌。隨著較舊的裝置更新至 Android 11 或更高版本,這一覆蓋範圍將持續擴大。
使用 Core Performance 庫
要使用 Jetpack Core Performance,首先在您的 Gradle 配置中添加相關模組的依賴,然後創建 DevicePerformance 的實例。初始化 DevicePerformance 應該只在應用中進行一次,並且儘早進行——例如,在 Application 類的 onCreate() 生命週期事件中。以下是使用 Google Play 服務實現的 DevicePerformance 示例:
// Implementation of Jetpack Core library.
implementation("androidx.core:core-ktx:1.12.0")
// Enable APIs to query for device-reported performance class.
implementation("androidx.core:core-performance:1.0.0")
// Enable APIs to query Google Play Services for performance class.
implementation("androidx.core:core-performance-play-services:1.0.0")import androidx.core.performance.play.services.PlayServicesDevicePerformance
class MyApplication : Application() {
lateinit var devicePerformance: DevicePerformance
override fun onCreate() {
// Use a class derived from the DevicePerformance interface
devicePerformance = PlayServicesDevicePerformance(applicationContext)
}
}稍後在應用中,當您想要檢索裝置的 MPC 等級時,可以調用 getMediaPerformanceClass() 方法:
class MyActivity : Activity() {
private lateinit var devicePerformance: DevicePerformance
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Note: Good app architecture is to use a dependency framework. See
// https://developer.android.com/training/dependency-injection for more
// information.
devicePerformance = (application as MyApplication).devicePerformance
}
override fun onResume() {
super.onResume()
when {
devicePerformance.mediaPerformanceClass >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> {
// MPC level 34 and later.
// Provide the most premium experience for the highest performing devices.
}
devicePerformance.mediaPerformanceClass == Build.VERSION_CODES.TIRAMISU -> {
// MPC level 33.
// Provide a high quality experience.
}
else -> {
// MPC level 31, 30, or undefined.
// Remove extras to keep experience functional.
}
}
}
}使用 Performance Class 時,以下是一些常見的策略:
MPC 旨在識別高端裝置,因此您可以預期每年頂級裝置會報告 MPC 等級,這些通常是您希望能夠長期支持的裝置。例如,Pixel 9 Pro 在 Android 14 發布時報告了 MPC 等級 34,這是該版本推出時的最新等級定義。
您應該將 MPC 當作您現有裝置分群解決方案的補充,例如查詢裝置的靜態規格或手動封鎖有問題的裝置。MPC 在新裝置推出時尤其有用,您可以使用 MPC 在新裝置發佈時就了解其性能能力,而不需要自行獲取硬體或手動測試每一台裝置。
一個很好的入門步驟是將 MPC 等級納入您的遙測數據中。這有助於您識別錯誤報告中的模式,或通過按 MPC 等級細分關鍵指標來更好地了解用戶群使用的裝置。從這裡開始,您可以考慮將 MPC 作為實驗管道中的一個維度,例如,基於 MPC 等級設置 A/B 測試組,或從最高的 MPC 等級開始逐步推出功能,這正是 Google 地圖所採用的方法。
您還可以進一步使用 MPC 來調整用戶端功能,例如,根據 MPC 等級的同時編解碼保證來調整應用嘗試同時播放的視頻數量。然而,在這種方法中,請確保查詢裝置的運行時能力,因為它們可能會因裝置的環境或狀態而有所不同。
0 留言