Deploying an Ionic 4 app to Play Store

on

This blog post is more of a reminder for myself on what the necessary steps for deploying an Ionic 4 app to Play Store are than it is a real tutorial. A lot of documentation on this subject already exists, including Ionic’s official documentation that is thorough and detailed. However, there are a few tips and tricks that I would like to have written down, as we have an app where new versions come out once every 3-6 months, and I forget these each time.

Quick intro – what is Ionic?

Ionic Framework is an open source UI toolkit for building performant, high-quality mobile and desktop apps using web technologies (HTML, CSS, and JavaScript).

Ionic Framework is focused on the frontend user experience, or UI interaction of an app (controls, interactions, gestures, animations). It’s easy to learn, and integrates nicely with other libraries or frameworks, such as Angular, or can be used standalone without a frontend framework using a simple script include.

Ionic was initially built on top of Angular alone, however, nowadays it readily integrates easily with React as well, and support for Vue.js is coming really soon. Ionic is built on top of Cordova which is an open-source mobile development framework which allows you to use HTML, CSS and JS to build cross-platform mobile apps.

Ionic

In this project we are using Angular and Ionic to build a mobile app that needs to run on Android devices.

Building your Ionic app for production

I will assume here that you have all the perquisites set up and have already set up everything to build the application, so we can just start with the deployment. There are three main steps you need to do:

  1. Build your application for production
  2. Sign your APK
  3. Use the Play Store developer interface to upload the application

1. Building your application for production

In order to build your application for production, you need to run the following command:

ionic cordova build android --prod --release

You will see a lot of output into your prompt and in the end you will get your .apk file in the following folder:

{{project-folder}}\platforms\android\app\build\outputs\apk\release

If you are like me and have not changed any settings to your file, then the .apk file that you are looking for will be called app-release-unsigned.apk and this is fine, you do not need to rename it to anything “memorable”, we will do this with the signed file to be published.

2. Signing your APK

I always create a folder for my deployment. This folder will contain some stuff connected with this particular app that I prefer to keep (not sure if I will ever need to use it, but I like having it).

Setting up a private key

If this is your first build, you need to generate a private key for this app. IMPORTANT! You must not lose your private key. Ever. EVER! You must not forget the password you set when creating your private key. Ever. EVER! If you do this, according to all docs, it will be impossible for you to ever upload a new version of your app to the Play Store.

Do not lose your password

To generate a private key, use the following command:

keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias

The keytool utility should come with the JDK, and if you have it set up properly, it should be available due to the PATH settings in your environment. If, for whatever reason, it is not, you should be able to find it here:

{{Java JDK folder}}\bin
(on my Windows system it's C:\Program Files\Java\jdk1.8.0_202\bin)

When you run this command, you will be prompted for a password. Make sure to save this password really reliably – you must never lose it!!! Also, this will generate a file for you, the my-release-key.jks. Do not lose this one as well!!!

What I do is I use a .txt file that I place in my deployment folder that says:

Applicaton: My Application
Release key file: my-release-key.jks
Password for my-release-key.jks: MyPassword123
Alias I have used with my-release-key.jks: my-alias

And I save this file together with the private key crypted in a couple of safe locations.

Signing the APK

So, you now have the private key and are ready to sign your .apk file. This is done using the jarsigner utility. This should automatically be in your path. If, for some reason, it is not, you can find it in the same folder you found your keytool utility.

Use the following command to sign your APK:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.jks app-release-unsigned.apk my-alias

Do note that this will prompt you for the password you initially set, and that, if you do not know it, you cannot complete this step!

Also, do note that you need to use the same alias you used when you set up the private key. If, however, like me, you forgot this alias – do not worry. There is a way to get this one back. If the alias is wrong, you should get the:

Certificate chain not found for: my-wrong-alias. my-wrong-alias must reference a valid KeyStore key entry containing a private key and corresponding public key certificate chain.

I have found a way to recover the alias on this StackOverflow thread. You basically need to use the keytool utility again to get info on the private key. This info includes the alias (somewhere close to the top). The command you need to run is:

keytool -keystore my-release-key.jks -list -v

After successfully running this, your app-release-unsigned.apk file should be signed (“Isn’t it ironic?”, as Alanis would say).

Optimizing the APK

In the end it is necessary to run the zipalign tool to optimize the APK. The zipalign tool comes with Android Studio. It is actually a stand-alone tool, and I could put it for download here, but no one wants to download an .exe file from a random site on the Internet and run it.

Normally it can be found in:

 {{AndroidPath}}\sdk\build-tools\VERSION\

On my Windows setup I found it in:

 C:\Users\{{user}}\AppData\Local\Android\Sdk\build-tools\VERSION\

I always copy it to the folder I made for deployment, just so I have it handy at any time, but this is not necessary. The zipalign command you need to run is:

zipalign -v 4 app-release-unsigned.apk MyApplication-ver001.apk

Do note that this utility actually creates a new APK file and that here it is practical to actually give it a name that makes sense for you to use it later.

And this is the tough part, now you have a file that you will just upload into the Google Developer Console and follow the guide.

3. Play Store developer interface

Go to the Google Play Console site.

Google Play Console

Once you log in, you will see a list of all your existing apps. Choose the one you are working on (or set up a new one if you are doing this for the first time).

You can set up a bunch of stuff here, like descriptions, translations, screenshots, app videos, manage reviews etc. I will not go into all of that here. To upgrade a version you need to go to the app, then follow the wizard to the place where you can upload the new APK file.

Anytime you are publishing a new version you need to add notes on what’s new and, as someone who actually likes to read these now and then, I urge you to actually try and make these descriptive.

And that’s it. After publishing it will take Google some time to verify the app is OK and to get it to the actual Play Store, but this is their process we cannot influence.

Good luck publishing your apps!

Leave a Reply

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