How to generate Android APK for Ionic project
Apk is android application package file format which is used by android operating system to distribute and install app. Apk file is used for publishing mobile application to google play store. Users never see the APK files because Android handles app installation in the background via Google Play or another app distribution platform.
Generate apk file from ionic project:-
Ionic is an HTML5 mobile app development framework which is used for building hybrid mobile apps. Ionic uses familiar and modern web technologies HTML5, CSS (Sass), and
JavaScript( Angular Js). Ionic mainly focuses on the look, feel and UI interactions of an app.
Below are the steps to generate APK file from Ionic code:
1) Firstly we need to download and install the Java JDK.
2) Android SDK:-
We need to install android studio with acceptance of the licence. If we don’t want install complete Android Studio then we can just download the Android SDK files for Windows here – https://developer.android.com/studio/index.html#downloads. If we install the Android SDK without Android Studio then we need to install Gradle separately. Download Gradle from the official Gradle website.
3) Environment Variables:-
We need to set environment variables of java home, android home, gradle home. In window most of the time it automatically sets path during installation but we need make sure that environment variables are set correctly.
ionic cordova build --release android // command is used for android.
This command will generate unsigned APK file in platforms/android/build/outputs/apk. Sometimes unsigned APK file works in android device and sometime not. For publishing app to google play store APK file should be signed. To sign the unsigned APK we need a signing key (private key). This key will be generated by keytool command that comes with the JDK (Java development kit)
$ keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
After executing this command it will ask you to create the password for keystore and will also ask some other questions as well. Same password needs to be entered during signing command.
If JDK is configured properly then this command will generate a private key file.
Make sure to save this file somewhere safely, if you lose it you won’t be able to submit updates to your app.
To sign the unsigned APK, jarsigner tool need to run which is also included in the JDK.
$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore apkfileName.apk alias_name< //This command needs to execute inside the java installed path in the operating system and that private key file also need to placed inside the java installed path. E.g:- C:\Program Files\Java\jdk1.8.0_181\bin
This command signs the apk in place. Now, we need to run the zipalign tool to optimize the APK. Make sure we have the Android SDK build-Tools installed then check where your SDK is installed
E.g:- C:\Program Files(x86)\Android\android-sdk\build-tools\23.0.1\zipalign
$ zipalign -v 4 Myapp-release-unsigned.apk MyappName.apk // Apk filename can be anything as per the user choice.
Now we have our final release binary called MyappName.apk and we can release this on the Google Play Store for others to use this application.