JobCancellationException with in-app updates

426 Views Asked by At

I've noticed that some of the users have issues to use flexible in-app update, the JobCancellationException: Job was cancelled is thrown with incomprehensible stack trace:

at dalvik.system.VMStack.getThreadStackTrace(VMStack.java)
at java.lang.Thread.getStackTrace(Thread.java:1538)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1068)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1063)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1955)

Unfortunately, I don't which part of the code is causing the issue. This is the only coroutine related code, staying in MyViewModel:

init {
    viewModelScope.launch {
        try {
            appUpdateManager.requestUpdateFlow().collect { appUpdateResult ->
                // Do something with result.
            }
        } catch (e: InstallException) {
            // Do something with an error.
        }
    }
}

fun requestUpdate(fragment: Fragment) {
    viewModelScope.launch {
        try {
            val appUpdateInfo = appUpdateManager.requestAppUpdateInfo()

            appUpdateManager.startUpdateFlowForResult(
                appUpdateInfo,
                AppUpdateType.FLEXIBLE,
                fragment,
                REQUEST_CODE
            )
        } catch (e: IntentSender.SendIntentException) {
        }
    }
}

I suspect that code inside requestUpdateFlow() is calling offer while the coroutine job is already cancelled and I can't see the exact stacktrace, because Play Core library is obfuscated?

I'm using following versions of the libraries:

"com.google.android.play:core:1.7.2"
"com.google.android.play:core-ktx:1.7.0"
1

There are 1 best solutions below

0
On

JobCancellationException: Job was cancelled is thrown in almost case is job in coroutine scope is cancelled.

Example: User go to a screen a in which call api to get something. But user press back to close this screen while api not complete. Thus, when receive response, job cancelled before -> exception.

To more handle JobCancellationException you can using suspendCancellableCoroutine.

More detail : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/suspend-cancellable-coroutine.html