How to handle InstallException in in-app updates?

Viewed 228

I am trying to implement in-app updates in my android app following the official documentation. I launched one version of my app on Play Store using internal testing track followed by another version with incremented versionCode.

When I opened the app first time it crashed with the following exception:

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)

But when I reopened the app, the update flow started properly. So maybe the PlayCore library wasn't able to fetch the right data first time and it threw the InstallException.

What I want is to catch all such InstallExceptions but I am not able to find where exactly to put the try-catch block. Which function of AppUpdateManager throws this InstallException? Is it the startUpdateFlow() method?

My code:

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()
        }
    }
}
0 Answers
Related