Google PlayStore and automated deployment

We recently set out to automatically deploy an apk to the PlayStore to speed up our distribution process.

Automation

Releasing can be a long manual task for a developer. So we like to use services, write scripts and generally make it one button press away.

Our team, currently uses CircleCI. It runs our tests and builds our apps. We recently set out to automatically deploy an apk to the PlayStore, to speed up our distribution process.

Google Play Developer API

How to

Service Account

You will need to retain the json key you created during this process.

Access Token

You will need the following variables from your json key:

AUTH_ISS — Field ‘client_email’

AUTH_AUD — Field ‘token_uri’

AUTH_TOKEN — Field ‘private_key’

In the snippet above, we are forming a JWT from the various components required, including signing the section. Note we have set an expiry date of 300 seconds time. Having an expiry is standard practice for these tokens, and just adds a level of security.

We use the JWT to request an access token from Google. Then checking the response is a success and using jq to retrieve the token.

Creating an Edit

First we create our post data, consisting of a JSON of an id and an expiry. You can pass any id you want, it should just be unique. We use our build number (passed into the script) to ensure uniqueness, whilst also providing traceability. The expiry defines how long you would like the edit to stay open, before automatically deleting. This helps to clear edits out which have failed.

Then we make our POST request to googleapis.com/androidpublisher/v3/applications/$PACKAGE_NAME/edits, capturing the results. We then check for success and retrieve the id. We use the id received from the API to ensure we have the correct one, although it should be identical to the id we posted.

Note you will need your apps package name. This can be retrieved from your apk, using the aapt tool from the Android SDK build tools.

CircleCI comes with multiple versions of build tools, so in this snippet we grab aapt from the latest one.

Uploading the APK

Here we post our apk file to googleapis.com/upload/androidpublisher/v3/applications/$PACKAGE_NAME/edits/$EDIT_ID/apks using the id of our edit to associate it.

Assign Edit to Track

We create our post JSON and call googleapis.com/androidpublisher/v3/applications/$PACKAGE_NAME/edits/$EDIT_ID/tracks/$PLAYSTORE_TRACK. Again checking for success.

You will need the following variables:

PLAYSTORE_TRACK - One of “alpha”, “beta”, “production”, “rollout” or “internal”

VERSION_CODE - You can retrieve the version code from your apk

STATUS - One of “completed”, “draft”, “halted”, “inProgress”. Halted and inProgress are for staged rollouts.

Commit your Edit

We simply post to googleapis.com/androidpublisher/v3/applications/$PACKAGE_NAME/edits/$EDIT_ID:commit.

If you now head over to the PlayStore console you should see your deployment in the console.

All together

We have put all the various pieces together into a single script, expecting certain variables to be passed in. To call this from CircleCI:

We put this script in the .circleci folder of our repository. We have stored our service account key in environment variables, named PLAYSTORE_SERVICE_KEY.

AAB

Summary

Helpful links

https://developers.google.com/identity/protocols/OAuth2ServiceAccount#callinganapi

https://developers.google.com/android-publisher/edits/#workflow

Also checkout

Previous Post

Selectable Cluster Map

Originally published at www.brightec.co.uk.

Mobile app developer, focusing on Android. Working at Brightec (www.brightec.co.uk). Links: www.github.com/alistairsykes www.twitter.com/SykesAlistair

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store