Android Play Store uploads from the command line

Having sorted out command line builds for a Unity3d Android application, I wanted more. Here’s the procedure to upload new builds from the Linux command line. I’ve also tried to arrange it so that in the future I’ll be able to use the same source tree to build iOS versions and install those too.

The Android Play store has an API that can be used to upload builds and store assets. I have no idea what the API looks like because there’s a package available called Fastlane that means I don’t need to know. In fact there’s not a lot that has to be done to setup command line Play store uploads.

Fastlane is a collection of sub-projects. the one I use for uploading to the Play store is called supply. The documentation for it is here. https://github.com/fastlane/fastlane/tree/master/supply#readme

The Fastlane documentation doesn’t go into much detail about what happens when you have multiple projects in your Play store account or how to upload a build that only your alpha testers can use. I needed both of these as I want to quickly upload things for the other team members to be able to try. I also want to be able to build for iOS from the same Unity project in the future, I think I’ve taken account of that.

I have a project in the play store called testapp because the app store needs to know all kinds of things like pricing that we haven’t decided on yet. When we do, we’ll set up the real project in the Play store.

So, without further waffling, here’s how to make Fastlane do the uploading. The first step is to install Fastlane. It’s a Ruby Gem, so the command line to install it is.

sudo gem install fastlane

Which results in an error!
Building native extensions. This could take a while…
ERROR: Error installing fastlane:
ERROR: Failed to build gem native extension.

I’m using Ubuntu Linux and apparently there’s a dependency missing, but from the error above I couldn’t figure it out.

sudo gem install fastlane --verbose

Resulted in this more verbose error message.
Building native extensions. This could take a while…
current directory: /var/lib/gems/2.3.0/gems/unf_ext-0.0.7.4/ext/unf_ext
/usr/bin/ruby2.3 -r ./siteconf20170913-3793-a3i1yx.rb extconf.rb
mkmf.rb can’t find header files for ruby at /usr/lib/ruby/include/ruby.h
ERROR: Error installing fastlane:
ERROR: Failed to build gem native extension.

Which I pasted into Google and got this solution…
Debian distributions need this…

sudo apt-get install ruby-dev

or this for Ubuntu.

sudo apt-get install ruby-all-dev

I didn’t read it properly, so I did both, but I expect just the ruby-all-dev one would get the first one anyway.

Now the tricky part.
Follow the Fastlane supply instructions to set up the google developer keyfile. These instructions are a bit complex at times, but have enough detail to get you through it perfectly. The only thing I found missing was that the default filename for the downloaded key has spaces in and the Fastlane software can’t handle spaces in the path. No problem, I just re-named the key file.

Then to initialize the project, just change directory to the project root folder and type

fastlane supply init

This will ask you for the key file and the bundle name. The name it needs is in com.company.product format. You can find the correct text the end of the URL when you click the project in the Play store console.

This will create a metadata folder with the store assets inside. This is the same name it would use for the Apple App Store, so I re-named it as follows.

mv metadata metadataAndroid

Now you can upload an APK with this simple command

fastlane supply --apk Android/testapp.apk --track alpha --json_key GooglePlayDeveloperKey.json --package_name com.companyname.productname --metadata_path metadataAndroid

The APK automatically goes gets rolled out to the track you chose, alpha in my case, so there’s no need to go to the Play store web site at all.

You can stick that command in an executable script file for convenience as typing that lot is an annoying as having to go through the play store web interface.

You could also add the command line to your build script, but I prefer the extra control I get from having two commands. I’ll be looking into adding both commands as Jenkins jobs in the future to automatically build and upload after every checkin. I’ll blog about it when I have it working.

 

One Reply to “Android Play Store uploads from the command line”

Leave a Reply

Your email address will not be published. Required fields are marked *