Simple sample of Android in-app billing v2 in Kotlin

2019-05-25

Over the course of a few days this week, for my side project, I implemented Android in-app billing v2 in Kotlin. It took about 5 hours to read through everything and implement it, but it should have taken less than an hour with the right sample code and walk-through.

This is that walk-through that I would have wanted to just quickly implement subscription services in my app.

Google, in general, has good documentation for their in-app billing, but their code sample was more helpful for me, even though it wasn't updated for the latest version of the library. There was a major version update with breaking changes that couldn't be copy-pasted into a current app.

There are about ten main pages of documentation for in-app billing, but to simply get started and running, you will only need one doc page and one sample code page:

Or...

I can just provide the highlights and updated code that will actually work...

  • Product IDs must be unique and can NOT be reused. Product IDs must start with a lowercase letter or a number and must be composed of only lowercase letters (a-z), numbers (0-9), underscores (_), and periods (.). The product ID 'android.test' is unavailable for use, along with all product IDs that start with 'android.test'.
  • Subscriptions can NOT be unpublished
  • Subscription prices must be within a certain price range, US currently USD .99 - 400.00: https://support.google.com/googleplay/android-developer/table/3541286
  • The easiest way to test your purchase flow is to have testers defined in the Google Play Console > Settings > Developer account > Account details > License Testing > 'Gmail accounts with testing access'

Code:

{% gist user="danialgoodwin" id="5bc709b19b776e707a827772401aaf96" %}

From "0" to "it works on my machine" :p




<Better programming language by Effective Java