5

I've been struggling with Android IAB v3 for a while now. I have it working consistently on my test devices. However, my crash reporting service shows two recurring crashes: one for null pointer exception and one for illegal state exception. I've tried updating the Google sample code w/ numerous suggestions from Stackoverflow users facing similar problems. I've gone through the "read crash reports, do research, attempt to fix crashes, submit update, see same crash reports again" cycle a few times. I think it's time for a different approach.

If you were starting with Android IAB today, what would you choose as the most up-to-date, correct resource for code samples, fixes, documentation, etc?

This doesn't have to be a single resource. Any combination of sample code, patches from SO posts, blog posts, or even "update with Android SDK Manager" will be helpful. Ideally, we can create a resource for folks new to IAB that prevents them from the headache and struggle of trying to integrate the service.


edit 1: More info on crashes

java.lang.IllegalStateException: Can't start async operation (refresh inventory) because another async operation(launchPurchaseFlow) is in progress.
   at com.android.vending.billing.IabHelper.flagStartAsync(IabHelper.java:832)
   at com.android.vending.billing.IabHelper.queryInventoryAsync(IabHelper.java:623)
   at com.android.vending.billing.IabHelper.queryInventoryAsync(IabHelper.java:651)
   ...


java.lang.NullPointerException
   at com.android.vending.billing.IabHelper.launchPurchaseFlow(IabHelper.java:398)
   at com.android.vending.billing.IabHelper.launchPurchaseFlow(IabHelper.java:350)
   ...

Those are the two exceptions. They don't always happen in the same places in IabHelper. I could probably just patch them, but that doesn't seem like the right way to solve this problem. Also, it doesn't help anyone else.

It's possible that I'm using out-dated sample code form Google. However, I've searched quite a bit and couldn't find anything more recent.

SundayMonday
  • 19,147
  • 29
  • 100
  • 154
  • 1
    I definitely second your idea. It would be also great to have more informations about those two crashes you mentioned above. – a.bertucci Oct 05 '14 at 23:54
  • 1
    SO is not the place for such question, it is [off topic](http://stackoverflow.com/help/on-topic) and should be asked somewhere else. It surely is a good idea to have such a resource, it is just not the place. – MByD Oct 05 '14 at 23:56
  • 1
    @MByD Stackoverflow is frequented by many developers having trouble with Anroid IAB v3. Hence, I think it's the ideal place for this question. – SundayMonday Oct 06 '14 at 00:18
  • @MByD are not wiki-style questions supported anymore in SO? – a.bertucci Oct 06 '14 at 08:59
  • @MByD suggestions for a better site? – SundayMonday Oct 06 '14 at 15:06
  • Maybe xda-developers, or android enthusiasts (although I'm not familiar with their rules..) – MByD Oct 06 '14 at 15:17
  • 1
    I would try two things. 1st illegalstate exeception might be cause by duplicate requests and a simple static boolean isRefreshInventoryProcessing would fix it. 2nd check interweb is available before refreshing inventory. Because your testing is done on rock solid internet and my experience tells me having a single request at a time in a google api is the responsibility of the developer. Good Luck inapp purchase is a //TODO for so I'm researching it. – danny117 Oct 06 '14 at 15:29

4 Answers4

6

Hey I'm also working on InApp Purchase since 10 days and I've successfully integrated in my existing app and ready to make it live. Initially when i had started doing this I've downloaded google InApp Billing Example called "Trivial Drive" from here.

But it didn't help me much as it has lots of issues and bugs, So I've decided do it on my own from scratch using new v3 api which you can find here. This tutorial has clear explanation that would help you and also if you have time see this youtube video where google employee had explained clearly how to integrate it.

Also if you want quick example, I've a sample app which you can download from here. Feel free to ask if you have any questions.

Ramesh
  • 1,252
  • 3
  • 12
  • 30
2

The first IllegalStateException is occurring because the previously launched operation using IAB Helper is yet to complete.

Probable causes:

  1. Forgot to call mHelper.dispose() in onDestroy() of activity.
  2. If you are launching purchase on button click rapidly double tapping the button on some device will produce the crash.

In your case you are trying to query inventory when already a launchPurchase is progress.

Solution: The status of async operation is reflected in variable mAsyncInProgress in IabHelper. You will have to change the scope of the variable to public, it package by default. You can then take either of the two approaches after querying the variable:

Make sure you have called mHelper.dispose() in onDestroy() of activity.

  1. cancel the current operation and start the new operation ignore the

or

  1. current request if any operation is running.

The exception is thrown in flagStartAsync function.

To figure our the root cause of the NullPointerException you will have to provide the code for launchPurchasFlow with line numbers.

Other precautions you should take: To avoid "IAB helper is not set up. Can't perform operation:launchPurchaseFlow which is caused when you call launchPurchaseFlow before IabHelper setup is complete.

You can disable the buy button by default. Enable the button on onIabSetupFinished callback of the IabHelper. Therefore the button will work only when the IabHelper setup is complete.

Anirudha Agashe
  • 3,510
  • 2
  • 32
  • 47
1

You are probably using async operations. The current IabHelper is not safe in case you use the ...async methods. The problem is that in any moment an async operation is running dispose can be called on the main thread. In this case you will get NullPointerExceptions and IllegalStateExceptions.

Try with this clone studiozanandroid

With ref this Ans , download the patch here to avoid async errors.

other than this checkout the signature validation issues like in this pay me lib,try to use OpenIAB libs.

Community
  • 1
  • 1
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
-1

Whatever you do DO NOT USE IabHelper. This is a leftover from earlier versions of in app billing. This is not needed with version 3 as it introduces another layer and is buggy.

Just go ahead and implement billing as shown in official docs - implementing in app billing.

V3 of the API basically contains only three methods and is very straightforward to implement. If you are serious about in-app-billing then you have to understand how this process works anyway, no library or helper is going to help you with that.

Okas
  • 2,664
  • 1
  • 19
  • 26
  • 2
    The official docs you are referring are also using the IabHelper... You can check here: http://developer.android.com/training/in-app-billing/preparing-iab-app.html – Yoann Hercouet Jun 10 '15 at 19:52