性能等级

性能等级是 Android 12 中首次引入的标准。性能等级定义超出 Android 基准要求的一组设备功能。

每个 Android 版本都有自己对应的性能等级,这在相应版本的 Android 兼容性定义文档 (CDD) 中进行了定义。Android 兼容性测试套件 (CTS) 会验证 CDD 要求。

每部 Android 设备都会声明其支持的性能等级。开发者可以在运行时检查设备的性能等级,并提供充分利用设备功能的升级体验。

如需了解设备的性能等级,请使用 Jetpack Core Performance 库。此库会根据 build 版本信息中的声明或来自 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

  • 注意:当 JavaScript 处于关闭状态时,系统会显示链接文字
  • 应用启动时间