Step by step guide create apk for your ionic framework project

Programmer Gamer Classic White Coffee Mug

I just learn to use ionic framework and I like it since I can use my knowledge in html and css a bit. The process also push me to learn about angular js. As the process getting fun each day I got litttle problem with create apk process (part of publishing). Specially in sign step. I try to recreate it by make simple ‘blank’ project.

Here’s my environment :

$ ionic info

Your system information:

Cordova CLI: 5.3.1
Gulp version:  CLI version 3.9.0
Gulp local:
Ionic CLI Version: 1.6.5
Ionic App Lib Version: 0.3.9
OS: Distributor ID:    Peppermint Description:    Peppermint Five
Node Version: v4.0.0

Create the project

$ ionic start test blank

$ cd test/

$ ionic platform add android

Start process build apk

$ cordova plugin rm cordova-plugin-console

$ cordova build –release android

…………….

……………………

……………………………

BUILD SUCCESSFUL

Total time: 13.138 secs
Built the following apk(s):
/home/alamsyah/test/platforms/android/build/outputs/apk/android-release-unsigned.apk

Generate Private Key

I use this convention :

keytool -genkey -v -keystore appname.keystore -alias appnamekey -keyalg RSA -keysize 2048 -validity 10000

$ keytool -genkey -v -keystore test.keystore -alias testkey -keyalg RSA -keysize 2048 -validity 10000
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]:  alamsyah rasyid
What is the name of your organizational unit?
[Unknown]:  digitalfusi
What is the name of your organization?
[Unknown]:  digitalfusi
What is the name of your City or Locality?
[Unknown]:  palembang
What is the name of your State or Province?
[Unknown]:  sumsel
What is the two-letter country code for this unit?
[Unknown]:  id
Is CN=alamsyah rasyid, OU=digitalfusi, O=digitalfusi, L=palembang, ST=sumsel, C=id correct?
[no]:  yes

Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 10,000 days
for: CN=alamsyah rasyid, OU=digitalfusi, O=digitalfusi, L=palembang, ST=sumsel, C=id
Enter key password for <testkey>
(RETURN if same as keystore password):
[Storing test.keystore]

Sign apk file

1st error :

$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore test.keystore android-release-unsigned.apk test
Enter Passphrase for keystore:
jarsigner: Certificate chain not found for: test.  test must reference a valid KeyStore key entry containing a private key and corresponding public key certificate chain.

note : I don’t use absolute path for this command.

2nd error :

$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore /home/alamsyah/test/test.keystore android-release-unsigned.apk testkey
Enter Passphrase for keystore:
jarsigner: unable to open jar file: android-release-unsigned.apk

note : I type the command not in the same location of .apk file

$ cd platforms/android/build/outputs/apk/

retype the command

$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore /home/alamsyah/test/test.keystore android-release-unsigned.apk testkey
Enter Passphrase for keystore:
adding: META-INF/MANIFEST.MF
adding: META-INF/TESTKEY.SF
adding: META-INF/TESTKEY.RSA
signing: AndroidManifest.xml
signing: assets/www/cordova-js-src/android/nativeapiprovider.js
signing: assets/www/cordova-js-src/android/promptbasednativeapi.js
signing: assets/www/cordova-js-src/exec.js
signing: assets/www/cordova-js-src/platform.js
signing: assets/www/cordova-js-src/plugin/android/app.js
signing: assets/www/cordova.js
signing: assets/www/cordova_plugins.js
signing: assets/www/css/style.css
signing: assets/www/img/ionic.png
signing: assets/www/index.html
signing: assets/www/js/app.js
………………………………………………

………………………………………………………

signing: classes.dex
jar signed.

Warning:
No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate’s expiration date (2043-02-17) or after any future revocation date.

Optimize apk with zipalign

zipalign is part of jdk. In my case I already install it in /opt

$ pwd
/opt/android-sdk/build-tools/23.0.1
alamsyah@alamsyah-HP-Pavilion-14-Notebook-PC /opt/android-sdk/build-tools/23.0.1 $ ls
aapt  arm-linux-androideabi-ld  dexdump  i686-linux-android-ld  jill.jar  llvm-rs-cc      mainDexClasses.rules     NOTICE.txt    runtime.properties  split-select
aidl  bcc_compat                dx       jack.jar               lib       mainDexClasses  mipsel-linux-android-ld  renderscript  source.properties   zipalign

So the correct command is :

$ /opt/android-sdk/build-tools/23.0.1/zipalign -v 4 android-release-unsigned.apk  test.apk
Verifying alignment of test.apk (4)…
50 META-INF/MANIFEST.MF (OK – compressed)
3297 META-INF/TESTKEY.SF (OK – compressed)
6633 META-INF/TESTKEY.RSA (OK – compressed)
7806 AndroidManifest.xml (OK – compressed)
8974 assets/www/cordova-js-src/android/nativeapiprovider.js (OK – compressed)
9752 assets/www/cordova-js-src/android/promptbasednativeapi.js (OK – compressed)
……………..

……………………

………………………..

2295604 res/drawable-xhdpi-v4/icon.png (OK)
2302852 res/drawable-xxhdpi-v4/icon.png (OK)
2314932 res/drawable-xxxhdpi-v4/icon.png (OK)
2331845 res/xml/config.xml (OK – compressed)
2333704 resources.arsc (OK)
2337101 classes.dex (OK – compressed)
Verification succesful.

That is good sign that apk successfully optimize and ready for google play 🙂

ls -l | grep apk
-rw-rw-r– 1 alamsyah alamsyah 2402681 Okt  2 16:46 android-release-unsigned.apk
-rw-rw-r– 1 alamsyah alamsyah 2402707 Okt  2 16:50 test.apk