I made a sample application named checkStatus
. Now I want to create a signed APK file. So I can install it in different devices for my testing.
For this, I Googled and found this documentation.
As per the document, I switched to my project directory and ran the following command:
keytool -genkey -v -keystore key-name.keystore -alias alias-name -keyalg RSA -keysize 2048 -validity 10000
After I ran the above command, I got a file named key-name.keystore
at projectRoot/key-name.keystore
.
And then I copy-pasted that file into projectRoot/platforms/android/key-name.keystore
.
After that, I created a file named ant.properties
and saved it in projectRoot/platforms/android
.
I wrote the following code inside the file:
key.store=projectRoot/key-name.keystore
key.alias=myApp
After that, I ran the following command to release
Cordova builds android --release
It's throwing the following error:
/home/projectRoot/platforms/android/cordova/node_modules/q/q.js:126
throw e;
^
Error code 1 for command: ant with args: release,-f,/home/projectRoot/platforms/android/build.xml,-Dout.dir=ant-build,-Dgen.absolute.dir=ant-gen
Error: /home/projectRoot/platforms/android/cordova/build: Command failed with exit code 8
at ChildProcess.whenDone (/usr/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:135:23)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:753:16)
at Process.ChildProcess._handle.onexit (child_process.js:820:5)
So this time, I modified key.store
value in ant.properties
file like in the following way.
key.store=/home/projectRoot/platforms/android/key-name.keystore
Again, I ran the cordova build android --release
command. It throws the same error.
Can anyone tell me what I've done wrong?
First Check your version code and version name if you are updating your app. And make sure you have a previous keystore.
If you are updating app then follow step 1,3,4.
Step 1:
Goto your cordova project for generate our release build:
Then, we can find our unsigned APK file in platforms/android/ant-build. In our example, the file was
if u used ant-build
OR
if u used gradle-build
Step 2:
Key Generation:
Syntax:
if keytool command not recognize do this step
Check that the directory the keytool executable is in is on your path. (For example, on my Windows 7 machine, it's in C:\Program Files (x86)\Java\jre6\bin.)
Example:
Then the Key store has been generated with name as NAME-mobileapps.keystore
Step 3:
Place the generated keystore in D:\projects\Phonegap\Example\platforms\android\ant-build
To sign the unsigned APK, run the jarsigner tool which is also included in the JDK:
Syntax:
If it doesn't reconize do these steps
(1) Right click on "This PC" > right-click Properties > Advanced system settings > Environment Variables > select PATH then EDIT.
(2) Add your jdk bin folder path to environment variables, it should look like this:
"C:\Program Files\Java\jdk1.8.0_40\bin".
Example:
This signs the apk in place.
Step 4:
Finally, we need to run the zip align tool to optimize the APK:
if zipalign not recognize then
(1) goto your android sdk path and find zipalign it is usually in android-sdk\build-tools\23.0.3
(2) Copy zipalign file paste into your generate release apk folder usually in below path
yourproject/platforms/android/ant-build/Example-release-unsigned.apk
OR
Now we have our final release binary called example.apk and we can release this on the Google Play Store.
Build cordova release APK file in cmd.
KEY STORE FILE PATH: keystore file path (F:/cordova/myApp/xxxxx.jks)
KEY STORE PASSWORD: xxxxx
KEY STORE ALIAS: xxxxx
KEY STORE ALIAS PASSWORD: xxxxx
PATH OF zipalign.exe: zipalign.exe file path (C:\Users\xxxx\AppData\Local\Android\sdk\build-tools\25.0.2\zipalign)
ANDROID UNSIGNED APK NAME: android-release-unsigned.apk
ANDROID RELEASE APK NAME: android-release.apk
Run below steps in cmd (run as administrator)
For Windows, I've created a
build.cmd
file:(replace the keystore path and alias)
For Cordova:
And for Ionic:
Save it in the ptoject's directory, you can double click or open it with cmd.
On Mac (osx), I generated two .sh files, one for the first publication and another one for updating :
And to update your app:
Assuming you're in your home folder or a folder topping your apps folders. Make sure to set correctly chmod to use this script. Then :
Your signed apk will be in Your App folder/platforms/android/build/outputs/apk/ as SignedApk.apk Make sure to use the correct key alias and password defined with the first script
Step1:
Go to
cordova\platforms\android
ant create a fille calledant.properties
file with the keystore file info (this keystore can be generated from your favorite Android SDK, studio...):Step2:
Go to cordova path and execute:
Note: You will be prompted asking your keystore and key password
An YourApp-release.apk will appear in
\cordova\platforms\android\ant-build