android检测本地是否安装,在本地测试模块的安装
Play 核心庫可讓您在本地測試應用是否能夠執行以下操作,而無需連接到 Play 商店:
請求并監控模塊的安裝。
處理安裝錯誤。
本頁介紹了如何將應用的拆分 APK 部署到測試設備,以便 Play 核心自動使用這些 APK 模擬從 Play 商店請求、下載和安裝模塊。
雖然您不需要對應用的邏輯進行任何更改,但需要滿足以下要求:
下載并安裝最新版 bundletool。您需要使用 bundletool 從 app bundle 構建一組新的可安裝 APK。
確保您的應用使用的是 Play 核心 1.7.2 或更高版本。如需了解詳情,請參閱在您的項目中添加 Play 核心庫。
注意:Play 核心庫目前不支持模塊卸載或推遲安裝。如需測試此功能,不妨考慮使用內部應用分享。
構建一組 APK
您應構建應用的拆分 APK(如果您尚未執行此操作),具體操作步驟如下:
使用以下某種方法為您的應用構建 app bundle:
使用 Android Studio 和 Android Plugin for Gradle 構建 Android App Bundle 并為其簽名。
使用 bundletool 針對所有設備配置生成一組 APK,具體命令如下:
bundletool build-apks --local-testing
--bundle my_app.aab
--output my_app.apks
--local-testing 標記包含 APK 清單中的元數據,告知 Play Core 庫使用本地拆分 APK 測試功能模塊的安裝,而無需連接到 Play 商店。
將應用部署到設備
使用 --local-testing 標記構建一組 APK 后,應使用 bundletool 安裝應用的基礎版本,并將其他 APK 轉移到設備的本地存儲空間。您可以使用以下命令執行這兩項操作:
bundletool install-apks --apks my_app.apks
現在,當您啟動應用并完成下載和安裝功能模塊的用戶流時,Play 核心庫會使用 bundletool 轉移到設備本地存儲空間的 APK。
模擬網絡連接錯誤
為了模擬從 Play 商店安裝模塊的過程,Play 核心庫使用 SplitInstallManager 的替代方法(名為 FakeSplitInstallManager)來請求模塊。當您將 bundletool 與 --local-testing 標記一起使用來構建一組 APK 并將其部署到測試設備時,它包含元數據,指示 Play 核心庫自動切換應用的 API 調用以調用 FakeSplitInstallManager,而不是 SplitInstallManager。
FakeSplitInstallManager 包含一個布爾標記,當應用下次請求安裝模塊時,您可以啟用該標記來模擬網絡連接錯誤。如需在測試中訪問 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);
總結
以上是生活随笔為你收集整理的android检测本地是否安装,在本地测试模块的安装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海欢乐谷成人两次入园票什么意思
- 下一篇: 这是张国荣的哪部电影