Ionic Tutorial – Series 3/3
Deploying to a Device
Testing your app in the browser with Ionic serve, or with an emulator, is simple, fast, and pretty convenient while your app is in development, however at some point you will have to test it on a device. This is the only way you can test your app accurately, including how it behaves and performs. A lot of Ionic Native plugins are going to work after they are ran on an actual hardware.
When deploying to an Android device, it is quite a straightforward procedure. If you own a working Android development environment, you are ready to go.
- Java JDK
- Android Studio
- Make sure you have updated Android SDK tools, platform and component dependencies. Available through Android Studio’s SDK Manager
Running Your App
In order to run your app, all you need to do is simply enable USB debugging and Developer Mode on your Android device. Afterwards, execute ionic Cordova run ‘android –device’ from the command line.
This should spawn a debug build of the application, both in terms of Android and Ionic’s code.
When you enable USB debugging and Developer Mode, it can vary between devices, but it is easy to search using Google.
For you to run or build your application for production, execute the following below.
ionic cordova run android --prod --release # or ionic cordova build android --prod –release
This should minify your app’s code as Ionic’s source as well as deleting any debugging capabilities from the APK. This is usually used after deploying an app to the Google Play Store.
Sign Android APK
If you would like to release your app in the Google Play Store, you will need to sign your APK file. In order to do this, you must create a new certificate/keystore.
You can generate a private key with the ‘keytool’ command which comes with the JDK.
keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias
You should be asked to create a password for the keystore and, afterwards, you will need to answer the rest of the nice tool’s questions. After this is all done, you will have a file named ‘my-release-key.jks’ in the current directory.
Note: Be sure you are saving this file in a safe place; if you lose it then you will lose the ability to submit updates to the application.
To sign the unsigned APK, use the ‘jarsigner’ tool as it is also within the JDK.
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.jks android-release-unsigned.apk my-alias
This should sign the APK in place.
Lastly, you will have to run the zip align tool in order to optimize the APK.
The zip align tool could be found in ‘/path/to/Android/sdk/build-tools/VERSION/zipalign’.
As an example, on OS X with Android Studio implemented, zipalign is located in the following.
zipalign -v 4 android-release-unsigned.apk HelloWorld.apk
Make sure you verify that your APK is signed.
The apksigner can also be found in the same path as the zipalign tool: apksigner verify HelloWorld.apk. You now have your final release binary called HelloWorld.apk and can release this on the Google Play Store for everyone in the world to enjoy.