Search code examples
androidin-app-billingandroid-billing

About In app billing initialization time


I am adding iab to my application this days but after reading all the docs at google and doing some tests I have a doubt. When should I init the service? Is it a good time to do it at application initialization? Should I init the system every time the user wants to buy a product?

How do you handle this?

Cheers.


Solution

  • Okay so it's time for a full-flavoured answer I guess.

    • You bind to/unbind from the IAB service when you need to perform a transaction or retrieve information/are finished. No need to worry about performance there because this is a local service which does not necessarily connect to Google servers when you bind to it (only exception: a purchase, but that'll take some time anyway); it follows a different strategy to decide when to go online.
    • It's a good idea to sync your app's internal idea of what the user owns with the idea of IAB, as tjPark rightly says. Whether that's at the startup of your app or only after the user makes a couple of choices depends on your app. If you need to know for your splash screen what IAB items the user owns, then do it in your splash screen Activity. If it's only becoming relevant later, it makes more sense to query the IAB service later.
    • You should also carefully think of a consumption strategy which suits your need if your IAB items can be consumed.
    • Always be aware that IAB V3 uses caching extensively so even synchronizing your app with the IAB service does not necessarily get you the latest information. E.g. when a user buys an in-app product on device 1 and wants to use it on device 2, there will be a delay until it shows up. Or if you cancel a transaction in Google Checkout/Wallet and the device is offline, you also won't know immediately.
    • Don't use Google example code without refining it to achieve product maturity.
    • Know that IAB service responses can be subject to re-play attacks because you cannot provide a nonce with your request.
    • Know that if you don't have a server-side validation then your whole IAB code could be replaced by dummy code which simply returns positive responses.