Using the command line to create a production android release for a cordova application

Creating a signed production android release or APK for your application is normally be done by using Google's Android Studio or with Eclipse and ADT. It seems a inefficient to fire up a whole IDE just to build a release for an application as cordova applications are normally created using other web development editors, so this guide will detail how to go about this task using only the command line.

This guide presumes you have a running android cordova application.

First build your application as usual:

cordova build android

This will build a unsigned APK which can be uploaded to emulators or devices via ADB or similar for initial testing.

You will need to create a new java keystore if you do not have one already (maybe for other java projects). This keystore will contain the private key and certificate that will be used to sign your application and ensure future versions are from the same author.

Creating a new private key and certificate

Warning Don't forget the password entered below, otherwise you will be no longer able to update your application in the play store.

Use the command below to create a new keystore and certificate, the alias used is generally the name of your application.

keytool -genkey -v -keystore android_app.keystore -alias myname -keyalg RSA -keysize 2048 -validity 10000
Enter keystore password:  
Re-enter new password: 
What is your first and last name?
  [Unknown]:  Bob Jones
What is the name of your organizational unit?
  [Unknown]:  Development
What is the name of your organization?
  [Unknown]:  The Dumb Terminal
What is the name of your City or Locality?
  [Unknown]:  Some Town
What is the name of your State or Province?
  [Unknown]:  Some State
What is the two-letter country code for this unit?
  [Unknown]:  GB
Is CN=Bob Jones, OU=Development, O=The Dumb Terminal, L=Some Town, ST=Some State, C=GB correct?
  [no]:  yes

Generating 2,048 bit RSA key pair and self-signed certificate (SHA1withRSA) with a validity of 10,000 days
    for: CN=Bob Jones, OU=Development, O=The Dumb Terminal, L=Some Town, ST=Some State, C=GB
Enter key password for <myname>
    (RETURN if same as keystore password):  
[Storing android_app.keystore]

Update build settings

Next, append the keystore details to your android platform ant properties, as shown in the example below:

more platforms/android/ant.properties 
key.store=/Users/bob/myname/android_app.keystore
key.alias=myname

Increase the version number in www/config.xml, see Configuring Cordova to update the Android version code for details.

Adjust any android manifest details first in platforms/android/AndroidManifest.xml, settings such as android:allowBackup or any specific screen size requirements.

Build the production release

Now to build the signed release APK ready for the play store:

cordova build android --release

....

-post-package:

-release-prompt-for-password:
    [input] Please enter keystore password (store:/Users/bob/myname/android_app.keystore):
droid-store
    [input] Please enter password for alias 'myname':
droid-store

-release-nosign:

-release-sign:
     [echo] Signing final apk...
 [zipalign] Running zip align on final apk...
     [echo] Release Package: /Users/bob/myname/platforms/android/ant-build/CordovaApp-release.apk
[propertyfile] Updating property file: /Users/bob/myname/platforms/android/ant-build/build.prop
[propertyfile] Updating property file: /Users/bob/myname/platforms/android/ant-build/build.prop
[propertyfile] Updating property file: /Users/bob/myname/platforms/android/ant-build/build.prop
[propertyfile] Updating property file: /Users/bob/myname/platforms/android/ant-build/build.prop

-post-build:
     [move] Moving 1 file to /Users/bob/myname/platforms/android/ant-build
     [move] Moving 1 file to /Users/bob/myname/platforms/android/CordovaLib/ant-build

release:

BUILD SUCCESSFUL
Total time: 47 seconds
Built the following apk(s):
    /Users/bob/myname/platforms/android/ant-build/CordovaApp-release.apk
    /Users/bob/myname/platforms/android/ant-build/CordovaApp-release-unsigned.apk

Testing

Before uploading to the play store, its best to test the new APK in real devices once it has been connected via USB.

First remove any previous development versions of your application from the device, otherwise you will receive a certificate error upon installation of the new signed APK.

Next install the new signed APK to the device by uploading it with the following command:

adb install platforms/android/ant-build/CordovaApp-release.apk  

Last updated: 08/04/2015