Integrate In App billing, just 3 lines of code in your app

Self Advertisement 🙂

Self Advertisement
—–Start of Advertisement——-
BUILD CAR POOL SOLUTIONS ON ANY DEVICE TO RUN ANYWHERE ( Introducing mCruiseOn, the java library /json api’s that you can use to build a car pool solution. Be the next,, mCruiseOn is your one stop API on EC2.
——End of Advertisement——-

The android in app billing documentation and implementation is a night mare. It takes more time then it should. It can take time to understand this complex piece of technology. So for those smart people around, who have less time to go through all that documentation, here goes.

I have uploaded 2 projects

  1. A sample project, that you just download and play with the in app billing features. Play with it, debug it and understand/figure it out.
  2. A cleaned up project to help you integrate your app quickly. As of now android sdk is a bit screwed up to allow this project to be a library. So I’ll just guide you to integrate this in your application.

Mind you : this is a simple implementation. I have not incorporated the advanced security recommendations in the projects below. Read android in app billing documentation, to get advanced security features. Until then you are on your own risk.

Sample Project Instructions

  • Download “”
  • Create a new gmail account for all correspondence (Why, I will explain below)
  • Pay that 25$ and purchase a “Market Place” account.
  • Import the project to your eclipse environment
  • Create a signed application apk, File-Export-Select Your Project-Create OR Use keystore, it should be valid for 25 years from today.
  • Upload the signed apk to the market place as a “Draft Application”, DO NOT PUBLISH it.
  • For the uploaded apk, you need to add “In App Products”. You will find this link on the home page for your uploaded apk.
  • In dont forget to add your “public key” from your market place account “edit profile” page.
  • On the “In App Product List”, you need to add the following products one by one

The text below should used as “In app Product id”

  • sword_001 as Managed
  • potions_001 as Unmanaged

Title and Description dont matter (for testing purpose). Add what you need here.

  • Cost add 1$ (minimum, may be more, since other countries too have a minimum)
  • Click auto fill
  • Save
  • Publish the in app project (dont get confused here, you only need to publish the in app product, NOT the APP)
  • In the test account, add your personal gmail id here. Now, this gmail account needs to be configured as your first gmail account on your phone.
  • Yes, this WONT work on your emulator
  • Now the sample app should work.

To Enable InApp in your Application

  • Download “”
  • Copy all the packages to your project
  • Copy the layout files, string values and color values.
  • Copy the contents of <application> from the manifest to your manifest.
  • Provide correct package names to your class file references in the manifest file
  • Add the BILLING and INTERNET permissions
  • You may want to rename main.xml to purchase_main.xml, and also change the setContentView in
  • Clean it up and get it to compile.
  • To integrate, in your calling activity initialize the checkout code

Handler handler = new Handler() ;
CheckoutPurchaseObserver checkout = new CheckoutPurchaseObserver(this, handler);

  • To send the checkout request for your product


Configure your Play Developer account for in app

  • First, you need a merchant account
  • Very important point, test account should have the email address, that your device is using as the primary account for gmail.
  • Only the signed version of the application will work, so dont install from your Run Configurations in Eclipse
  • This purchaseUri is the “In-app Product ID” of your resource on the “>Create New In-App Product” options. This string should be set as the “in app product” id. That’s why the “id” is most important. The “In-App Product ID” is how you refer to that particular product.
  • Also, in dont forget to add your “public key” from your market place account “edit profile” page.

Note :

  • LogCat will show all errors as InAppBilling tag
  • This project creates a shared_preferences named “inappbilling”
  • And debug is set to true
  • The androidmanifest.xml may not be needed since this project does not have a home screen.
  • Now for the last part of this blog. Why do we need to create a new gmail account. Simple. The account you purchase your market place account for, cant be used for “testing” your in app billing. Since you cannot purchase products for yourself :). And your primary account on your device should be set as a “test account” on the market place account. Chicken-Egg issue here. Hope its clear.

If you cant get any of this to work, or find out that I have missed out on some steps, please comment and let me know.

An interesting blog I came by “

A very interesting Question from Stackoverflow. here

Edit : manifest file permissions (thanks to Jeff Ortiga)


40 thoughts on “Integrate In App billing, just 3 lines of code in your app

  1. I am having some trouble getting the inapp billing to work. I have an app that users can use for freee (it is using webview however) but then i have areas that i want to charge for. i am not sure how to make this work, or merge your code into my code. could you help me please, is there a way to contact you so i can send you my akp

  2. To start with you need to get the in app billing sample to work. That should eliminate any dependencies with your project. To get the sample working refer to my blog above. I have provided a eclipse project download for the sample.

    I think this link “” may have something for you. Go thru.

    I am thinking may be you can use “final static void sendString(Context context, String string)
    Sends the given string using an Intent with ACTION_SEND and a mime type of text/plain.” Read thru

    My Email Id is

  3. I have both the codes you suggested. What i am stuck with and having trouble understanding is where or how do i add the action_send into my code. Is this something I put in my APK or is it something I put into the html on my site?

    More specifically I have an app that has a main area free for all users. But then I have premium areas for users I wish to charge for membership. My app is a webview based app so everything really is html and php heavy on the programming side. I am struggling with how do I get the action send and inapp billing to work with this? My thought is that there is some java code that I would add to my html on my site that would then call back to the app and then inturn activate the inapp billing feature or verify that the user has already paid thus inturn letting them into the premium areas. ….But how is this accomplished?

  4. That’s a great article and example. But I was wondering if I want to available some contents for free, how could I do that?
    Let me explain my scenario. I’m developing an app for a magazine that wants to sell their editions online. But they want to give some editions for free, do I have to use the market for that? I can’t add a product with price ZERO in my product list. How could I do that?

    Many Thanks

    • Thanks Leonardo. I am glad that you liked the article.

      Out side of in app products, you would get a list of editions. And with each edition you would know if its free or priced. If priced you will add a product for it on market place. If free, you need not call the “checkout.sendCheckoutRequest(purchaseUri.toString(), null);”. Right ?

      I think you need to not confuse between the “content and its cost” with in app billing.

      For example, you can also create in app products like
      – 1 edition
      – bulk order minimum 10 editions
      – 25% discounted edition

      And then you will have a separate db/webservice that will give you the edition details like name, description, cost, category of cost. So that you can choose between the in app products to do the purchase.

      Hope I have made some sense. If not, please let me know.

  5. Thanks for your reply, but I’m a little bit confused with that! I’m new to Android world.

    So, as you say, I just put a kind of reference in the Android Market to bill the products?
    The magazine edition app have to be in my server to download?

    Is each edition an app? How do people do that usually?

    Kind Regards

    • Let me explain with a code sample. In the below sample, some ringtones are priced and some are free.

      if (isRingToneFree(id) || isItemOwned(getResourceGoogleCheckoutId(id))) {
      } else {
      Log.d(TAG, "ItemName : " + (String) ((Button) v).getText()
      + " PurchaseUri : " + purchaseUri.toString());
      checkout.sendCheckoutRequest(purchaseUri.toString(), null);

      Here I am assuming that you are using my code snippet as discussed on the original blog.

  6. I got that but let me ask about the download. After the user had purchased the ring tone, do you download it from where? Your server or is it published in the Market?

    And do you know how magazines do this in their app? How is the app architecture?

    • Thats the point, dont expect market to hold the product inventory or the product itself for your. In my case, I download the ringtone from my own webservice/app server.

  7. Hello,

    After a user purchases an item, my app crashes. I have just used the code you provided and the button. I receive the payment, but it crashes after the purchase.

    Thank You

  8. Hello,

    I have used the exact code that you have provided, but when a user purchases an item, the app crashes. I am just using this as a form of donations. It works and the user can buy an item, but right after the purchase the app crashes. I receive the money though.



  9. I have looked at numerous posts and there is still some ambiguity.–

    Is it possible to have a subscription model using either Paypal or the in-app billing model? Does it or does it not violate the Android Terms of Service?

    If we use in-app billing, do we need to prompt the user each month that we charge them? If we decide not to prompt, does that violate the Android Terms of Service?

    Instead of in-app billing, can we use paypal instead to collect our monthly fee? Can we charge them not using a monthly prompt?

    What are papers like the NYT doing? When I download their app and try to subscribe, I am taken to a form that accepts a credit card. Doesn’t that violate the Android terms of service?

    • Using any other form of billing model (Paypal, CCAvenue, or any thing else) does not violate Android TOS. Android is way different then iOS and Apple. A lot more open and providing.

      The choice to prompt is tied down to refund terms. Amazon EC2 also does not provide a prompt, instead just a information, that we are going to charge you so much. But its all based on the fact that they are authentic and will refund if there is a dispute. In short more a legal reason than a technical one.

      So you can choose to use a credit card to authenticate/charge a customer. That does not violate Android TOS.

      That said, you should consult a legal expert here. I can only share my experience.

  10. Hello, first of all thanks alot for this! Im a newb, and I don´t get this part: “This purchaseUri is the Uri of your resource,”, how and where do I define this? I know how to set it up on google market but not in the code..

    In the google example they do it like this: ” private static final CatalogEntry[] CATALOG = new CatalogEntry[] {
    new CatalogEntry(“sword_001″, R.string.two_handed_sword, Managed.MANAGED),” in

    I’m only selling 1 item (to activate to Pro mode). Do you have any tip? I’m sure I’ve probably missed something easy, thanks for your time 🙂

  11. Karl,
    The URI is nothing but your “In-App Product ID” which you “Create new In-App ID”. This should be a string without spaces (at best). And when you want to send a “purchase request from your code” (may be from your onClick of Checkout Button), you need to call
    CheckoutPurchaseObserver checkout = new CheckoutPurchaseObserver(this, handler);

    Hope its clear now. I have also modified the blog for more clarity.

  12. Hi, thanks for your reply. Like someone else was talking about I also get a crash after the purchase is complete.
    This is the message:
    “ERROR/AndroidRuntime(11326): FATAL EXCEPTION: main java.lang.NullPointerException at com.mcondev.test.CheckoutPurchaseObserver.onPurchaseStateChange(
    at com.mcondev.test.PurchaseObserver$
    at android.os.Handler.handleCallback(”
    I am using this: checkout.sendCheckoutRequest(“android.test.purchased”, “1”);
    Btw do you know if theres any respons from AM I can use to identify the transaction? (ie I want to store device id + transactionid in our database).

  13. Thanks a lot! One question, how do I catch the answer if an item is purchased, what should the code look like? Thanks

  14. Pingback: onPurchaseStateChange never called |

  15. Integrate In App billing, just 3 lines of code in your app mCruiseOn Dev's Blog I was suggested this web site by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my trouble. You are wonderful! Thanks! your article about Integrate In App billing, just 3 lines of code in your app mCruiseOn Dev's BlogBest Regards Craig

  16. I am getting this error “Item that you have requested is not available for purchase” . Can anyone point me towards a right direction

  17. Felix,

    I’ll make my tar available to you as soon as you share your email id with me. Send me a test mail on mcondev at gmail com

    I just evaluated your project, and frankly, it will take me about 40 hours to integrate it into your Junit project. It will cost you about 200$ for the integration.

    That said, I am willing to have a conversation with you to help you integrate all that you need. Its been a while since I have worked on it, but I am sure I can help. This at no charge :).

    Hope that helps.

  18. Hi, I am trying to integrate in app products in my android apps and got struck with google sample code.
    I hope your guidance above would be of help to me. But I could not get the linked shares u have put up in please give me the live links for your samples.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s