Creating a signed APK file using Cordova- command line interface


Step 1: browse to your application directory and run command

root@cheeyu:/home/user/application# cordova plugin rm org.apache.cordova.console --save

add the --save so that it removes the plugin from the config.xml file

Step 2:

To generate a release build for Android, we first need to make a small change to the AndroidManifest.xml file found in platforms/android. Edit the file and change the line:

<application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">

and change android:debuggable to false: like below

<application android:debuggable="false" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">

It’s best to leave out the android:debuggable attribute from the manifest. If you do, then the tools will automatically insert android:debuggable=true when building an APK to debug on an emulator or device. And when you perform a release build, such as Exporting APK, it will automatically set it to false.

Step 3: generate our release build:

root@cheeyu:/home/user/application# cordova build --release android

Now you have the unsigned APK file in platforms/android/build/outputs/apk/android-release-unsigned.apk

Step 4:

Key Generation:

Syntax:

keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>

Egs:

keytool -genkey -v -keystore NAME-mobileapps.keystore -alias NAMEmobileapps -keyalg RSA -keysize 2048 -validity 10000

keystore password? : xxxxxxx
What is your first and last name? :  xxxxxx
What is the name of your organizational unit? :  xxxxxxxx
What is the name of your organization? :  xxxxxxxxx
What is the name of your City or Locality? :  xxxxxxx
What is the name of your State or Province? :  xxxxx
What is the two-letter country code for this unit? :  xxx

Then the Key store has been generated with name as NAME-mobileapps.keystore

Note down password. Keep password note file and keystore file safely. For all future versions of app you must have to use this keystore. Other wise you will not be able to release future versions of app which are signed with different keystore. All app versions must be signed with same keystore.

Step 5:

Place the generated keystore in

old version cordova

root@cheeyu:/home/user/application/platforms/android/ant-build

New version cordova

root@cheeyu:/home/user/application/platforms/android/build/outputs/apk

To sign the unsigned APK, run the jarsigner tool which is also included in the JDK:

Syntax:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename <Unsigned APK file> <Keystore Alias name>

root@cheeyu:/home/user/application/platforms/android/build/outputs/apk> jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore NAME-mobileapps.keystore Example-release-unsigned.apk xxxxxmobileapps

Enter KeyPhrase as 'xxxxxxxx'

This signs the apk in place.

in case this step throwing a warning “No -tsa or -tsacert is provided and this jar is not timestamped.”. Fix it by adding “-tsa timestamp.digicert.com” to the command.

Step 6:

Finally, we need to run the zip align tool to optimize the APK:

root@cheeyu:/home/user/application/platforms\android\ant-build> zipalign -v 4 Example-release-unsigned.apk Example.apk

Now we have our final release binary called example.apk and we can release this on the Google Play Store.

Leave a comment