android检测本地是否安装,在本地测试模块的安装
Play 核心庫可讓您在本地測試應(yīng)用是否能夠執(zhí)行以下操作,而無需連接到 Play 商店:
請求并監(jiān)控模塊的安裝。
處理安裝錯誤。
本頁介紹了如何將應(yīng)用的拆分 APK 部署到測試設(shè)備,以便 Play 核心自動使用這些 APK 模擬從 Play 商店請求、下載和安裝模塊。
雖然您不需要對應(yīng)用的邏輯進(jìn)行任何更改,但需要滿足以下要求:
下載并安裝最新版 bundletool。您需要使用 bundletool 從 app bundle 構(gòu)建一組新的可安裝 APK。
確保您的應(yīng)用使用的是 Play 核心 1.7.2 或更高版本。如需了解詳情,請參閱在您的項目中添加 Play 核心庫。
注意:Play 核心庫目前不支持模塊卸載或推遲安裝。如需測試此功能,不妨考慮使用內(nèi)部應(yīng)用分享。
構(gòu)建一組 APK
您應(yīng)構(gòu)建應(yīng)用的拆分 APK(如果您尚未執(zhí)行此操作),具體操作步驟如下:
使用以下某種方法為您的應(yīng)用構(gòu)建 app bundle:
使用 Android Studio 和 Android Plugin for Gradle 構(gòu)建 Android App Bundle 并為其簽名。
使用 bundletool 針對所有設(shè)備配置生成一組 APK,具體命令如下:
bundletool build-apks --local-testing
--bundle my_app.aab
--output my_app.apks
--local-testing 標(biāo)記包含 APK 清單中的元數(shù)據(jù),告知 Play Core 庫使用本地拆分 APK 測試功能模塊的安裝,而無需連接到 Play 商店。
將應(yīng)用部署到設(shè)備
使用 --local-testing 標(biāo)記構(gòu)建一組 APK 后,應(yīng)使用 bundletool 安裝應(yīng)用的基礎(chǔ)版本,并將其他 APK 轉(zhuǎn)移到設(shè)備的本地存儲空間。您可以使用以下命令執(zhí)行這兩項操作:
bundletool install-apks --apks my_app.apks
現(xiàn)在,當(dāng)您啟動應(yīng)用并完成下載和安裝功能模塊的用戶流時,Play 核心庫會使用 bundletool 轉(zhuǎn)移到設(shè)備本地存儲空間的 APK。
模擬網(wǎng)絡(luò)連接錯誤
為了模擬從 Play 商店安裝模塊的過程,Play 核心庫使用 SplitInstallManager 的替代方法(名為 FakeSplitInstallManager)來請求模塊。當(dāng)您將 bundletool 與 --local-testing 標(biāo)記一起使用來構(gòu)建一組 APK 并將其部署到測試設(shè)備時,它包含元數(shù)據(jù),指示 Play 核心庫自動切換應(yīng)用的 API 調(diào)用以調(diào)用 FakeSplitInstallManager,而不是 SplitInstallManager。
FakeSplitInstallManager 包含一個布爾標(biāo)記,當(dāng)應(yīng)用下次請求安裝模塊時,您可以啟用該標(biāo)記來模擬網(wǎng)絡(luò)連接錯誤。如需在測試中訪問 FakeSplitInstallManager,您可以使用 FakeSplitInstallManagerFactory 獲取它的實例,如下所示:
Kotlin
// Creates an instance of FakeSplitInstallManager with the app's context.
val fakeSplitInstallManager = FakeSplitInstallManagerFactory.create(context)
// Tells Play Core Library to force the next module request to
// result in a network error.
fakeSplitInstallManager.setShouldNetworkError(true)Java
// Creates an instance of FakeSplitInstallManager with the app's context.
FakeSplitInstallManager fakeSplitInstallManager =
FakeSplitInstallManagerFactory.create(context);
// Tells Play Core Library to force the next module request to
// result in a network error.
fakeSplitInstallManager.setShouldNetworkError(true);
總結(jié)
以上是生活随笔為你收集整理的android检测本地是否安装,在本地测试模块的安装的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海欢乐谷成人两次入园票什么意思
- 下一篇: 这是张国荣的哪部电影