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:
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.
First, go to the Google Play Console and sign in.
Next, if you haven't already, create your app by clicking on Create App and filling in the necessary details.
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.
With these foundational steps complete, you're ready to integrate the necessary libraries into your app to handle in-app purchases.
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.
To start, include the Google Play Billing Library dependency in your app-level build.gradle file:
This integration allows your app to communicate with Google Play's billing system, setting the stage for 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.
Begin by creating a BillingClient instance:
BillingClient billingClient = BillingClient.newBuilder(context)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
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.
Once the BillingClient is initialized, the next step is to query the products available for purchase so you can display them in your app.
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.
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.
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.
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.
Click Create email list.
Example of the accept invitation page that each tester will receive.
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.
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.
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.
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.
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.
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:
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.