Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In App Billing v3 IllegalArgumentException using IabHelper

I've had in app billing v3 implemented in my app for about a week now. I used a lot of android's sample code to simplify the integration. I've been logging a crash fairly often that I can't seem to reproduce:

Exception Type: java.lang.RuntimeException
Reason: Unable to destroy activity    {[package].billing.BillingActivity}: java.lang.IllegalArgumentException: Service not registered: [package].billing.util.IabHelper$1@40646a70

It seems to be breaking on this line:

if (mContext != null) mContext.unbindService(mServiceConn);

I'm binding this service in my onCreate method and disposing it in my onDestroy method (which is where this error is logged). Any pointers?

like image 881
jbenowitz Avatar asked Apr 25 '13 18:04

jbenowitz


3 Answers

I checked out the latest version of the sample project and up to today my recommendation is to currently to NOT use IabHelper. It is massively flawed.

To give you an idea:

1.) the async methods of IabHelper start a new thread. If IabHelper.dispose() is called while a thread is running you will always get various exceptions you cannot even handle.

2.) If the connection to the billing service goes down, they set it to null. But apart from that they never check if mService is null before accessing the methods. So it will always crash with NullPointerException in this case.

public void onServiceDisconnected(ComponentName name) {
                logDebug("Billing service disconnected.");
                mService = null;

and this is just the tip of the ice berg. Seriously I do not understand how somebody can publish this as reference code.

like image 129
tmanthey Avatar answered Nov 15 '22 08:11

tmanthey


You could replace the line you mentioned:

if (mContext != null) mContext.unbindService(mServiceConn);

by this line

if (mContext != null && mService != null) mContext.unbindService(mServiceConn);

This should do the trick

like image 24
sam Avatar answered Nov 15 '22 07:11

sam


I just encountered the same issue but on android emulator. Billing v3 requires that Google Play app should be launched at least once and since the emulator lack of Google Play app it cannot set up helper and cannot dispose it in onDestroy().

My personal workaround is just skipping that error in try/catch:

@Override
protected void onDestroy() {
    super.onDestroy();

    if (bHelper != null){
        try {
            bHelper.dispose();
        }catch (IllegalArgumentException ex){
            ex.printStackTrace();
        }finally{}
    }
    bHelper = null;
}

Add this in every onDestroy() where you dispose helper. Works fine for me.

like image 6
Allesad Avatar answered Nov 15 '22 07:11

Allesad