如何处理应用内更新中的InstallException?

Arp*_*kla 6 android kotlin google-play in-app-update google-play-core

我正在尝试按照官方文档在我的 Android 应用程序中实现应用程序内更新。我使用内部测试轨道在 Play 商店上启动了我的应用程序的一个版本,然后是版本代码递增的另一个版本。

当我第一次打开应用程序时,它崩溃了,并出现以下异常:

Fatal Exception: com.google.android.play.core.install.InstallException: Install Error(-10): The app is not owned by any user on this device. An app is "owned" if it has been acquired from Play. (https://developer.android.com/reference/com/google/android/play/core/install/model/InstallErrorCode#ERROR_APP_NOT_OWNED)
       at com.google.android.play.core.appupdate.o.a(o.java:6)
       at com.google.android.play.core.internal.o.a(o.java:28)
       at com.google.android.play.core.internal.j.onTransact(j.java:20)
       at android.os.Binder.execTransactInternal(Binder.java:1166)
       at android.os.Binder.execTransact(Binder.java:1130)
Run Code Online (Sandbox Code Playgroud)

但是当我重新打开应用程序时,更新流程正常启动。因此,也许 PlayCore 库第一次无法获取正确的数据,并且抛出了InstallException.

我想要的是捕获所有此类InstallException,但我无法找到放置 try-catch 块的确切位置。哪个函数AppUpdateManager抛出这个InstallException?是startUpdateFlow()方法吗?

我的代码:

private lateinit var updateInfo: AppUpdateInfo

suspend fun checkForUpdate() {
    updateInfo = appUpdateManager.requestAppUpdateInfo() // suspend function from play-core-ktx
    if(updateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && updateInfo.isImmediateUpdateAllowed) {
        startImmediateUpdate(activity)
    }
}

fun startImmediateUpdate(activity: Activity) {
    appUpdateManager.startUpdateFlow(   // Here I am using startUpdateFlow and not startUpdateFlowForResult
            updateInfo, activity, AppUpdateOptions.defaultOptions(AppUpdateType.IMMEDIATE)
    ).addOnSuccessListener { result ->
        if (result == Activity.RESULT_CANCELED) {
            activity.finish()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)