In order to submit an app to the Google Play Store, you need to sign the apk no matter what development framework you're using. In this article, we'll learn how to sign an apk using Cordova. Before starting we will assume that we have previously made all the necessary Cordova installations and added an android platform for our project.
Create a keystore file
First, we'll need to create the keystore file which is basically a binary file that can hold a set of keys. To create this file you'll need to go to the root of your project and run the command below:
keytool -genkey -v -keystore myapp.keystore -alias myappalias -validity 10000
TheĀ
validity
Ā tag stands for the expiration of the key. After running this command you will see theĀ myapp.keystore
Ā file at the root of your project.Create a build.json file
TheĀ
build.json
Ā file also needs to be created at root level and this is the place where we will store all the parameters needed for signing. The file needs to look something like this:{
"android": {
"debug": {
"keystore": "./myapp.keystore",
"storePassword": "pass",
"alias": "myappalias",
"password" : "pass",
"keystoreType": "jks"
},
"release": {
"keystore": "/myapp.keystore",
"storePassword": "pass",
"alias": "myappalias",
"password" : "pass",
"keystoreType": "jks"
}
}
}
Set version and package name
Next, we need to do is open theĀ
config.xml
Ā file at root level where we can set the version, version code, and package name in the widget
Ā tag at the top of the file.<widget android-versionCode="10" defaultlocale="en-US" id="com.example.myapp" version="1.0" ...>
Ā is used only for calculations by Google Play and is not visible for usersversionCode
Ā is the package name of your appid
Ā is the version of your app that will be visible on Google Playversion
Building the app
The last thing that we want to do is build the app using the parameters that we defined inĀ
build.json
. We will achieve that by adding the flag --buildConfig
Ā to the build command:cordova build android --release --buildConfig
This command will build a signed apk that you can upload to Google Play. The location of the apk will be written as an output of the command, but the correct location should
beĀ platforms/android/app/build/outputs/apk/release
.For more details on the installation, building, and signing you can go to the officialĀ Cordova Android Platform Guide
Previously published at https://blog.codechem.com/signing-android-apk-with-apache-cordova