This lesson teaches you to
You should also read
Before publishing your In-app Billing application, you'll need to define the product list of digital goods available for purchase in the Google Play Developer Console .
Specify In-app Products in Google Play
From the Developer Console, you can define product information for in-app products and associate the product list with your application.
To add new in-app products to your product list:
- Build a signed APK file for your In-app Billing application. To learn how to build and sign your APK, see Building Your Application for Release . Make sure that you are using your final (not debug) certificate and private key to sign your application.
- In the Developer Console, open the application entry that you created earlier.
- Click on the APK tab then click on Upload new APK. Upload the signed APK file to the Developer Console. Don’t publish the app yet!
- Navigate to the uploaded app listing, and click on In-app Products .
-
Click on the option to add a new product, then complete the form to specify the product information such as the item’s unique product ID (also called its
SKU
), description, price, and country availability. Note down the product ID since you might need this information to query purchase details in your application later.
Important: The In-app Billing Version 3 service only supports managed in-app products, so make sure that you specify that the purchase type is 'Managed' when you add new items to your product list in the Developer Console.
-
Once you have completed the form, activate the product so that your application can purchase it.
Warning: It may take up to 2-3 hours after uploading the APK for Google Play to recognize your updated APK version. If you try to test your application before your uploaded APK is recognized by Google Play, your application will receive a ‘purchase cancelled’ response with an error message “This version of the application is not enabled for In-app Billing.”
Query Items Available for Purchase
You can query Google Play to programmatically retrieve details of the in-app products that are associated with your application (such as the product’s price, title, description, and type). This is useful, for example, when you want to display a listing of unowned items that are still available for purchase to users.
Note: When making the query, you will need to specify the product IDs for the products explicitly. You can manually find the product IDs from the Developer Console by opening the In-app Products tab for your application. The product IDs are listed under the column labeled Name/ID .
To retrieve the product details, call
queryInventoryAsync(boolean, List, QueryInventoryFinishedListener)
on your IabHelper instance.
-
The first input argument indicates whether product details should be retrieved (should be set to
true
). -
The
List
argument consists of one or more product IDs (also called SKUs) for the products that you want to query. -
Finally, the
QueryInventoryFinishedListener
argument specifies a listener is notified when the query operation has completed and handles the query response.
The following code shows how you can retrieve the details for two products with IDs
SKU_APPLE
and
SKU_BANANA
that you previously defined in the Developer Console.
ListadditionalSkuList = new List (); additionalSkuList.add(SKU_APPLE); additionalSkuList.add(SKU_BANANA); mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
If the query is successful, the query results are stored in an
Inventory
object that is passed back to the listener.
The following code shows how you can retrieve the item prices from the result set.
IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() { public void onQueryInventoryFinished(IabResult result, Inventory inventory) { if (result.isFailure()) { // handle error return; } String applePrice = inventory.getSkuDetails(SKU_APPLE).getPrice(); String bananaPrice = inventory.getSkuDetails(SKU_BANANA).getPrice(); // update the UI } }