29

I have setup In-App Billing for the first time using the new v3 API. It is working correctly on my devices but I have received a lot of error reports from other users.

One of them is:

java.lang.IllegalStateException: IAB helper is not set up. Can't perform operation: queryInventory
    at my.package.util.iab.IabHelper.checkSetupDone(IabHelper.java:673)
    at my.package.util.iab.IabHelper.queryInventory(IabHelper.java:462)
    at my.package.util.iab.IabHelper$2.run(IabHelper.java:521)
    at java.lang.Thread.run(Thread.java:1019)

And another one is:

java.lang.NullPointerException
    at my.package.activities.MainActivity$4.onIabSetupFinished(MainActivity.java:159)
    at my.package.util.iab.IabHelper$1.onServiceConnected(IabHelper.java:242)

My activity implementation follows Google's example code (all referenced classes are untouched from the example):

IabHelper mHelper;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //...

    mHelper = new IabHelper(this, IAB_PUBLIC_KEY);
    mHelper.enableDebugLogging(true);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                return;
            }

            // Hooray, IAB is fully set up. Now, let's get an inventory of
            // stuff we own.
            mHelper.queryInventoryAsync(mGotInventoryListener); //***(1)***
        }
    });
}

// Listener that's called when we finish querying the items we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
            Inventory inventory) {
        if (!result.isFailure()) {
            if (inventory.hasPurchase(SoundsGlobals.IAB_SKU_PREMIUM)){
                //we are premium, do things
            }
        }
        else{
            //oops
        }
    }
};

@Override
protected void onDestroy() {
    if (mHelper != null) {
        mHelper.dispose();
        mHelper = null;
    }
    super.onDestroy();
}

I assume that both errors originate from the line marked as ***(1)***

What is the cause of these errors? If I'm calling queryInventoryAsync only within onIabSetupFinished, how is it possible that mHelper is null, or that mHelper is not set up?

Does anyone know a solution to this?

1
  • 1
    Also having the same exception: IAB helper is not set up. Can't perform operation: queryInventory. Could be a bug in the Google Play Billing Library?
    – Wizche
    Jan 17, 2013 at 10:54

11 Answers 11

19

As @Martin explained, there was a bug in the Google In-App Billing example which caused this.

However, after fixing it, I was still receiving some errors in internal calls (queryInventory inside the thread created in queryInventoryAsync in some rare cases reports that the helper is not setup). I have solved this by adding an additional catch in that case:

try {
    inv = queryInventory(querySkuDetails, moreSkus);
}
catch (IabException ex) {
    result = ex.getResult();
}
catch(IllegalStateException ex){ //ADDED THIS CATCH
    result = new IabResult(BILLING_RESPONSE_RESULT_ERROR, "Helper is not setup.");
}

I also got a crash on mHelper.dispose() which I fixed in a similar way:

try{
    if (mContext != null) mContext.unbindService(mServiceConn);
}
catch(IllegalArgumentException ex){ //ADDED THIS CATCH
    //IGNORE IT - somehow, the service was already unregistered
}

Of course, instead of ignoring these errors you can silently log them to ACRA, for example :)

Thanks for all your comments.

3
  • 2
    Of course, it is bad practice to catch RuntimeExceptions like this in your code. The fact that this needs to be done here either means that there is a bug in your code, or a bug in Google's code. Looking at the IAB starter code, I wouldn't be surprised if it is the latter (and in fact, I can see several instances where race conditions could cause problems in the IABHelper class). Aug 2, 2014 at 16:46
  • The bug is that Helper throws where it never should. It owns the thread that throws, the exception should never leave the IabHelper. There is nothing a caller can do to prevent or handle it properly. Mar 21, 2016 at 10:47
  • Is this bug already fixed in the API google? I recently integrated the In app billing API and faced this issue in the production. I picked up the code from github.com/googlesamples/android-play-billing/tree/master/… as suggested in Android docs.
    – Swapnil
    Jun 17, 2017 at 5:55
14

There is a bug in IABHelper. The return line in the exception handler is missing, meaning it drops through and calls the success hanlder - however, mSetupDone has not been set, so further calls to the API fail. Add the return statement in as below - this will still fail, but the failure will be correctly reported to your app so you can take appropriate action.

                catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
                                                "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;  // This return line is missing
            }

            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
5
  • 2
    You are right. I'd like to note that it has already been fixed by Google.
    – Ereza
    Mar 9, 2013 at 13:12
  • 1
    @Ereza Fixed perhaps, but not yet available for download using the SDK manager as of the time & date of this comment. For now, I just inserted the return statement manually.
    – logray
    Mar 17, 2013 at 0:12
  • 1
    This isn't it. Or there is something else going on.The exception is thrown from a thread started by queryInventoryAsync(), after it checks the setup flag. Mar 21, 2016 at 10:22
  • I probably isn't it now, given that bug was fixed 3 years ago. Probably some new error that you're seeing.
    – Martin
    Mar 21, 2016 at 21:24
  • Do you have a link to the bug report?
    – ericn
    May 14, 2021 at 13:11
10

I believe that there are still two bugs in the Android code, which explains why you still see the error. Notice that the call stack is on a stand-alone thread. But the code that sets mSetupDone (IabHelper) to true executes on the main UI thread. Java does not guarantee that data changed by one thread will be visible to the other thread due to CPU caching unless you declare the variable with the volatile keyword. Thus it is possible that it was setup (mSetupDone == true), but that the new value of mSetupDone is cached on UI thread, not yet visible to this thread in your call stack. So that thread still sees mSetupDone == false.

I tried to fix this by declaring mSetupDone with volatile, and also every other non-final field of IabHelper just to be safe.

Now the other problem is the .dispose() function. It does not stop the ongoing threads. This means that it can set mSetupDone to false while one of the worker threads is running. If you look at queryInventoryAsync(), you will see it does check that mSetupDone is true. And based on your call stack, it did get past that. Then it crashed later with mSetupDone == false. Only way that could happen is if dispose() were called while your thread was in flight. Fix is that dispose() needs to signal threads to just silently bail out instead of continuing and throwing errors when it sees mSetupDone == false. This also prevents yet another problem with IabHelper where disposed instances call listener callbacks even after being disposed! It's a bit complicated to explain line by line here, but hopefully thus gets you pointed in the right direction.

6

I found out! It's about the version of the user's Google Play Store app. In-app billing V3 needs 3.9.16 or higher (http://developer.android.com/google/play/billing/versions.html). I used an older version and I received that error, now on 4.4.21 its ok!

2

Make sure you are implementing the IabHelper.han.handleActivityResult(requestCode, resultCode, data) method in your activities onActivityResult method.

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

// Pass on the activity result to the helper for handling
  if (!mIabHelper.handleActivityResult(requestCode, resultCode, data)) {
      // not handled, so handle it ourselves (here's where you'd
      // perform any handling of activity results not related to in-app
      // billing...
      super.onActivityResult(requestCode, resultCode, data);
  } else {
      Log.i(TAG, "onActivityResult handled by IABUtil.");
  }
}
2

I'm getting that EXACT same error with pretty-much the exact same code.

Only seems to be happening on certain handsets (actually, it seems almost exclusively the Acer Iconia Tablet in recent error reports!!) - and I am handling onActivityResult...

There are a number of errors in the Google v3 Billing Sample which can cause ANRs/FCs - I suspect this is just another one (shoddy code and shoddy docs are becoming a Google trademark - sadly).

My guess is - for now - that we need to allow for either mHelper or mGotInventoryListener being null and just disable In-App Billing in that case (as if result.isSuccess() was false, basically)

p.s. editted to add - it could just be that the user has an out-of-date version of the Play Store - that only auto-updates if they allow it to run!?

1

You can keep up to date with the inapp v3 API development at https://code.google.com/p/marketbilling/

The code there is newer than the one that is available through the Android SDK Manager.

2
  • I believe this does provide a solution to the author. He is using a old and buggy version of a library and so I direct him to use the most recent non-buggy version. Sep 8, 2014 at 7:14
  • 1
    Why is Google keeping the code in two places? Which one is master? And is it not better to deliver this in a git repository rather than with the SDK download?
    – powder366
    Sep 21, 2014 at 6:22
1

In addition to @DavidM and @Ereza.

Another major issue with the IabHelpr class is the poor choice of throwing RuntimeExcptions (IllegalStateException) in multiple methods. Throwing RuntimeExeptions from your own code in most cases is not desirable due to the fact that they are unchecked exceptions. That is like sabotaging your own application- if not caught, these exceptions will bubble up and crash your app.

The solution to this is to implement your own checked exception and change the IabHelper class to throw it, instead of the IllegalStateException. That will force you to handle this exception everywhere it could be thrown in your code at compile time.

Here is my custom exception:

public class MyIllegalStateException extends Exception {

    private static final long serialVersionUID = 1L;

    //Parameterless Constructor
    public MyIllegalStateException() {}

    //Constructor that accepts a message
    public MyIllegalStateException(String message)
    {
       super(message);
    }
}

Once we make the changes in the IabHelper class, we can handle our checked exception in our code where we call the class methods. For example:

try {
   setUpBilling(targetActivityInstance.allData.getAll());
} catch (MyIllegalStateException ex) {
    ex.printStackTrace();
}
1

If all above fail to help you, give it a go to try to analyse your code a little - is your IabHelper really set up at the time you're calling it?

I found myself doing it slightly wrong without realising it. A simple example of using it wrong in Activity.onCreate()

m_iabHelper = new IabHelper(this, base64EncodedPublicKey); // Declare

m_iabHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { // Setup
    public void onIabSetupFinished(IabResult result) {
        // Setup code
    }
}

// Don't do this, will produce an error
List additionalSkuList = new ArrayList(); 
additionalSkuList.add(SKU_MYSKU);
m_iabHelper.queryInventoryAsync(true, additionalSkuList, m_queryFinishedListener);
// Don't do this, will produce an error

.

Above will honour you with the "IAB helper is not set up" error since while your app tries to execute the m_iabHelper.queryInventoryAsync(), the IabHelper it is not set up yet. Consider using these functions in onIabSetupFinished() or somewhere after that function is called (eg. outside the onCreate())

0

I get the same errors. I also ran into other issues...

Having more than one google account on a device disabled in-app billing on my Galaxy Tab 7. Remove one account re-enabled it.

I've seen issues with the in app billing sample code on a GT-P5110, LGL75C, and GT-S5839i among others.

(I use the code in an application with ACRA installed... so everytime it crashes, I get info)

The devices Android version ranges from 2.3.3 to 4.0.4.

It's very annoying.

0

There were many problems with IABHelper.java.

Firstly - the version that is downloaded by the SDK Manager is not kept up to date. Use the version found here: https://code.google.com/p/marketbilling/source/detail?r=15946261ec9ae5f7c664d720f392f7787e3ee6c7 It is the most up to date version as of posting this answer. Many issues seem to have been fixed with this version compared to the initial release which comes from the SDK Manager.

Only thing I've had to change in this version is adding flagEndAsync(); after line 404 which fixes an IllegalStateException when 2 IAB purchase flows are launched in quick succession.

With this version you do not need to manage using flagEndAsync(); in your files and can leave the method to not be public.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.