设备定向可让您更精细地控制应用包的哪些部分会分发到特定设备。例如,您可以确保大型资源仅分发到具有高 RAM 的设备,或者您可以将某个资源的不同版本分发到不同的设备。
您可以根据以下设备属性进行定向:
- 设备型号
- 设备 RAM
- 系统功能
- 片上系统(适用于 API 级别至少为 31 的设备)
所需步骤概览
以下是启用设备定向所需的步骤:
- 在设备定向配置文件中定义您的设备组。
- 指定您的应用包的哪些部分应分发到哪些设备组。
- 可选:在本地测试您的配置。
- 将您的应用包(包含配置文件)上传到 Google Play。
Android Gradle 插件与 Play Unity 插件
确切的所需步骤取决于您是使用 Android Gradle 插件还是 Play Unity 插件构建应用。在继续之前,请选择您的构建设置:
检查 Android Gradle 插件版本
要使用设备定向,请确保您的 Android Gradle 插件 (AGP) 版本至少为 8.10.0-alpha01。此版本随 Android Studio Meerkat 2 及更高版本提供。
在 Android Gradle 插件中启用设备定向
必须在 gradle.properties
文件中明确启用设备定向
android.experimental.enableDeviceTargetingConfigApi=true
创建设备定向配置 XML 文件
设备定向配置文件是一个 XML 文件,您可以在其中定义自定义设备组。例如,您可以定义一个名为 high_ram
的设备组,其中包含所有 RAM 至少为 8GB 的设备。
<config:device-targeting-config
xmlns:config="http://schemas.android.com/apk/config">
<config:device-group name="high_ram">
<config:device-selector ram-min-bytes="8000000000"/>
</config:device-group>
</config:device-targeting-config>
一个设备组由最多 5 个设备选择器组成。如果设备满足其任何设备选择器,则该设备会包含在设备组中。
一个设备选择器可以有一个或多个设备属性。如果设备匹配选择器的所有设备属性,则该设备会被选中。
可用设备属性
- device_ram:设备 RAM 要求
- min_bytes(包含):所需最小 RAM(以字节为单位)
- max_bytes(不包含):所需最大 RAM(以字节为单位)
- included_device_ids:要包含在此选择器中的设备型号(每个组最多 10000 个 device_id)。如果设备与列表中的任何 device_id 匹配,则满足此属性。
- build_brand:设备制造商
- build_device:设备型号代码
- excluded_device_ids:要从此选择器中排除的设备型号(每个组最多 10000 个 device_id)。如果设备与列表中的任何 device_id 均不匹配,则满足此属性。
- build_brand:设备制造商
- build_device:设备型号代码
required_system_features:设备必须具有才能被此选择器包含的功能(每个组最多 100 个功能)。设备必须具有此列表中的所有系统功能才能满足此属性。
系统功能参考
- name:系统功能
forbidden_system_features:设备不能具有才能被此选择器包含的功能(每个组最多 100 个功能)。如果设备具有此列表中的任何系统功能,则不满足此属性。
系统功能参考
- name:系统功能
system-on-chip:要包含在此选择器中的片上系统。设备必须具有此列表中的任何芯片才能满足此属性。片上系统只能在 API 级别至少为 31 的设备上进行定向。
在单个选择器中包含多个属性会创建逻辑 AND,例如:
<config:device-selector ram-min-bytes="7000000000">
<config:included-device-id brand="google" device="flame"/>
</config:device-selector>
将为所有 RAM > 7GB 且为 Pixel 4 的设备创建条件,也可写为如下形式:
如果您想要 OR 条件,请在单个设备组中创建单独的选择器,例如:
<config:device-selector ram-min-bytes="7000000000"/>
<config:device-selector>
<config:included-device-id brand="google" device="flame"/>
</config:device-selector>
将为所有 RAM > 7GB 或为 Pixel 4 的设备创建条件,也可写为如下形式:
以下是显示所有可能设备属性的示例:
<config:device-targeting-config
xmlns:config="http://schemas.android.com/apk/config">
<config:device-group name="myCustomGroup1">
<config:device-selector ram-min-bytes="8000000000">
<config:included-device-id brand="google" device="redfin"/>
<config:included-device-id brand="google" device="sailfish"/>
<config:included-device-id brand="good-brand"/>
<config:excluded-device-id brand="google" device="caiman"/>
<config:system-on-chip manufacturer="Sinclair" model="ZX80"/>
<config:system-on-chip manufacturer="Commodore" model="C64"/>
</config:device-selector>
<config:device-selector ram-min-bytes="16000000000"/>
</config:device-group>
<config:device-group name="myCustomGroup2">
<config:device-selector ram-min-bytes="4000000000" ram-max-bytes="8000000000">
<config:required-system-feature name="android.hardware.bluetooth"/>
<config:required-system-feature name="android.hardware.location"/>
<config:forbidden-system-feature name="android.hardware.camera"/>
<config:forbidden-system-feature name="mindcontrol.laser"/>
</config:device-selector>
</config:device-group>
</config:device-targeting-config>
官方设备制造商和设备型号代码
您可以通过 Google Play 管理中心中的设备目录查找设备制造商和型号代码的正确格式,具体方法有以下两种:
使用设备目录检查单个设备,并在如下示例所示的位置查找制造商和型号代码(对于 Google Pixel 4a,制造商是“Google”,型号代码是“sunfish”)
下载受支持设备的 CSV,并将“制造商”和“型号代码”分别用于 build_brand 和 build_device 字段。
在您的应用包中包含设备定向配置文件
将以下内容添加到主模块的build.gradle
文件中
android {
...
bundle {
deviceTargetingConfig = file('device_targeting_config.xml')
deviceGroup {
enableSplit = true // split bundle by #group
defaultGroup = "other" // group used for standalone APKs
}
}
...
}
device_targeting_config.xml
是您的配置文件相对于主模块的路径。这可确保您的配置文件与应用包一起打包。
`deviceGroup` 子句确保从您的应用包生成的 APK 会按设备组进行拆分。
适用于 Play 功能分发的设备定向
要将设备定向与 Play 功能分发结合使用,请参阅条件分发的文档。
适用于 Play Asset Delivery 的设备定向
要将设备定向与 Play Asset Delivery 结合使用,请参阅资源包文档。
适用于设备端 AI 模型分发的设备定向
要将设备定向与适用于设备端 AI 的 Play 结合使用,请参阅AI 包文档。
报告 bug
在公开的问题跟踪器上报告任何 bug。