[Tutorial] Google Play Billing: In-app purchases.

Vadims Leonovs
Dec 4, 2018 · 5 min read


In this tutorial, I would like to share my knowledge about the integration of Google Play Billing Service.

We will create a simple android app using Kotlin, connect it with Developer Console and implement in-app products purchases.

1. Preparing

1.1. Create a new project and add dependencies to the gradle module app:

dependencies {
implementation 'com.android.billingclient:billing:1.2'
implementation 'com.android.support:recyclerview-v7:28.0.0'

1.2. Connect it with Developer Console

Steps are the following:

Build signed apk → https://play.google.com/apps/publish/ → All Applications → Create Application → Enter the Title and Create.

Then skip adding Product Details and select the App Releases tab from the left → Scroll down till Internal test track, choose it → Create Release → Upload signed apk → Save → Review

If you haven’t used an internal test yet, probably you will have a warning after Reviewing.

If you have this warning click App Release → Scroll Down till Internal test track → Manage → Add at least one tester (f.e. yourself) → Add a Feedback Email address or URL → Click Save

Then we are going to the In-app products tab from the left side.

And click Create managed product

The most important part here is Product ID. It will be used to request the available products list a little bit later. Product ID = SKU.

Fill in all necessary fields. Don’t forget to enable Active Status.

I’ve created 2 products with ids get_5_coins and get_10_coins

Fill in all remaining fields to publish your application.

Tip: For Store listing, you can use a placeholder for banners and screenshots now. Useful website: https://dummyimage.com/

In Content rating, you should mark this point as ‘yes’. Others according to your app, for me it’s ‘no’

When you solve all grey-colored steps, you will see that the app is ready for publication.

Go to App Releases → Closed track → Edit Release → Add From Library → Choose apk → Save → Review → Start rollout to alpha.

We finish with preparing!

While the application is under processing in Google Play we will finish continuing with coding.

2. Let’s Kotlin.

2.1. Let’s create layouts:

An item for RecyclerView

2.2. After that, we will create an adapter to show in-app products:

We have prepared everything you need, there is only one thing — the activity, where everything will happen. This part we will analyze in more detail:

2.3. Firstly we need to setupBillingClient(), put it in onCreate() method

2.4. We have a button in layout in the point 2.1., let’s implement click handling:

Pay attention to skuList.

SkuList is the list of product ID, which we’ve added in Developer Console:

and the type is BillingClient.SkuType.INAPP.

2.5. Now after click to the button we should receive a json and put it in our adapter:

2.6. Our Activity or Fragment should implement PurchasesUpdatedListener,

with onPurchasesUpdated function. Make it.

2.7. And now we can check the result:

After Clicking on Load Products, we will see in the logcat:

querySkuDetailsAsync, responseCode: 0

The list should be loaded and shown

Tip #0: if you received instead of zero some other value, you can your search problem here:


After clicking on some option, you will see buying dialog:

Tip #1: if you see something like that:

The item you requested is not available for purchase.

Firstly, let’s check that your account was added in testing list for this app and you’re approved

Find in Developer Console App Releases → Alpha → Manage testers → Find Opt-in URL and open it to approve that you’re tester.

Tip #2: If you use emulator, google understand that you’re tester, and provide for your whole purchase testing workflow automatically.

3. Let’s configure for your needs

Let’s decide what kind of purchases you need: one-time or that you can make constantly. By default, all purchases are one-time. And if you need to make it so that coins can be bought many times you need to clear the purchase every time after a successful purchase.

After tapping on 1-Tap Buy and successful buying if you wanna to buy the same item the second time you will receive: Response code 7 in onPurchasesUpdated. It means: Item Already Owned.

Now you have 2 options to clear purchases:

3.1. Very Dangerous!!! Don’t use it if you’re not understand what will happen

You can click Clear Cache in Google Play Store app

or from command line:

clear cache adb shell pm clear com.android.vending

3.2. Save method. Use it!

Just call this method in your app, to clear all in-app purchase tokens

As a result you will see something like this in logcat:

onPurchases Updated consumeAsync, purchases token removed: $yourToken

3.3. If you need make in-app purchases reusable, just clean it after each success buying

Just expand our onPurchaseUpdated function

That it’s all. Happy coding!

Source Code: https://github.com/vleonovs/billing-project

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade