Upgrade your browser Download Chrome Download Chrome Download Firefox Download Firefox Download Opera Download Opera Download Safari Download Safari Download Explorer Download Explorer

Check out our newest children's book for iPad & iPhone: Who's in the Loo? Download Close

Robot Media

Blog

Android Billing Library: in-app billing made simple

Android Billing Library is an open-source Android library to work with Android Market’s in-app billing service.

The library provides high-level functions (as simply as requestPurchase(“android.example.purchased”)) for the full in-app billing specification, in addition to an obfuscated transaction database that is automatically populated and can be easily queried. The code also works as an example in-app billing implementation.

This is our third open-source contribution, with more to come shortly. If you’re interested in in-app billing, we invite you to fork the code on github and contribute with issues or pull requests.

38 thoughts on “Android Billing Library: in-app billing made simple

  1. Michael says:

    Hi, you say in the readme that this should not be used for production code. I was wondering - what are the known issues with it? In other words, why shouldn’t it be used for production code?

  2. admin says:

    We think the library needs a few months in the open before considering it stable.

    That said, using it should be better than starting from scratch.

    If you find any issues, please report them on github. We will try to fix them ASAP.

    Thanks.

  3. Andrey says:

    Hi,

    I would like to develop two-in-one application. By default, user downloads Free version. If he presses some button in the app and pay, then application should become Paid version.

    Now I need to write a lot of code to get it.
    Will your library help me?

    What I want to have - just one boolean variable (user paid or not), which I will check in my code. Everything else should be handled by library.

    Ideally, if requested in the code, if internet connection is available, application should check if user really paid (or this is hacked version of the application).

  4. admin says:

    @Andrey Yes, this is exactly what this library is for.

  5. Andrey says:

    Thanks. Is there any simple code to check?

  6. admin says:

    AbstractBillingActivity should be a good place to start.

  7. Andrey says:

    Thanks. But actually some example would be quite helpful…
    Please see my question - http://stackoverflow.com/questions/6573973/how-to-use-android-billing-library

    Thank you

  8. Andrey says:

    Also I am not quite clear where to input my key.

  9. Andrey says:

    OK, looks like I’ve got how to use that - in my class I should use extends AbstractBillingActivity instead of extends Activity (it is a little bit tricky with PreferenceActivity - I had to modify AbstractBillingActivity).

    Still have the question about the key.

  10. Trevor Johns says:

    This looks very good! I’m glad to see developers outside Google extending the in-app billing API.

    That being said, you might want to consider licensing this under something other than (L)GPL. Normally I’m not one to nit over license choice. However, many in-app billing developers need to manually obfuscate their code for security, and needing to release their obfuscated source would largely negate these efforts.

    You might also want to register your purchase DB with the Android backup framework (http://developer.android.com/guide/topics/data/backup.html) so that unmanaged purchases can be restored if a user wipes their phone.

  11. Andrey says:

    Need help with the next item.

    Let’s say I’ve requested purchase of the item: requestPurchase(ANDROID_MARKET_ITEM);
    then I’ve enabled some ‘paid function’ in my application at onPurchaseExecuted. What should I do then? Should I store ‘purchased status’ in my database? Or, is it done automatically? (in addition to an obfuscated transaction database that is automatically populated and can be easily queried)

    If I call restoreTransactions, will it call onPurchaseExecuted? Or, how can I understand if user paid or not?

  12. admin says:

    @Trevor Thanks for the feedback!

    Yes, we’re considering changing the license for this particular library.

    And using the Android backup framework is a great suggestion!

  13. admin says:

    @Andrey Yes, you can query the database or handle it yourself.

    restoreTransactions will populate the database and call onPurchaseSomething accordingly.

    It would be better if we moved the more technical discussions to github, or stackoverflow. We will reply much faster in github.

  14. mi6x3m says:

    This is just great, exactly what I needed, will check it ASAP. And yes I need it for production code :).

  15. admin says:

    @mi6x3m Gald to help! The library is getting better with user contributions. Hopefully it will be considered production ready soon.

  16. Hello,

    Great work; I love what you’ve done so far. Since there’s been some new work done on this in the path month, would you consider the library ready for production at this point, or have you heard of any apps using it successfully?

    Either way, it’s probably better than what I could do from scratch, so if I end up using it in production, I’ll try to give you some feedback.

  17. admin says:

    While we haven’t receive any major complaints, there’s a few known bugs and improvement points. As you mentioned, we do believe that it’s better than starting from scratch, but we don’t consider it production-ready yet. If you use it, test, test, test, test. :)

  18. Daniel says:

    Please consider the alternative library : android-test-billing to test the In-App billing on the emulator.
    This library is used in the project Horer - horaires de RER trains.
    In comparison to the “Android Billing Library”, this is not a high-level library, but just an implementation of the In-App Billing API, which works the same way as the real API.

  19. Nomi says:

    I used this app for in app billing. In app billing implemented in paid version of this application simply change your product id to file and in app billing will start working in to your application. Instruction included in the package.

    http://www.preproject.com/projectDetail.asp?projectID=244

  20. amir says:

    I’ve a simple question.
    how to test it ?
    I uploaded the apk to the market and add my gmail as a test account and when trying to buy it, it’s appear the product with “add payment method”.
    how to sell it, I’m a test account, i wondering…

    thanx

  21. Alan says:

    Where exactly to I put the line?

    requestPurchase(“application_package”);

    I have the library installed had have imported the net.robotmedia.billing.helper.AbstractBillingActivity to my starting activity where I am trying to put it but does not work. I cannot figure out where that line needs to go where it will not cause an error. If it has something to do with sub-classing I need an example because I an not sure what you are referring to. thanks

  22. Alan says:

    Can someone please tell me where/how I need to implement the call to use this
    I have the library installed but cannot figure out where and how this line of code needs to be implemented:

    requestPurchase(“com.example.item”)

    I know I need to replace what is in quotes with my application’s package name but where/how exactly do I do it

    thanks

    Alan

  23. I think i’ve implemented everything just fine,
    the purchase window pops up and I can click
    on buy, but then it is saying “Authorizing Purchase”
    for ever and then it fails. The purchase is listed
    in the google checkout account as “Cancelled”
    with the reason “Reason: Took too long to deliver”.
    After this i receive the
    com.android.vending.billing.IN_APP_NOTIFY
    but it’s of course not in the state “purchased”.

    Any ideas?

  24. Tr4X says:

    Great lib! I was searching for something like that :)
    I just have a little issue, when I request a purchase for the first time, only a black screen appears. Then if a press back and re-request, it works fine.

    Is that a known issue?
    Thanks!

  25. admin says:

    Thanks Tr4x!

    Would you mind asking this question on https://github.com/robotmedia/AndroidBillingLibrary/issues so other developers can benefit from it?

    Best!

  26. Developer says:

    For Alan:
    The key is that you extend your activity class with AbstractBillingActivity as follows, then you can directly use the magic line :)

    package com.shariq.inapp;

    import net.robotmedia.billing.BillingController;
    import net.robotmedia.billing.BillingRequest.ResponseCode;
    import net.robotmedia.billing.helper.AbstractBillingActivity;
    import net.robotmedia.billing.model.Transaction.PurchaseState;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;

    public class ShariqInAppActivity extends AbstractBillingActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    requestPurchase(“com.saltish”);
    //magic line

    }
    });

    }

    @Override
    public byte[] getObfuscationSalt() {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public String getPublicKey() {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void onBillingChecked(boolean supported) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onPurchaseStateChanged(String itemId, PurchaseState state) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onRequestPurchaseResponse(String itemId, ResponseCode response) {
    // TODO Auto-generated method stub

    }
    }

  27. Josh says:

    Any plans on integrating the Amazon Appstore version of in-app billing or can you point to anyone who has added this to this library? It seems like it could be cross-platform to work with multiple app stores if tweaked a bit, but I don’t want to sink the time into that work if it has already been done or is already in progress somewhere else.

  28. Giuseppe says:

    Can we use this fantastic lib also for Subscription ?

  29. admin says:

    Yes. The library now supports subscriptions too.

  30. Tim says:

    It appears that the library requires Android OS 3.0 at minimum. How can the library be used to support apps that use Android 2.3.3?

  31. Tim says:

    (In response to my earlier post). The library DOES support apps that use Android 2.3.3. Make sure that the Google Play app has been updated on the device. Then take a look at how the Dungeons Redux example uses the library (look at the project’s properties).

  32. harshid says:

    hello friends if any have android in-app demo project which is perfect run then please send me.

  33. Lucy says:

    Hi, I ‘am trying to compile the dungeonsRedux app to see how the billing works etc… I have added my Key, but the app crashes instantly when i try to load it on my device from eclipse. I’m not sure what else i need to do. Here is the logcat. Devices tested on are “S3 ICS” and “HTC Desire 2.2″

    Hoe you can help!!

    08-17 15:06:11.922: E/AndroidRuntime(862): FATAL EXCEPTION: main
    08-17 15:06:11.922: E/AndroidRuntime(862): java.lang.RuntimeException: Unable to instantiate application net.robotmedia.billing.example.Application: java.lang.ClassNotFoundException: net.robotmedia.billing.example.Application in loader dalvik.system.PathClassLoader[/data/app/net.robotmedia.billing.dungeons.redux-1.apk]
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:670)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4483)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.app.ActivityThread.access$3000(ActivityThread.java:135)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2181)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.os.Handler.dispatchMessage(Handler.java:99)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.os.Looper.loop(Looper.java:144)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.app.ActivityThread.main(ActivityThread.java:4937)
    08-17 15:06:11.922: E/AndroidRuntime(862): at java.lang.reflect.Method.invokeNative(Native Method)
    08-17 15:06:11.922: E/AndroidRuntime(862): at java.lang.reflect.Method.invoke(Method.java:521)
    08-17 15:06:11.922: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    08-17 15:06:11.922: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    08-17 15:06:11.922: E/AndroidRuntime(862): at dalvik.system.NativeStart.main(Native Method)
    08-17 15:06:11.922: E/AndroidRuntime(862): Caused by: java.lang.ClassNotFoundException: net.robotmedia.billing.example.Application in loader dalvik.system.PathClassLoader[/data/app/net.robotmedia.billing.dungeons.redux-1.apk]
    08-17 15:06:11.922: E/AndroidRuntime(862): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    08-17 15:06:11.922: E/AndroidRuntime(862): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    08-17 15:06:11.922: E/AndroidRuntime(862): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.app.Instrumentation.newApplication(Instrumentation.java:945)
    08-17 15:06:11.922: E/AndroidRuntime(862): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:660)

  34. Christopher Perry says:

    I’m also curious about Josh’s question above. If somebody already rolled the dice on the Amazon version, it would be great if you could point us in the right direction. I’m willing to contribute if somebody wants to integrate Amazon into this lib on a fork on Github.

  35. Oleg Godovykh says:

    Lucy, I’ve got similar problem (but I’m using IntelliJ IDEA). I solved it with defining module AndroidBilliLibrary as Android library (not the project). Also I’ve removed test project, but I don’t think that it made some changes to all the project.

  36. Antonello says:

    Hi everybody,
    I have installed and used the example Dugeons Redux. Everything works fine and it has been very helpful.

    One thing still not clear to me, how do I use a remote server to download contents?
    Something like:

    public void onPurchaseStateChanged(String itemId, PurchaseState state) {
    ContentManager cm = new ContentManager();

    if (state.equals(PurchaseState.PURCHASED)){
    cm.downloadContent(itemId);
    }
    Log.i(TAG, "onPurchaseStateChanged() itemId: " + itemId);
    updateOwnedItems();
    }

    ?
    How do I implement security on the server?
    In the google inapp example there is a Security class in which it is written:

    /**
    * Security-related methods. For a secure implementation, all of this code
    * should be implemented on a server that communicates with the
    * application on the device. For the sake of simplicity and clarity of this
    * example, this code is included here and is executed on the device. If you
    * must verify the purchases on the phone, you should obfuscate this code to
    * make it harder for an attacker to replace the code with stubs that treat all
    * purchases as verified.
    */

    Obviously I did not understand something.
    Could you kindly explain how to implement security on the remote server and how to do in particular with your library?
    thanks
    Antonello

  37. Ruy says:

    Wow, I just want to say that this Library is very impressive! I spent the last two days trying to understand the in-app billing sample application and its million classes and reasons why it wasnt working.. And now with this library I could finnally see the google Play window popping up! The implementation was completely straigthforward and in less than 15 minutes I was already using! Congratulations and thank you for making this sweet heavenly Library! Big fan now! :)

  38. Hemant Chand says:

    when i import the billing library project in eclipse then its show the error on generate java file
    com.android.vending.billing
    IMarketBillingService.aidl
    @Override public android.os.IBinder “”asBinder()””
    {
    return this;
    }

    in above asbinder() show the error

    “The method asBinder() of type IMarketBillingService.Stub must override a superclass method”

    so please tell me what kind of mistake i have done on this project ???

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>