Now if you’re already using the in-app billing library from Google, you may be thinking — why should I be considering using the play billing library?

If you don’t currently have any in-app billing setup within your application, then I feel like it would make sense to go ahead and use what is the most current way of doing so.

If you already have in-app billing implemented in your app, then it may not make sense to switch over to the play billing library right away. The documentation does not state the library as a replacement, but more as an easier way of implementing in-app billing.

However, the library does allow you to reduce the amount of code required to implement in-app billing. This not only makes the in-app billing flow easy to implement, but it will be tidier to implement and therefore easier to maintain

On a final note, because it is newer and easier to implement I personally found that for developers who may not have any experience with in-app billing, the Play Billing Library makes the whole implementation process a lot more enjoyable. So for future developers joining the team, it may be a nicer choice to use the Play Billing Library.

To get going with the Play Billing library you need to begin by adding the following dependency to your application-level build.gradle file:

compile 'com.android.billingclient:billing:1.0'

Note: Be sure to check the version number when adding the dependancy as a newer version may be available at the time of you reading this.

Now we have the Play Billing API available in our project, we need to construct an instance of the BillingClient class. This can be instantiated by using the static newBuilder() method, passing in the reference to the activity which we are associating our Billing Client with. Finally, we can use the setListener() method to pass in a reference to our PurchasesUpdatedListener instance which is used to receive callbacks when the purchases for the current user are changed.

Now that we have a reference to the Billing Client instance we can go ahead and kick off the connection to the in-app billing API. Because this request is asynchronous, we pass in an instance of a BillingClientStateListener so that we can receive events based on the status of our API connection.

When you connect to the billing service and the connection is successful the onBillingSetupFinished() callback will receive an event — this is the point at which you will be able to interact with the billing service and query purchases, make purchases and so on. However, if the connection fails then the onBillingServiceDisconnected() will be triggered and you will need to handle the retrying of the connection yourself.