通过有条件交付,您可以为功能模块设置某些设备配置要求,以便在应用安装期间自动下载。例如,您可以配置一个包含增强现实 (AR) 功能的功能模块,使其仅在支持 AR 的设备上随应用安装时提供。
此交付机制目前支持根据以下设备配置在应用安装时控制模块下载:
如果设备不符合您指定的所有要求,则该模块不会在应用安装时下载。但是,您的应用稍后可以使用 Play Core SDK 按需请求下载该模块。
在开始之前,请确保您使用的是 Android Studio 3.5 或更高版本。以下部分向您展示了如何为功能模块添加有条件交付支持。
添加带有有条件交付选项的新模块
创建具有有条件交付功能的新模块最简单的方法是通过 New Module 向导,步骤如下:
- 要打开 New Module 对话框,请从菜单栏中选择 File > New > New Module。
- 在 New Module 对话框中,选择 Dynamic Feature Module,然后点击 Next。
- 像往常一样配置您的模块,然后点击 Next。
在 Module Download Options(模块下载选项)部分中,完成以下操作:
指定 Module title(模块标题),最多 50 个字符。平台使用此标题向用户标识模块,例如在确认用户是否要下载模块时。因此,您的应用的基础模块必须包含模块标题作为字符串资源,您可以对其进行翻译。使用 Android Studio 创建模块时,IDE 会自动将字符串资源添加到基础模块中,并在功能模块的清单中注入以下条目:
<dist:module ... dist:title="@string/feature_title"> </dist:module>
在 Install-time inclusion(安装时包含)下的下拉菜单中,选择 Only include module at app install for devices with specified features(仅在应用安装时包含指定功能的设备上的模块),这会创建一个模块,该模块仅在具有您可以指定的特定配置(例如设备功能或国家/地区)的设备上随应用安装时包含。Android Studio 会在模块的清单中注入以下内容以反映您的选择:
<dist:module ... > <dist:delivery> <dist:install-time> <dist:conditions> <!-- If you specify conditions, as described in the steps below, the IDE includes them here. --> </dist:conditions> </dist:install-time> </dist:delivery> </dist:module>
如果您想限制模块的自动下载到特定国家/地区或最低 API 级别,请点击 Finish 完成模块创建,然后阅读有关如何根据国家/地区或最低 API 级别指定条件的章节。否则,点击 + device feature 添加设备在安装时下载模块所需的功能。
在 device-feature 旁边,从下拉菜单中选择以下选项之一并指定其值:
- Name:允许您指定设备在安装时下载模块所需的硬件或软件功能。有条件交付支持的功能与
PackageManager
列出的FEATURE_*
常量相同。如果选择此选项,请在下拉菜单旁边的字段中开始键入功能常量值的任何部分(例如“bluetooth”),然后选择出现的建议之一。 - OpenGL ES Version:允许您指定设备在安装时下载模块所需的 OpenGL ES 版本。如果选择此选项,请在下拉菜单旁边的字段中开始键入版本(例如“0x00030001”),然后选择出现的建议之一。
- Name:允许您指定设备在安装时下载模块所需的硬件或软件功能。有条件交付支持的功能与
如果您想根据可用设备功能添加多个条件,请为要指定的每个设备功能条件点击 + device feature。
如果您希望此模块适用于运行 Android 4.4(API 级别 20)及更低版本的设备并包含在多 APK 中,请选中 Fusing 旁边的复选框。这意味着您可以为此模块启用按需行为并禁用合并,以将其从不支持下载和安装拆分 APK 的设备中排除。Android Studio 会在模块的清单中注入以下内容以反映您的选择:
<dist:module ...> <dist:fusing dist:include="true | false" /> </dist:module>
配置完模块下载选项后,点击 Finish。
请注意,Android Gradle 插件不支持从动态功能模块运行 lint。从相应的应用程序模块运行 lint 将在其动态功能模块上运行 lint,并将所有问题包含在应用的 lint 报告中。
为现有功能模块添加有条件交付选项
您可以通过模块的清单轻松地为现有功能模块添加有条件交付选项。但是,您应该首先阅读有关有条件交付选项与其他可能已启用的交付选项的兼容性。
首先,您需要将清单迁移到新的 <dist:delivery>
元素。以下代码片段显示了旧语法的示例:
<!-- This is the old syntax. -->
<dist:module
dist:title="@string/feature_title" dist:onDemand="true">
<dist:fusing dist:include="true"/>
</dist:module>
上述交付选项现在指定如下。
<dist:module
dist:title="@string/feature_title">
<dist:delivery>
<dist:on-demand/>
</dist:delivery>
<dist:fusing dist:include="true"/>
</dist:module>
然后,您可以根据设备功能包含有条件交付选项,如下所示。
<dist:module
dist:title="@string/feature_title">
<dist:delivery>
<dist:on-demand/>
<dist:install-time>
<dist:conditions>
<!-- Requires that the device support AR to download the module at
app install-time. -->
<dist:device-feature dist:name="android.hardware.camera.ar"/>
</dist:conditions>
</dist:install-time>
</dist:delivery>
<dist:fusing dist:include="true"/>
</dist:module>
以下部分讨论了有条件交付的其他选项,例如按国家/地区或最低 API 级别。
与其他模块下载选项的兼容性
由于功能模块提供了多种选项来配置每个功能如何交付到用户设备,因此了解有条件交付选项如何受其他设置影响非常重要。下表总结了有条件交付与其他模块下载选项的兼容性。
模块下载选项 | 与有条件交付的兼容性 |
---|---|
合并 (<dist:fusing dist:include="true"/> ) |
如果模块将此选项设置为 true,则 Google Play 在将您的应用部署到运行 API 级别 19 或更低版本的设备时,将不遵守您指定的有条件交付选项。也就是说,对于运行 API 级别 19 或更低版本的设备,启用合并的功能模块始终在安装时包含。 |
支持即时运行 (<dist:module dist:instant="true"/> ) |
有条件交付选项不支持支持即时运行的功能模块。 |
按需 (<dist:on-demand/> ) |
默认情况下,如果您指定了有条件交付选项,则该模块也支持按需提供。 |
根据国家/地区指定条件
有条件交付还允许您指定要排除(或包含)在应用安装时下载模块的国家/地区。指定此条件可能很有用,例如,如果您的模块实现了在某些地区不可用的支付方式。
在此上下文中,设备国家/地区通常由用户在 Google Play 帐号中注册的账单地址确定。
要为您的模块指定国家/地区,请在功能模块的清单中包含以下内容。
<dist:conditions>
<!-- Set to "true" to specify countries to exclude from downloading
this module at app install-time. By default, modules are available
for download to all user countries. -->
<dist:user-countries dist:exclude="true">
<!-- Specifies the two-letter CLDR country code for regions that should
not download the module at app install-time. -->
<dist:country dist:code="CN"/>
<dist:country dist:code="HK"/>
</dist:user-countries>
</dist:conditions>
指定 API 级别条件
如果功能模块依赖于仅在某些版本的 Android 平台中可用的 API,则根据设备的 API 级别指定条件可能很有用。
要根据最低或最高设备 API 级别设置条件,请在您的功能模块的清单中包含以下内容。
<dist:conditions> <!-- Specifies the minimum API level that the device must satisfy in order to download your module at app install-time. The API level you specify must be greater or equal to the module's own minSdkVersion. --> <dist:min-sdk dist:value="21"/> <!-- Specifies the maximum API level that the device cannot exceed in order to download your module at app install-time. The API level you specify must be less than or equal to the module's own maxSdkVersion. --> <dist:max-sdk dist:value="24"/> </dist:conditions>
指定其他设备属性的条件(Beta 版)
要指定其他设备属性的条件,例如型号名称、RAM、系统功能和系统级芯片,您可以使用设备定位配置文件。
要创建设备定位配置文件,请参阅设备定位文档。
创建配置文件后,您可以通过在功能模块的清单中包含以下内容来为模块指定设备组。
<dist:conditions>
<dist:device-groups>
<dist:device-group dist:name="myCustomGroup1"/>
<dist:device-group dist:name="myCustomGroup2"/>
</dist:device-groups>
</dist:conditions>