Skip to content

Most visited

Recently visited

navigation

Preparing Your In-app Billing Application

Video

Implementing Freemium

Before you can start using the In-app Billing service, you need to add the library that contains the In-app Billing Version 3 API to your Android project. You also need to set the permissions for your application to communicate with Google Play. In addition, you need to establish a connection between your application and Google Play. You must also verify that the In-app Billing API version that you are using in your application is supported by Google Play.

Download the Sample Application

In this training class, you use a reference implementation for the In-app Billing Version 3 API called the TrivialDrive sample application. The sample includes convenience classes to quickly set up the In-app Billing service, marshal and unmarshal data types, and handle In-app Billing requests from the main thread of your application.

To download the sample application, follow these steps:

  1. In Android Studio, select File > New > Import Sample.
  2. Type Trivial Drive into the search bar and select the Trivial Drive sample.
  3. Follow the rest of the wizard to import the sample to a directory of your choosing.

Alternatively, you can use git to manually clone the repository from the Google Samples GitHub site.

You cannot run this sample app as-is. It requires a unique license key from the Google Play Developer Console. For instructions, see the README file at the root of the Trivial Drive project directory.

Add Your Application to the Developer Console

The Google Play Developer Console is where you publish your In-app Billing application and manage the various digital products that are available for purchase from your application. When you create a new application entry in the Developer Console, it automatically generates a public license key for your application. You need this key to establish a trusted connection from your application to the Google Play servers. You need to generate this key only once per application, and you don’t need to repeat these steps when you update the APK file for your application.

To add your application to the Developer Console, follow these steps:

  1. Go to the Google Play Developer Console site and log in. If you have not registered previously, you need to register for a new developer account. To sell in-app products, you also need a Google payments merchant account.
  2. In the All Applications tab, complete these steps to add a new application entry:
    1. Click Add new application.
    2. Enter a name for your new In-app Billing application.
    3. Click Prepare Store Listing.
  3. In the Services & APIs tab, find and make a note of the public license key that Google Play generated for your application. This is a Base64 string that you need to include in your application code later.

Your application should now appear in the list of applications in Developer Console.

Add the In-app Billing Library

To use the In-app Billing Version 3 features, you must add the IInAppBillingService.aidl file to your Android project. This Android Interface Definition Language (AIDL) file defines the interface to the Google Play service.

You can find the IInAppBillingService.aidl file in the provided sample app. To add the In-app Billing library to your project, follow the instructions below for a new or existing project.

Adding in-app billing to a new project

To add the In-app Billing Version 3 library to a new project, follow these steps:

  1. Copy the TrivialDrive sample files into your Android project.
  2. Modify the package name in the files that you copied to use the package name for your project. In Android Studio, you can right-click the package name and then select Refactor > Rename.
  3. Open the AndroidManifest.xml file and update the package attribute value to use the package name for your project.
  4. Fix import statements as needed so that your project compiles correctly. In Android Studio, you can press Ctrl+Shift+O in each file showing errors.
  5. Modify the sample to create your own application. Remember to copy the Base64 public license key for your application from the Developer Console to your MainActivity.java.

Adding in-app billing to an existing project

To add the In-app Billing Version 3 library to an existing project, follow these steps:

  1. Copy the IInAppBillingService.aidl file to your Android project.
    • In Android Studio: Create a directory named aidl under src/main, add a new package com.android.vending.billing in this directory, and then import the IInAppBillingService.aidl file into this package.
    • In other dev environments: Create the following directory /src/com/android/vending/billing and copy the IInAppBillingService.aidl file into this directory.
  2. Build your application. You should see a generated file named IInAppBillingService.java in the /gen directory of your project.
  3. Add the helper classes from the /util directory of the TrivialDrive sample to your project. Remember to change the package name declarations in those files accordingly so that your project compiles correctly.

Your project should now contain the In-app Billing Version 3 library.

Set the Billing Permission

Your app needs to have permission to communicate request and response messages to the Google Play billing service. To give your app the necessary permission, add the following line in your AndroidManifest.xml manifest file:

<uses-permission android:name="com.android.vending.BILLING" />

Initiate a Connection with Google Play

To send In-app Billing requests to Google Play from your application, you must bind your Activity to the Google Play In-app Billing service. The sample includes convenience classes that handle the binding to the In-app Billing service, so you don’t have to manage the network connection directly.

To set up synchronous communication with Google Play, create an IabHelper instance in your activity's onCreate method, as shown in the following example. In the constructor, pass in the Context for the activity along with a string containing the public license key that was generated earlier by the Google Play Developer Console.

Security Recommendation: Google highly recommends that you do not hard-code the exact public license key string value as provided by Google Play. Instead, construct the whole public license key string at runtime from substrings or retrieve it from an encrypted store before passing it to the constructor. This approach makes it more difficult for malicious third parties to modify the public license key string in your APK file.

IabHelper mHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
   // ...
   String base64EncodedPublicKey;

   // compute your public key and store it in base64EncodedPublicKey
   mHelper = new IabHelper(this, base64EncodedPublicKey);
}

Next, perform the service binding by calling the startSetup method on the IabHelper instance that you created, as shown in the following example. Pass the method an OnIabSetupFinishedListener instance, which is called once the IabHelper completes the asynchronous setup operation. As part of the setup process, the IabHelper also checks if the In-app Billing Version 3 API is supported by Google Play. If the API version is not supported, or if an error occurs while establishing the service binding, the listener is notified and passed an IabResult object with the error message.

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
   public void onIabSetupFinished(IabResult result) {
      if (!result.isSuccess()) {
         // Oh no, there was a problem.
         Log.d(TAG, "Problem setting up In-app Billing: " + result);
      }
         // Hooray, IAB is fully set up!
   }
});

If the setup completed successfully, you can now use the mHelper reference to communicate with the Google Play service. When your application is launched, it is a good practice to query Google Play to find out what in-app items are owned by a user. This is covered further in the Query Purchased Items section.

Important: Remember to unbind from the In-app Billing service when you are done with your activity. If you don’t unbind, the open service connection could degrade device performance. To unbind and free your system resources, call the IabHelper's dispose method when your Activity is destroyed, as shown in the following example.

@Override
public void onDestroy() {
   super.onDestroy();
   if (mHelper != null) mHelper.dispose();
   mHelper = null;
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.