How to build publishable android apk in ionic through command line

Creating a publishable apk in ionic can be a very simple task if you follow the below steps.

  1. Open the Command Line at the root directory of your ionic app
  2. Run the following command to generate the release build of your ionic app
    ionic cordova build android --release
  3. This will generate a file “app-release-unsigned.apk” inside the directory “\platforms\android\app\build\outputs\apk\release“. Now, we have to sign the file with a key.
  4. Open the command line inside the directory “\platforms\android\app” and run the following command.
    keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
    Replace the “my-release-key.keystore” and “alias_name” with your desired names.
    Enter the required information like Password, First & Last name, Name of organizational unit, Name of organization, City, State, Country Code.
    This will generate a key file.
    Note: Store this key at some safe place. If you lose this key, you won’t be able to upload app updates.
  5. Now, we have the unsigned apk and the key. Next step is to sign the apk with the generated key. This will be done through “jarsigner” tool (which is included in JDK)
    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore build/outputs/apk/release/app-release-unsigned.apk alias_name
    It’s a good idea to rename the file now to “app-release-signed.apk” because now it’s signed.
  6. Finally, we need to optimize the apk using zip align tool. Zip align tool is available at “/path/to/Android/sdk/build-tools/VERSION/zipalign“. To avoid path errors, create a folder “apps” in specified directory i.e. ” /path/to/Android/sdk/build-tools/VERSION/” and copy the generated apk in step 5 in the created apps folder.
  7. Run the following command in directory ” /path/to/Android/sdk/build-tools/VERSION/ ” to zip align the apk.
    ./zipalign.exe -v 4 apps/app-release-signed.apk apps/app-release-signed-zipaligned.apk

That’s all. Now, you can upload the “app-release-signed-zipaligned.apk” file on google play store.

I would like to mention it again that you need to keep the key with you in order to upload app updates in future.