Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle BillingClient.onBillingServiceDisconnected()?

Recently I migrated one of my apps to Google Play In-App Billing v3. Since the release I get some crash reports on Samsung devices only, which are all related to BillingClient.onBillingServiceDisconnected() being called.

Current code looks like this:

val billingClient = BillingClient.newBuilder(context)
            .setListener(updatedListener)
            .enablePendingPurchases()
            .build()

billingClient.startConnection(
            object : BillingClientStateListener {
                override fun onBillingSetupFinished(billingResult: BillingResult) {
                    if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                        // The billing client is ready. You can query purchases here.
                        querySkuDetails()
                    }
                }

                override fun onBillingServiceDisconnected() {
                    // Try to restart the connection on the next request to
                    // Google Play by calling the startConnection() method.
                    initBilling() // all code here is wrapped in this method
                }
            }
)

where I obviously re-initialize the BillingClient and call startConnection() again in error case. The crash then is

java.lang.IllegalStateException: 
  at android.os.Parcel.createException (Parcel.java:2096)
  at android.os.Parcel.readException (Parcel.java:2056)
  at android.os.Parcel.readException (Parcel.java:2004)
  at android.app.IActivityManager$Stub$Proxy.registerReceiver (IActivityManager.java:5557)
  at android.app.ContextImpl.registerReceiverInternal (ContextImpl.java:1589)
  at android.app.ContextImpl.registerReceiver (ContextImpl.java:1550)
  at android.app.ContextImpl.registerReceiver (ContextImpl.java:1538)
  at android.content.ContextWrapper.registerReceiver (ContextWrapper.java:641)
  at com.android.billingclient.api.zze.zza (zze.java:5)
  at com.android.billingclient.api.zzd.zza (zzd.java:5)
  at com.android.billingclient.api.BillingClientImpl.startConnection (BillingClientImpl.java:58)
  at de.memorian.gzg.presentation.base.IAPHelper.initBilling (IAPHelper.java:40)
  at de.memorian.gzg.presentation.base.IAPHelper$initBilling$1.onBillingServiceDisconnected (IAPHelper.java:53)
  at com.android.billingclient.api.BillingClientImpl$zza.onServiceDisconnected (BillingClientImpl.java:11)
  at android.app.LoadedApk$ServiceDispatcher.doConnected (LoadedApk.java:2060)
  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run (LoadedApk.java:2099)
  at android.os.Handler.handleCallback (Handler.java:883)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:7857)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1076)
Caused by: android.os.RemoteException: 
  at com.android.server.am.ActivityManagerService.registerReceiver (ActivityManagerService.java:16726)
  at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:2250)
  at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:3357)
  at android.os.Binder.execTransactInternal (Binder.java:1021)
  at android.os.Binder.execTransact (Binder.java:994)

I was wondering what I'm doing wrong within onBillingServiceDisconnected(), so I googled some time and didn't find any clear advise but // implement your own retry logic. That's e.g. what Google says. What exactly is the retry logic here? As you see in the stacktrace calling startConnection() again, as suggested by Google's comment, leads to the crash. Here Google says that I should ignore it since Play Services will call onBillingSetupFinished() eventually, later.

How do you handle this case?

like image 522
Syex Avatar asked Aug 08 '20 07:08

Syex


2 Answers

Didn't find a concrete answer to my question how to handle the failure case. I refactored my code so I basically ignore a call to onBillingServiceDisconnected() and only show an error message to the user.

Each call to attempting to make a purchase now checks if

  • BillingClient is initiliazed
  • BilligClient is ready
  • Sku details are not empty

And only after these succeed try to make the purchase.

Previously I did all of above on app init once. If connection fails, now, I will simply retry when the user clicks on the purchase item again (with a try catch). This maybe doesn't fix the crash issue but at least gives the user a better experience and control.

like image 168
Syex Avatar answered Sep 22 '22 09:09

Syex


I've also been receiving a similar stack trace in my Google Play Dashboard, however not as a crash but an ANR. The way I solved it was by moving the call to reinitialize the billing to a background thread.

Note that onBillingServiceDisconnected will be called when there's a connection, but it gets lost. You can test it by clearing Google Play's data while your app is open. If you don't retry at this point the connection will be lost.

While onBillingSetupFinished with an error code will be called when you are trying to connect, but it failed. No connection existed beforehand. Confusingly enough you should also retry here depending on the error code.

like image 30
RodXander Avatar answered Sep 22 '22 09:09

RodXander