In App Billing

In the last post I wrote about how I have been adding In App Billing to my game. During the Christmas I had enough time to finish and test the implementation. Thanks to my IAP testers as well!

Adding the In App Billing can done in few simple steps. You need to do the following (at least):

  1. You need to have Google Wallet merchant account
  2. Download Google play billing library with SDK Manager
  3. Add the application to the Developer Console
  4. Implement the workflow
  5. Test the application

A complete tutorial can be found here.

Unfortunately the example code provided by Google isn’t very good. It contains some bugs, some serious ones too, so you really need to know your workflow well and understand the example. The util classes you are going to import (if following the Google tutorial) as part of your app contain class named IabHelper. This class has a variable boolean mAsyncInProgress which is a flag to allow one Async call per time by the helper. This flag is modified by methods flagStartAsync and flagEndAsync which are package-private scope.

Well the class doesn’t do very good job since sometimes you get IllegalStateException when using the IabHelper. (Extra note: I personally don’t like this type of naming of the variables with scope because it prevents the use of code generation with modern IDEs like Eclipse).

You probably can find related articles and posts in different forums, like SO (StackOverflow) and some of them guide you to change the flagStartAsync and/or flagEndAsync to public and verify manually each time by calling that method so it will not throw an exception. Some say wrap your code with try catch blocks to prevent it.

Well, I think the code author wanted the flag to be handled internally and that’s the reason for package-private visibility. Surrounding the code with try catch is bad idea too because you are really not handling the real cause, rather just the symptom.

My solution was to tweak the code so that I appropriately call flagEndAsync in correct places of the code (IabHelper). You need to check this with your own flow but this is problem many other have found as well. One issue regarding the IabHelper is the fact that it throws NullPointerException if used in Emulator. In startSetup method:

replace:

if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {

with :

List queryIntentServices = mContext.getPackageManager().queryIntentServices(serviceIntent, 0);
if (queryIntentServices != null && !queryIntentServices.isEmpty()) {

This is probably because the Emulator packageManager cannot find the InAppBillingService intent and the returned list is null. This works without the check in real device if you have followed the tutorial and implemented it correctly.

Testing the purchase flow is whole another story. I will not get down to that now.

This is how my game currently looks. There’s few less buttons than before and few new ones. “Get hints” -button initiates the Google In App Billing flow and “Use Hint” -button uses a Hint if you have them left. For UI consistency I changed the “Exit” -button to similar “Quit” -button as in Main Screen and I removed “Next” -button which takes you to next level then you have completed the level. Now you just tap the screen to do so.

What do you think?

Image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s