The following topics will be discussed in this post.
- Step 1: Packaging PhoneGap application files into an APK
- Step 2: Gradle building to produce the unsigned APK
- Step 3: Generating a key store file
- Step 4: Signing the APK
- Step 5: Verify the signed APK
- Step 6: Generating the aligned final signed APK for the play store
- You're done
If you have reached this post, either by a Google search or a referral link, then you are probably a hybrid mobile application developer who's looking to publish his PhoneGap app to the play store.
You might have also experienced that PhoneGap is a super easy framework that makes mobile apps' development a very easy task, despite all the drawbacks of having a mobile application running on a web view.
The good news is: Packaging the PhoneGap application to Google play store is even much easier.
In this post, I will assume you have finished your mobile application's design and development and ready to publish it to the store. I am going to walk you through the publishing process step by step starting with your PhoneGap application.
The first part of the process is to convert your PhoneGap application files (HTML, JS, CSS and media files like images) to a packaged Android APK file.
You are going to use the PhoneGap CLI to build and package the target signed apk (discussed later). If you haven't installed the PhoneGap CLI yet, make sure you install the following:
- NodeJS from NodeJS site
- PhoneGap CLI (Command Line Interface) by running the following command:
Step 1: Packaging PhoneGap application files into an APK
Assuming you are working on a Windows environment, open a command prompt and go to the PhoneGap application's directory, for example:
where C:\\PhoneGap\App is your PhoneGap application's main directory, then run the following command that will build the PhoneGap application and produce the Android debug APK version:
The build command would produce the debug version of the apks as well as creating some other necessary gradle build files if they are not yet created. To make sure that this command worked as expected, you would see the command result on the command prompt as shown in the following screenshot.
Also, if you browsed on your file explorer to the folder path: App\platforms\android\build\outputs\apk You would find a couple of files have been created. Android-debug.apk and android-debug-unaligned.apk.
Step 2: Gradle building to produce the unsigned APK
The second step is to create the unsigned APK file that would be used in the subsequent signing steps. On your command prompt, browse to the platforms\android folder as follows:
Then type in the following command to produce the unsigned APK:
The above build command would produce a third APK file called android-release-unsigned.apk.
Note: Ignore any build failure messages as long as you find that third new APK unsigned file.
Step 3: Generating a key store file
In order to be able to upload the APK produced in step 2 to the play store, it must be signed first. To get the APK signed, you must use a file called the key store file. If you have created one then skip to Step 4, if not then run the following command to create one:
The above command will result in asking you some questions on the command prompt. Keep answering those questions until you reach the password question, keep that password along with the alias you provided in the previous command saved somewhere to be able to use it in the future because signing the APK process will ask you for that password.
Once you finished, you should see your key store file generated. It's a file with a .keystore extension.
Step 4: Signing the APK
In this step, you would sign the unsigned APK generated in Step 2 with the key store file generated in Step 3. First, move the key store file to the path where the android-release-unsigned.apk is placed in your android PhoneGap application. Usually the apk is saved in C:\\<your_phonegap_app_path\platforms\android\build\outputs\apk.
We moved the key store file to where the unsigned APK is to make it easier dealing with file paths and command errors due to invalid ones. Now sign the apk by running the following command
You'd be asked for the password you saved in Step 3. Then you should find a success message as "jar signed" and you can ignore the warning message if any.
Step 5: Verify the signed APK
The next step is to verify the signed APK by running the following command
After running this command you should get a success message as "jar verified" and you can also ignore any warning messages.
Step 6: Generating the aligned final signed APK for the play store
The final step is to generate the APK that you will use to upload to the play store. That one would be generated by running the following command:
Where FinalApk.apk is the filename of the APK that you will upload to the play store. You can, of course, put the name that you'd prefer.
Now you have got the APK that you will be able to use and publish on the play store for alpha testing, beta testing or production. I hope this post helped you with the build and signing process, and feel free to leave me comments if you have any questions.
What You Should Read Next
The following articles are related to 6 commands to publish phonegap apps to playstore.
The complete guide to migrating an existing Cordova application to a Cordova project on Visual Studio 2017 with building and running the app in 3 steps
A step-by-step quick guide on how to allow a PhoneGap android application to upgrade new versions without Google play store or any auto-update plugins.