How to Implement In-App Purchases in Android?
Last Updated :
26 Nov, 2021
In this article, we’ll look at how to use the billing library to make in-app purchases on Android smartphones. We’ll create an app as part of the process to help us learn how In-App purchases work. So, without further ado, let’s get started. We’ll start by creating an app with an Empty Activity as the first activity, naming it MainActivity, and naming the XML file activity main.xml.
Step by Step Implementation
Step #1
In your app’s build.gradle file, add the following line to the dependencies section:
dependencies {
...
implementation 'com.android.billingclient:billing:2.0.1'
}
Step #2
You must also provide the billing permission in the Android manifest:
<uses-permission android:name="com.android.vending.BILLING" />
And now that we’ve finished setting up the permissions and dependencies, we must begin the implementation process by establishing a connection with Google Play.
Step #3
Two variables will be created in MainActivity.
Kotlin
class MainActivity : AppCompatActivity() {
private lateinit var myBilled: BillingClient
private val courseList = listOf( "geeks_for_geeks_android_course" , "geeks_for_geeks_dsa_course" )
|
The first variable we’ve generated is of type BillingClient, which offers the main interface for communication between the library and user application code, and we’ve also established a list of courseList that defines the list of product IDs we need to buy.
Step #4
We’ll now set up the billing client using the onCreate() method.
Kotlin
class MainActivity : AppCompatActivity(), PurchasesUpdatedListener {
override fun onCreate(savedInstanceState: Bundle?) {
..........
doMyBiller()
}
private fun doMyBiller() {
myBiller = MyBiller.newBuilder( this )
.enablePendingPurchases()
.setListener( this )
.build()
myBiller.startConnection(object : MyBillerStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == MyBiller.BillingResponseCode.OK) {
}
}
override fun onBillingServiceDisconnected() {
}
})
}
|
Step #5
At this point, Let’s get our purchase product set up in our game console. To begin, log in to your game console and select CREATE APPLICATION from the menu. Set up your app like you would any other app, including providing information such as the App name, App description, and so on.

Image #1: Creating a new app in the play developer console
Gather all necessary information, such as store listings and ratings, and proceed to the point where you only need to submit the APK or AAB file. After you’ve completed the aforementioned procedures, we’ll need to develop PRODUCTS that we can buy. To accomplish this, when we’ve finished filling out the above information, we’ll need to scroll down to activate it and enter the product’s pricing.

Image #2: Setting up in-app purchase
Press Save!
Step #6
When you are done with the amount and payments, the next step is to load the products, do this by:
Kotlin
private fun loadBillers() = if (myBiller.isReady) {
val params = Billers
.newBuilder()
.setSkusList(skuList)
.setType(MyBiller.SkuType.INAPP)
.build()
myBiller.queryLoadBillersAsync(params) { billingResult, loadBillersList ->
if (billingResult.responseCode == MyBiller.BillingResponseCode.OK && loadBillersList.isNotEmpty()) {
for (loadBillers in loadBillersList) {
}
}
}
} else {
println( "Something Wrong" )
}
|
Step #7
To complete the purchase, we’ll use the click listener. We’ll add a success callback for SKU fetching in step 06’s success callback.
Kotlin
if (myBiller.responseCode == BillingClient.BillingResponseCode.OK && myBiller.isNotEmpty()) {
for (myBillers in myBillers) {
if (myBiller.sku == "gfg_product_one" )
buyCourseBtn.setOnClickListener {
val billingFlowParams = BillingFlowParams
.newBuilder()
.setSkuDetails(skuDetails)
.build()
myBiller.launchBillingFlow( this , billingFlowParams)
}
}
}
|
When the myBiller clicks, BillingFlowParams commences the buy flow with the precise SKU data, and the billingClient launches the billing flow to begin the purchase of the specified SKU.
Step #8
Now we create a mutable list, to get the list of all the purchasable items on the app, and then perform the in-app purchases
Kotlin
override fun onPurchasesUpdated(
myBiller: MyBiller?,
purchases: MutableList<Purchase>?
) {
if (myBiller?.replyCode == BillingClient.BillingReplyCode.OK && purchases != null ) {
for (purchase in purchases) {
acknowledgePurchase(purchase.purchaseToken)
}
} else if (myBiller?.replyCode == BillingClient.BillingReplyCode.USER_CANCELED) {
} else {
}
}
private fun acknowledgePurchase(purchaseToken: String) {
val params = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchaseToken)
.build()
billingClient.acknowledgePurchase(params) { myBiller ->
val replyCode = myBiller.replyCode
val debugMessage = myBiller.debugMessage
}
}
|
We’ve completed our task. Simply create a Signed APK and publish it to the Play Store, then wait for it to be accepted. After that, all we have to do is set up the card and buy it. That’s it for this article, hope you get the exact idea on how to implement in-app purchases in your app and start earning!
Similar Reads
How to Implement Item Click Interface in Android?
When we click on an item in an application either it gives some information or it redirects the user to any other page. In this article, we will learn that how we can implement Item Click Interface in an android application. What we are going to build in this article? In this article, we will be usi
4 min read
How to Implement Swipe Down to Refresh in Android
Certain applications show real-time data to the users, such as stock prices, availability of a product on online stores, etc. Showing real-time data requires continuous syncing of the application, and could be possible by implementing a program such as a thread. A Thread could be initiated by the ap
3 min read
How to Implement Google AdMob in Your Android App?
In order to earn money from the Android app or game, there are several ways such as in-App Purchases, Sponsorship, Advertisements, and many more. But there is another conventional approach to earning money from the Android app is by integrating an advertisement which is known as Google AdMob. Google
4 min read
How to Implement Google Map Inside Fragment in Android?
In Android, the fragment is the part of Activity that represents a portion of the User Interface(UI) on the screen. It is the modular section of the android activity that is very helpful in creating UI designs that are flexible in nature and auto-adjustable based on the device screen size. The UI fl
4 min read
How To Run Android Apps On Linux
There are many users who use Android as their portable device but they prefer working on a Linux machine. Now, there are ways or apps available that can easily sync these two platforms. But what if anyone wants to run an Android app on a Linux desktop? There are multiple uses wanting to use mobile-e
4 min read
How to Request Permissions in Android Application?
Starting from Android 6.0 (API 23), users are not asked for permissions at the time of installation rather developers need to request the permissions at the run time. Only the permissions that are defined in the manifest file can be requested at run time. Types of Permissions1. Install-Time Permissi
6 min read
How to Integrate In-App Review API into Android App?
Once we publish our app over the PlayStore and as it became live there, app rating and reviews become very crucial to driving audience and downloads to your app. In order to increase this, we ask our users to rate the app through a popup window and redirecting them to the PlayStore. But this nowaday
3 min read
How To Make An Android App
Have you always wanted to make an Android app but got lost in the complexity of the Android Studio or the technical jargon used in the development process? Don't worry, you have come to the right place. We are going to help you get started with your first Android app. The app that we are going to ma
7 min read
How to Build a Simple Notes App in Android?
Notes app is used for making short text notes, updating when you need them, and trashing when you are done. It can be used for various functions as you can add your to-do list in this app, some important notes for future reference, etc. The app is very useful in some cases like when you want quick a
9 min read
How to Create a News App in Android?
Networking is an integral part of android development, especially when building data-driven clients. The java class mainly used for network connections is HttpUrlConnection. The class requires manual management of data parsing and asynchronous execution of requests. For network operations, we are be
10 min read