Google Play Billing Library meets Kotlin CoroutineXiaoyi LIBlockedUnblockFollowFollowingMay 13Google Play Billing is a service that lets you sell digital content on Android.
Many developers might already be familiar with Play Billing if you built applications offering in-app products and subscriptions within your apps.
For many years, it was not easy to develop on Google Play Billing, because you had to use an Android Interface Definition Language (AIDL) interface to implement some features of the Google Play Billing service.
The situation has changed since Google announced the new Google Play Billing Library in September 2017.
The library includes several convenient classes and features for you to use when integrating your Android apps with the In-app Billing API.
The library also provides an abstraction layer on top of the Android Interface Definition Language (AIDL) service, making it easier for you to define the interface between your app and the In-app Billing API.
Convenient APIs but with ListenersThe library provides convenient APIs to call Google Play such as:Query available products for In-app and subscriptionQuery purchases for In-app and subscriptionConsume the purchasesPurchase In-app and subscription productsHowever, the above functions in Google Play Billing Library are asynchronous calls with listeners.
Listeners, similar to Callbacks, are widely used in Java programming, especially for asynchronous operations.
In terms of coding style, listeners based programming will create an object implementing the listener interface, and you have to put the continuation code inside the callback functions of the listener.
A simple example to use the queryPurchaseHistoryAsync() function.
In the main function, it looks like your code finishes after the call but it actually does not, you have to jump to the onPurchaseHistoryResponse() to continue your programming.
This is not a natural human reading habit.
Nowadays many Android developers use Kotlin and Coroutine in their daily Android development.
Kotlin Coroutine is a solution for asynchronous or non-blocking programming.
Basically, Coroutine allows you to write asynchronous code like a regular, blocking, synchronous code.
Unlike Coroutines, these Listener based APIs in Google Play Billing Library break the sequential declaration of the code.
Suspend asynchronous billing APIs with CoroutinesuspendCoroutine() is a function from the coroutine library to obtains the current continuation instance and suspend suspends currently running coroutine immediately.
With the continuation, we can manually control when we want to resume the coroutine.
resume() we can return a value from our suspend function in case of success.
resumeWithException() we can resume the coroutine with an exception in case of error.
Here I will show a few code snippets about how to adapt the API in Coroutine.
Suspend the querySkuDetailsAsync()Suspend the queryPurchaseHistoryAsync()Suspend the consumeAsync()Can we suspend launchBillingFlow() ?Yes, we can, with Coroutine’s Channels pattern.
launchBillingFlow() will launch the Google Play service with a flow of UI for users to complete the billing process externally, and resume to the application.
The PurchasesUpdatedListener can be called multiple times, an unlimited Channel will allow us to buffer all the results and avoid race conditions.
If we can get the purchase results in the return type of function, that will be easy to read and debug the billing process.
Put everything togetherWe want to run the whole billing process like writing regular, blocking, synchronous code, which is cleaner to write, read and debug.
ConclusionIf you neither like the inconvenient way of AIDL nor dealing with the Listeners and Callbacks provided by the Google Play Billing Library, you probably want to move to the Coroutine style of the Billing process.
It’s more natural for humans to read regular, blocking and synchronous code, especially when doing code review or debugging programs.
With some wrappers of the Google Play Billing Library functions, we are able to take advantage of the high scalability and flexibility of Kotlin Coroutine, writing code with Google Play Billing Library can become simple and straight forward.