效能類別

「效能類別」是 Android 12 首次引進的標準,可定義一系列超出 Android 基準要求的裝置功能。

每個 Android 版本都有各自對應的效能類別,其定義列載於該版本的 Android 相容性定義說明文件 (CDD)Android Compatibility Test Suite (CTS) 可驗證 CDD 要求。

由於每部 Android 裝置都會聲明自身支援的效能類別,開發人員可以在執行階段查看裝置的效能類別,並提供能充分利用裝置功能的進階體驗。

如要查看裝置的效能類別等級,請使用 Jetpack Core Performance 程式庫。這個程式庫會根據版本資訊中聲明的類別或來自 Google Play 服務的資料,回報裝置的媒體效能類別。

請先在 Gradle 檔案中新增相關模組的依附元件:

Kotlin

// 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-beta02")
// Enable APIs to query Google Play Services for performance class.
implementation("androidx.core:core-performance-play-services:1.0.0-beta02")

Groovy

// 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-beta02'
// Enable APIs to query Google Play Services for performance class.
implementation 'androidx.core:core-performance-play-services:1.0.0-beta02'

Then, create an instance of a DevicePerformance implementation, such as PlayServicesDevicePerformance, in the onCreate() lifecycle event of your Application. This should only be done once in your app.

Kotlin

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)
  }
}

Java

import androidx.core.performance.play.services.PlayServicesDevicePerformance;

class MyApplication extends Application {
  DevicePerformance devicePerformance;

  @Override
  public void onCreate() {
    // Use a class derived from the DevicePerformance interface
    devicePerformance = new PlayServicesDevicePerformance(applicationContext);
  }
}

然後,您可以擷取 mediaPerformanceClass 屬性,根據裝置功能量身打造應用程式體驗:

Kotlin

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.TIRAMISU -> {
        // Performance class level 13 and later.
        // Provide the most premium experience for the highest performing devices.
      }
      devicePerformance.mediaPerformanceClass == Build.VERSION_CODES.S -> {
        // Performance class level 12.
        // Provide a high quality experience.
      }
      else -> {
        // Performance class level 11 or undefined.
        // Remove extras to keep experience functional.
      }
    }
  }
}

Java

class MyActivity extends Activity {
  private DevicePerformance devicePerformance;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    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 = ((MyApplication) getApplication()).devicePerformance;
  }

  @Override
  public void onResume() {
    super.onResume();
    if (devicePerformance.getMediaPerformanceClass() >= Build.VERSION_CODES.TIRAMISU) {
      // Performance class level 13 and later.
      // Provide the most premium experience for the highest performing devices.
    } else if (devicePerformance.getMediaPerformanceClass() == Build.VERSION_CODES.S) {
      // Performance class level 12.
      // Provide a high quality experience.
    } else {
      // Performance class level 11 or undefined.
      // Remove extras to keep experience functional.
    }
  }
}

效能類別具有前瞻相容性,所以即使不更新效能類別,裝置還是可以升級至較新的平台版本。舉例來說,某部裝置最初支援的效能類別是 12,雖然可以升級為 Android 13,但如未符合類別 13 的要求,則可繼續回報自身的支援類別為 12。因此,您可以利用效能類別為裝置分類,而不用依靠特定的 Android 版本。

圖 1. 裝置可以升級 Android 版本,並繼續回報其支援最初支援的類別。

效能類別 14

效能類別 14 是以效能類別 13 中導入的要求為基礎。如需瞭解具體的效能類別要求,請參閱 Android CDD。除了對效能類別 13 的項目要求增加之外,CDD 還針對下列項目指定規定:

媒體

  • AV1 硬體解碼器的底片顆粒效果支援
  • AVIF 基準設定檔
  • AV1 編碼器效能
  • HDR 影片轉碼器
  • RGBA_1010102 顏色格式
  • YUV 紋理取樣
  • 影片編碼品質
  • 多頻道音訊混合

鏡頭

  • 夜間模式擴充功能
  • 具備 HDR 功能的主要相機
  • 臉部偵測場景模式

一般

  • 硬體重疊
  • HDR 螢幕

效能類別 13

效能類別 13 是以效能類別 12 中導入的要求為基礎。如需瞭解具體的效能類別要求,請參閱 Android CDD。除了對效能類別 12 的項目要求增加之外,CDD 還針對下列項目指定規定:

媒體

  • AV1 硬體解碼器
  • 安全的硬體解碼器
  • 解碼器初始化延遲時間
  • 往返音訊延遲時間
  • 有線耳機和 USB 音訊裝置
  • MIDI 裝置
  • 硬體支援的受信任執行環境

相機

  • 預覽防震功能
  • 慢動作錄影功能
  • 超廣角鏡頭的最小縮放比例
  • 並行鏡頭
  • 邏輯多鏡頭
  • 串流用途

效能類別 12

效能類別 12 著重於媒體用途。如需瞭解具體的效能類別要求,請參閱 Android CDD。CDD 會明定以下各領域的要求:

媒體

  • 並行影片轉碼器工作階段
  • 編碼器初始化延遲時間
  • 解碼器影格遺失
  • 編碼品質

鏡頭

  • 解析度和畫面更新率
  • 啟動及擷取延遲時間
  • FULL 或更高硬體級別
  • 時間戳記來源為即時資料
  • RAW 功能

一般

  • 記憶體
  • 讀取及寫入效能
  • 螢幕解析度
  • 螢幕密度

效能類別 11

效能類別 11 內含效能類別 12 的部分要求,可讓開發人員在仍可正常使用的舊版裝置上提供專屬體驗。如需瞭解具體的效能類別要求,請參閱 Android CDD