Reputation: 1350
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?
Upvotes: 21
Views: 6383
Reputation: 3305
Here is the official example from Google: https://github.com/android/play-billing-samples/blob/main/TrivialDriveKotlin/app/src/main/java/com/sample/android/trivialdrivesample/billing/BillingDataSource.kt
They are trying to reconnect with exponential delay:
private const val RECONNECT_TIMER_START_MILLISECONDS = 1L * 1000L
private const val RECONNECT_TIMER_MAX_TIME_MILLISECONDS = 1000L * 60L * 15L // 15 minutes
...
override fun onBillingSetupFinished(billingResult: BillingResult) {
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
// The billing client is ready. You can query purchases here.
// This doesn't mean that your app is set up correctly in the console -- it just
// means that you have a connection to the Billing service.
reconnectMilliseconds = RECONNECT_TIMER_START_MILLISECONDS
defaultScope.launch {
querySkuDetailsAsync()
refreshPurchases()
}
}
else -> retryBillingServiceConnectionWithExponentialBackoff()
}
}
...
/**
* This is a pretty unusual occurrence. It happens primarily if the Google Play Store
* self-upgrades or is force closed.
*/
override fun onBillingServiceDisconnected() {
retryBillingServiceConnectionWithExponentialBackoff()
}
...
/**
* Retries the billing service connection with exponential backoff, maxing out at the time
* specified by RECONNECT_TIMER_MAX_TIME_MILLISECONDS.
*/
private fun retryBillingServiceConnectionWithExponentialBackoff() {
handler.postDelayed(
{ billingClient.startConnection(this@BillingDataSource) },
reconnectMilliseconds
)
reconnectMilliseconds = min(
reconnectMilliseconds * 2,
RECONNECT_TIMER_MAX_TIME_MILLISECONDS
)
}
Upvotes: 5
Reputation: 7493
You can handle onBillingServiceDisconnected()
menthod like this way by implement a Retry logic:
private static long reconnectMilliseconds = 1000;
@Override
public void onBillingServiceDisconnected() {
retryBillingServiceConnection();
}
private void retryBillingServiceConnection() {
new Handler().postDelayed(() ->
connectToBillingService(),
reconnectMilliseconds);
reconnectMilliseconds = reconnectMilliseconds * 2;
}
Upvotes: 1
Reputation: 833
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.
Upvotes: 3
Reputation: 1350
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 initiliazedBilligClient
is ready
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.
Upvotes: 10