Home
Blog
How to Implement In-App Purchases in Android Apps: A Detailed Guide

How to Implement In-App Purchases in Android Apps: A Detailed Guide

Portrait photo of blog author
Jigar Brahmbhatt
Android Consultant

Implementing in-app purchases (IAPs) in your Android app can significantly enhance your app's monetization strategy. In-app purchases allow users to buy digital content or subscriptions directly within your app, offering a seamless and engaging user experience. In this article, we'll provide a detailed guide on how to implement in-app purchases in your Android app. From setting up your Google Play Console to integrating the Billing Library, we cover every step to ensure a smooth implementation process.

Table of Contents:

In this blog post:

1. Setting Up Your Google Play Console

Before you can implement in-app purchases, you need to configure your Google Play Console. This step is crucial as it lays the foundation for your in-app purchases.

Sign In

First, go to the Google Play Console and sign in.

Google Play Console payments profile screenshot

Create an App

Next, if you haven't already, create your app by clicking on Create App and filling in the necessary details.

Google Play Console create app screenshot

Set Up In-App Products

Finally, navigate to Monetize -> Products -> In-app products. Click on Create product to define your in-app purchase items, such as consumable, non-consumable, or subscription.

Google Play Console set up app screenshot

With these foundational steps complete, you're ready to integrate the necessary libraries into your app to handle in-app purchases.

2. Adding the Google Play Billing Library

Now that your Google Play Console is set up, the next step is to integrate the Google Play Billing Library, which is essential for handling in-app purchases within your app.

Add Dependency

To start, include the Google Play Billing Library dependency in your app-level build.gradle file:

j


This integration allows your app to communicate with Google Play's billing system, setting the stage for initializing the BillingClient.

3. Initializing the BillingClient

After adding the Billing Library, the next step is to initialize the BillingClient. This client is used to communicate with the Google Play Billing service.

Create a BillingClient Instance

Begin by creating a BillingClient instance:

BillingClient billingClient = BillingClient.newBuilder(context)
      .setListener(purchasesUpdatedListener)
      .enablePendingPurchases()
      .build();


Start the Connection

Next, start the connection:

billingClient.startConnection(new BillingClientStateListener() {
  @Override
  public void onBillingSetupFinished(BillingResult billingResult) {
      if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
          // The BillingClient is ready. You can query purchases here.
      }
  }

  @Override
  public void onBillingServiceDisconnected() {
      // Try to restart the connection on the next request to Google Play by calling the startConnection() method.
  }
});


With the BillingClient initialized, you can now query the products available for purchase in your app.

4. Querying Available Products

Once the BillingClient is initialized, the next step is to query the products available for purchase so you can display them in your app.

Query Product Details

Start by querying product details:

List<String> skuList = new ArrayList<>();
skuList.add("premium_upgrade");
skuList.add("gas");

SkuDetailsParams params = SkuDetailsParams.newBuilder()
      .setSkusList(skuList)
      .setType(BillingClient.SkuType.INAPP)
      .build();

billingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
  @Override
  public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
      if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
          for (SkuDetails skuDetails : skuDetailsList) {
              String sku = skuDetails.getSku();
              String price = skuDetails.getPrice();
              // Use this information to display product details in your app's UI.
          }
      }
  }
});

This step ensures that you can list all available products to your users, setting up the purchase flow.

5. Create an App Release

First, you’ll need to generate a signed release for your app.  There are a few steps involved in this which you can read in Google’s documentation.  Once you’ve complete this, go to Release > Testing > Internal testing in the left navigation and click the Create new release button.

Google Play Console internal testing screenshot

Upload the signed APK or Android app bundle you created following the instructions in the link to Google’s documentation above and then click Start rollout to internal testing.

6. Add App Testers

It is very important in testing tracks to add and enable testers for your app.  This will allow users to find and install the app while it is only available for testing on not available for general release on the Play Store.

This is a 2-step process.  First, you’ll register a testing group and then add individual testers to the group.  Go to Release > Testing > Internal testing on the left navigation and then select the Testers tab.

Google Play Console internal testers screenshot

Click Create email list.

  • Choose a name that will make sense for your app testers for your reference.
  • Add the email addresses for each user’s Google account that they have linked to their Android test device.
  • Save changes.
  • Click the Copy link button under the section How testers join your test.
  • Send this link to each of your testers.  They must then open the link and click Accept Invite
Google Play app tester invitation screenshot
Example of the accept invitation page that each tester will receive.

Example of the accept invitation page that each tester will receive.

7. Add IAP Testers

There is one other very important testing permission to set. After you add users as app testers they need a separate permission to be able to test in-app purchases without incurring any charges to their account.  You’ll find this under Settings > License testing in the left navigation.

Add the same email address that is linked to a Google account for each user you added above in this section and click the Save changes button.

Google Play Console license testing screenshot

8. Set Up an In-App Purchase Subscription Product

Now that you’ve completed the setup of your app on the Play Console for in-app purchases and added the billing permissions to your app itself, the last step is to create an in-app purchase product.  In this example, we’ll create a subscription.  Go to Monetize > Products > Subscriptions in the left navigation.

Google Play console manage subscriptions screenshot

Now click the Create subscription button on the right side of the screen.  Fill in the information on the next screen including a Product ID (you’ll reference this in your app code), a Name and Description, Price, and Billing Period.  Then click the Save button.

Google Play Console create subscriber product screenshot

Note that once you create a Product ID it cannot be changed or reused, so choose a name carefully.  

A common practice is to use a reverse url structure with your app name.  You may also want to include the price point if you anticipate testing different price points for your app in the future.  An example might be com.namiml.demoApp.monthlySub.299.

Integrating your subscription product into your app is beyond the scope of this article.  You can find more details in Google’s documentation and in Nami ML’s SDK setup instructions.

9. Confirm Purchase Testing is Working

Finally, let’s start up your app with an integrated purchase process and try to buy a product to confirm that you are able to make purchases as a tester and not get charged.  If you have set up your email address correctly as a tester, when the Google Play bottom dialog opens up to complete the purchase in your app, next to the GPay information it should say Test card, always approves.  

Android Phone test subscrpition purchase screenshot

If it does, then you will not be charged for the purchase.  If you see details on another payment method at this point, try clicking on the payment method and see if the Test card, always approves option is available and select it.  

If this option is not available, there are 2 things you should check:

  • Are you signed in to the correct Google account on your device or in the emulator?
  • Does the account you have signed in as match the invite you received to be a tester that was added following the steps above?

As long as both of these are true, you should be all set.

Now you should be all set for testing and making in-app purchases in your Google Play store Android app.

👉Read more: Play Billing Deferred Proration Mode

Jigar Brahmbhatt is a consulting Android Architect at Nami ML. His journey in the world of Android dates back to 2012 when the Android SDK he worked on shipped to millions of users of top US brick-and-mortar retailer apps.  Since then, he has been building top-class Android apps and SDKs in areas like chatbots to banking and e-commerce.

Sign up to our newsletter

Get the latest articles delivered straight to your inbox.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Similar articles

Read similar articles to this one