借助设备定位,您可以更精细地控制将 App Bundle 的哪些部分分发到特定设备。例如,您可以确保仅将大型资源分发给 RAM 较大的设备,也可以将资源的不同版本分发给不同的设备。
您可以定位设备属性,例如:
- 设备型号
- 设备 RAM
- 系统功能
- 系统芯片(适用于 API 级别至少为 31 的设备)
必要步骤概览
如需启用设备定位,必须执行以下步骤:
- 在 XML 文件中定义设备组。
- 指定软件包的哪些部分应分发到哪些设备组。
- [可选] 在本地测试您的配置。
- 将您的软件包(包含 XML 文件)上传到 Google Play。
检查 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:此选择器中要包含的设备型号(每组最多 10,000 个 device_ids)。如果设备与列表中的任何 device_id 匹配,则满足此属性。
- build_brand:设备制造商
- build_device:设备型号代码
- excluded_device_ids:此选择器中要排除的设备型号(每组最多 10,000 个 device_ids)。如果设备与列表中的任何 device_id 都不匹配,则满足此属性。
- build_brand:设备制造商
- build_device:设备型号代码
required_system_features:设备想要包含在此选择器中所需要具备的功能。(每组最多 100 项功能)。设备需要具备此列表中的所有系统功能才能满足此属性。
系统功能参考
- name:系统功能
forbidden_system_features:设备想要包含在此选择器中就不能具备的功能(每组最多 100 项功能)。如果设备具备此列表中的任何系统功能,则不满足此属性。
系统功能参考
- name:系统功能
system-on-chip:要包含在此选择器中的系统芯片。设备需要具备此列表中的任何条状标签才能满足此属性。只能以API 级别至少为 31 的设备为目标平台来开发 SoC 系统。
- manufacturer:芯片级系统制造商
- model:片上系统型号
在单个选择器中包含多个属性会创建一个逻辑“与”条件,例如:
<config:device-selector ram-min-bytes="7000000000">
<config:included-device-id brand="google" device="flame"/>
</config:device-selector>
将创建如下条件:RAM 高于 7GB 并且是 Pixel 4 的所有设备。此条件也可写作如下方式:
如果您需要创建一个逻辑“或”条件,请在单个设备组中创建单独的选择器,例如:
<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 字段分别表示制造商和型号代码。
在 app bundle 中添加设备定位配置文件
将以下代码添加到主要模块的 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
是相对于主模块的配置文件路径。这样可确保您的配置文件与您的 app bundle 一起打包。
deviceGroup
子句可确保系统按设备组拆分从 bundle 生成的 APK。
Play Feature Delivery 设备定位
如需将设备定位与 Play Feature Delivery 搭配使用,请参阅条件分发文档。