在您的项目中添加 Play 核心库
在开始使用 Play 核心库之前,您需要先将其作为 Gradle 依赖项导入应用模块,如下所示:
// In your app’s build.gradle file: ... dependencies { // This dependency is downloaded from the Google’s Maven repository. // So, make sure you also include that repository in your project's build.gradle file. implementation 'com.google.android.play:core:1.6.4' ... }
First, open app/build.gradle and add the following inside the android {} block:
bundle { language { enableSplit = true } density { enableSplit = true } abi { enableSplit = true } }
This ensures that language, density, and abi configuration splits are all enabled.
请求按需模块
当您的应用需要使用动态功能模块时,它可以通过 SplitInstallManager
类在前台进行请求。在发起请求时,您的应用需要指定由目标模块清单中的 split
元素所定义的模块名称。当您使用 Android Studio 创建动态功能模块时,编译系统会使用您提供的模块名称,在编译时将该属性注入模块清单中。如需了解详情,请参阅动态功能模块清单。
例如,假设某个具有按需模块的应用可使用设备的相机拍摄和发送图片消息,并且此按需模块在其清单中指定了 split="pictureMessages"
。以下示例使用 SplitInstallManager
来请求 pictureMessages
模块(以及用于一些宣传过滤器的其他模块):
// Creates an instance of SplitInstallManager. val splitInstallManager = SplitInstallManagerFactory.create(context) // Creates a request to install a module. val request = SplitInstallRequest .newBuilder() // You can download multiple on demand modules per // request by invoking the following method for each // module you want to install. .addModule("pictureMessages") .addModule("promotionalFilters") .build() splitInstallManager // Submits the request to install the module through the // asynchronous startInstall() task. Your app needs to be // in the foreground to submit the request. .startInstall(request) // You should also be able to gracefully handle // request state changes and errors. To learn more, go to // the section about how to Monitor the request state. .addOnSuccessListener { sessionId -> ... } .addOnFailureListener { exception -> ... }
当您的应用请求按需模块时,Play 核心库会采用“即发即弃”策略。也就是说,它会发送请求以将该模块下载到平台,但不会监控安装是否成功。要在安装后继续用户操作流程或妥善处理错误,请务必监控请求状态。
注意:您可以请求已安装在设备上的动态功能模块。如果检测到该模块已安装,则 API 会立即将该请求视为已完成。此外,安装模块后,Google Play 会自动使其保持最新状态。也就是说,当您上传新版 App Bundle 时,平台会更新所有属于您应用的已安装 APK。如需了解详情,请参阅管理应用更新。
要立即访问模块的代码和资源,您的应用需要启用 SplitCompat。请注意,Android 免安装应用不需要使用 SplitCompat,因为他们可以立即访问功能模块。
延迟安装按需模块
如果您不需要应用立即下载并安装按需模块,可以延迟到应用在后台运行时再安装该模块。例如,您想要预先加载一些宣传材料并在之后启动应用时使用这些材料。
您可以使用 deferredInstall()
方法指定之后要下载的模块,如下所示。而且,与 SplitInstallManager.startInstall()
不同,您的应用无需在前台就可发起延迟安装请求。
// Requests an on demand module to be downloaded when the app enters // the background. You can specify more than one module at a time. splitInstallManager.deferredInstall(listOf("promotionalFilters"))
收到延迟安装请求后,系统将尽力而为,您无法跟踪其进度。因此,在尝试访问您已指定为延迟安装的模块之前,请检查该模块是否已安装。如果您需要立即使用该模块,请改为使用 SplitInstallManager.startInstall()
进行请求,如上一部分中所示。
监控请求状态
为了能够更新进度条,在安装后触发 Intent 或者妥善处理请求错误,您需要监听来自异步 SplitInstallManager.startInstall()
任务的状态更新。要开始接收安装请求更新,请先注册监听器并获取该请求的会话 ID,如下所示。
// Initializes a variable to later track the session ID for a given request. var mySessionId = 0 // Creates a listener for request status updates. val listener = SplitInstallStateUpdatedListener { state -> if (state.sessionId() == mySessionId) { // Read the status of the request to handle the state update. } } // Registers the listener. splitInstallManager.registerListener(listener) ... splitInstallManager .startInstall(request) // When the platform accepts your request to download // an on demand module, it binds it to the following session ID. // You use this ID to track further status updates for the request. .addOnSuccessListener { sessionId -> mySessionId = sessionId } // You should also add the following listener to handle any errors // processing the request. .addOnFailureListener { exception -> // Handle request errors. } // When your app no longer requires further