How to create a signed APK file using Cordova command line interface?
Solution 1
Step 1:
D:\projects\Phonegap\Example> cordova plugin rm org.apache.cordova.console --save
add the --save
so that it removes the plugin from the config.xml
file.
Step 2:
To generate a release build for Android, we first need to make a small change to the AndroidManifest.xml
file found in platforms/android. Edit the file and change the line:
<application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
and change android:debuggable
to false
:
<application android:debuggable="false" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
As of cordova 6.2.0 remove the android:debuggable tag completely. Here is the explanation from cordova:
Explanation for issues of type "HardcodedDebugMode": It's best to leave out the android:debuggable attribute from the manifest. If you do, then the tools will automatically insert android:debuggable=true when building an APK to debug on an emulator or device. And when you perform a release build, such as Exporting APK, it will automatically set it to false.
If on the other hand you specify a specific value in the manifest file, then the tools will always use it. This can lead to accidentally publishing your app with debug information.
Step 3:
Now we can tell cordova to generate our release build:
D:\projects\Phonegap\Example> cordova build --release android
Then, we can find our unsigned APK file in platforms/android/ant-build
. In our example, the file was platforms/android/ant-build/Example-release-unsigned.apk
Step 4:
Note : We have our keystore keystoreNAME-mobileapps.keystore
in this Git Repo, if you want to create another, please proceed with the following steps.
Key Generation:
Syntax:
keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>
Egs:
keytool -genkey -v -keystore NAME-mobileapps.keystore -alias NAMEmobileapps -keyalg RSA -keysize 2048 -validity 10000
keystore password? : xxxxxxx
What is your first and last name? : xxxxxx
What is the name of your organizational unit? : xxxxxxxx
What is the name of your organization? : xxxxxxxxx
What is the name of your City or Locality? : xxxxxxx
What is the name of your State or Province? : xxxxx
What is the two-letter country code for this unit? : xxx
Then the Key store has been generated with name as NAME-mobileapps.keystore
Step 5:
Place the generated keystore in
old version cordova
D:\projects\Phonegap\Example\platforms\android\ant-build
New version cordova
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk
To sign the unsigned APK, run the jarsigner tool which is also included in the JDK:
Syntax:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename> <Unsigned APK file> <Keystore Alias name>
Egs:
D:\projects\Phonegap\Example\platforms\android\ant-build> jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore NAME-mobileapps.keystore Example-release-unsigned.apk xxxxxmobileapps
OR
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk> jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore NAME-mobileapps.keystore Example-release-unsigned.apk xxxxxmobileapps
Enter KeyPhrase as 'xxxxxxxx'
This signs the apk in place.
Step 6:
Finally, we need to run the zip align tool to optimize the APK:
D:\projects\Phonegap\Example\platforms\android\ant-build> zipalign -v 4 Example-release-unsigned.apk Example.apk
OR
D:\projects\Phonegap\Example\platforms\android\ant-build> C:\Phonegap\adt-bundle-windows-x86_64-20140624\sdk\build-tools\android-4.4W\zipalign -v 4 Example-release-unsigned.apk Example.apk
OR
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk> C:\Phonegap\adt-bundle-windows-x86_64-20140624\sdk\build-tools\android-4.4W\zipalign -v 4 Example-release-unsigned.apk Example.apk
Now we have our final release binary called example.apk and we can release this on the Google Play Store.
Solution 2
An update to @malcubierre for Cordova 4 (and later)-
Create a file called release-signing.properties
and put in APPFOLDER\platforms\android
folder
Contents of the file: edit after = for all except 2nd line
storeFile=C:/yourlocation/app.keystore
storeType=jks
keyAlias=aliasname
keyPassword=aliaspass
storePassword=password
Then this command should build a release version:
cordova build android --release
UPDATE - This was not working for me Cordova 10 with android 9
- The build was replacing the release-signing.properties file. I had to make a build.json
file and drop it in the appfolder, same as root. And this is the contents - replace as above:
{
"android": {
"release": {
"keystore": "C:/yourlocation/app.keystore",
"storePassword": "password",
"alias": "aliasname",
"password" : "aliaspass",
"keystoreType": ""
}
}
}
Run it and it will generate one of those release-signing.properties
in the android folder
Solution 3
In the current documentation we can specify a build.json with the keystore:
{
"android": {
"debug": {
"keystore": "..\android.keystore",
"storePassword": "android",
"alias": "mykey1",
"password" : "password",
"keystoreType": ""
},
"release": {
"keystore": "..\android.keystore",
"storePassword": "",
"alias": "mykey2",
"password" : "password",
"keystoreType": ""
}
}
}
And then, execute the commando with --buildConfig argumente, this way:
cordova run android --buildConfig
Solution 4
Step1:
Go to cordova\platforms\android
ant create a fille called ant.properties
file with the keystore file info (this keystore can be generated from your favorite Android SDK, studio...):
key.store=C:\\yourpath\\Yourkeystore.keystore
key.alias=youralias
Step2:
Go to cordova path and execute:
cordova build android --release
Note: You will be prompted asking your keystore and key password
An YourApp-release.apk will appear in \cordova\platforms\android\ant-build
Solution 5
In cordova 6.2.0, it has an easy way to create release build. refer to other steps here Steps 1, 2 and 4
cd cordova/ #change to root cordova folder
platforms/android/cordova/clean #clean if you want
cordova build android --release -- --keystore="/path/to/keystore" --storePassword=password --alias=alias_name #password will be prompted if you have any
Related videos on Youtube
vasan
@Infratab - Front-end Developer, Like to Sketch a lot & love to solve front-end code issues.
Updated on December 03, 2021Comments
-
vasan over 2 years
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
atprojectRoot/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 inprojectRoot/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 inant.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?
-
Sombriks almost 9 yearsstarting with cordova 5, the process changes a little bit: ilee.co.uk/Sign-Releases-with-Cordova-Android/…
-
zeuf over 2 yearsboth links are dead.
-
-
cfprabhu about 9 yearsOkay I accept your thought. But I have private git repository and we have a team for work on mobile app. So it is comfortable for me.
-
Sombriks almost 9 yearsstarting with cordova 5, the process changes a little bit: ilee.co.uk/Sign-Releases-with-Cordova-Android/…
-
Shai UI over 8 yearsWhy was it important to remove org.apache.cordova.console?
-
cfprabhu over 8 yearsCordova.console plugin is used for debugging purpose. That why we have removed from production app. Because production app didn't need that. And if we removed that plugin it will be reduce the app size also.
-
Shah Abaz Khan over 8 yearsLooks a bit scary, but went through the steps with ease. Nice one! Also, latest cordova has changes in the release apk directory.
-
gustavohenke about 8 yearsAwesome <3 your answer is about files that I can version control!
-
Mario Orlandi about 8 yearsWith Cordova 5 name file "release-signing.properties" instead of "ant.properties"; all other steps are exactly as described by @malcubierre
-
Thomas Bormans about 8 years@cfprabhu Step 5 is throwing a warning "No -tsa or -tsacert is provided and this jar is not timestamped.". Fixed it by adding "-tsa timestamp.digicert.com" to the command. Might be useful for other users
-
Dunc about 8 yearsThanks! You can also change the name/location of your properties file, but you have to specify in a
build-extras.gradle
file. Relevant cordova doc here. -
Beelphegor about 8 yearsin which directory should this json exist?
-
shadi about 8 yearscordova build android --release
-
NGB about 8 yearsMy app is not getting update to next version, first version created in ant build(cordova 3). I upgraded my cordova to 6.0.0 now second version is created in gradle build, I followed the latest cordova doc to create signed apk but still my app not getting updated. I gone through the all the info of the web but still somewhere am going wrong. Please help me @Dunc
-
Dunc about 8 years@Naveen I'm using Cordova 5.2.0, had various problems with other versions > 5. Suggest you try that if you can. Then, if still having problems, I recommend asking a new question.
-
NGB about 8 yearsStill having problem, new version of apk is not getting downloaded from existing app. @Dunc
-
NGB about 8 yearsnew version of apk is not getting downloaded from existing app in cordova 6.0.0. I followed all the docs still not solved my issue can you help me @MarioOrlandi
-
NGB about 8 years@Dunc any solution
-
Dunc about 8 years@Naveen Not sure I can help, it's probably best if you start a new SO question detailing your specific issue: stackoverflow.com/questions/ask
-
NGB about 8 yearsI asked this question so many times and ended up in blocking stackoverflow, I can only comment here that's why asking like this. If you know something please help me@Dunc
-
AishApp almost 8 years@Jon. Thanks for the solution. But i am getting the apk with name android-release. Is there a way to name my apk? please help
-
Jon almost 8 yearsyou can rename the apk like any file in windows (etc). I just upload the xxx-relese.apk to google play, and all the encoded app name etc from manifest /config etc shows up on the play store. I am not sure if there is any difference as to the name of the actual apk.
-
Joel Caton almost 8 yearsPlace the build.json in the root project folder along with the keystore file. This is the most effective hassle free method for signing I've found.
-
Jon over 7 years
-
Dilhan Jayathilake over 7 yearsFor some reason the file path I had to changed to "../android.keystore". (forward slash)
-
laughingpine over 7 yearsWould again like to point out the docs. They outline a few different ways to sign, including gradle. cordova.apache.org/docs/en/latest/guide/platforms/android/…
-
chillwalker over 7 yearszipalign under OSX is available at ~/Library/Android/sdk/build-tools/22.0.1/zipalign
-
mr5 over 7 yearsI am getting an error of
The system cannot find the file specified
using Cordova 6.2.0 -
Midhun KM over 7 yearshave you replaced the keystore path? If so please post the command here.
-
mr5 over 7 yearsYep. It's
cordova build android --release -- --keystore="C:\release.keystore" --storePassword=****** --alias=mr5
-
mr5 over 7 yearsAfter enclosing my password with
"
(double quote), it started to run. I think Cordova has an issue about parsing special characters. -
Thomas about 7 years@ThomasBormans somehow
-tsa timestamp.digicert.com
threw a NullPointerException for me.-tsa http://timestamp.digicert.com
seems to fix it -
Tadej about 7 yearsBest answer! Thanks. :)
-
Sartheris Stormhammer about 7 yearsthis answer does not works, I dont know why it has so many upvotes
-
Marek about 7 yearsThis one I like :)
-
cfprabhu almost 7 years@aswzen can you give me some more details regarding your issue plz?
-
Shawn almost 7 yearsDo we still need to run
zipalign
after this? -
aswzen almost 7 years@cfprabhu i am commenting a comment.. by the way in windows, default android sdk if installed from latest cordova console was
C:\Users\[PC Username]\AppData\Local\Android\sdk\build-tools
..zip align tool was there -
infinito84 almost 7 yearsFrom cordova 5.0, I don't need to use --buildConfig, just with having the build.json at the top of the folder (the main folder) works, and when I need to publish on Google Play, I just use cordova build android --release.
-
aswzen almost 7 yearsbe careful with Key Generation...if you playing with google play store
-
Nguyen Tran almost 7 yearsIf anyone use Ionic, you can checkout this document ionicframework.com/docs/cli/cordova/build
-
Mawcel over 6 yearsYou still need to run zipalign on the signed apk like in cfprabhu 's answer
-
Toni Michel Caubet over 6 yearsHello, I did it in the past following your steps. but in a different computer now step 4 prompts:
error de herramienta de claves: java.io.FileNotFoundException: NAME-~/.android/release.keystore (No such file or directory) java.io.FileNotFoundException: NAME-~/.android/release.keystore (No such file or directory)
any idea why? ( usingkeytool -genkey -v -keystore NAME-~/.android/release.keystore -alias appName -keyalg RSA -keysize 2048 -validity 10000
) -
cfprabhu over 6 yearsPlease make sure that folder have your keystore file.
-
Aleks G over 6 yearsNote that if you do not want to store the passwords in the file, just remove the password lines. When building, cordova will prompt for the passwords.
-
Aleks G over 6 yearsNote that if you do not want to store the passwords in the file, just remove the password lines. When building, cordova will prompt for the passwords.
-
artuska over 6 yearsI've made all of these steps and everything works like a charm! Thank you. I'm a frontend developer not familiar with Cordova/Android/Android Studio/etc., so you saved me tons of hours.
-
cfprabhu over 6 years@artuska Thank you. Happy coding. If you need any help on cordova contact me [email protected]
-
Ajit Singh about 6 yearsif you are running with the problem of "c:\program is not recognized" then pls use ""[double quote] from start to end eg D:\projects\Phonegap\Example\platforms\android\build\outputs\apk> "C:\Phonegap\adt-bundle-windows-x86_64-20140624\sdk\build-tools\android-4.4W\zipalign" -v 4 Example-release-unsigned.apk Example.apk ]
-
croppio.com almost 6 yearsWorks with cordova 8.0.0
-
HolloW over 5 yearsgood solution, simple. I just run cordova build android --release
-
Intuitisoft over 3 yearsthe dot after aliaspass is confusing. Reproduce the same password with "password" : "aliaspass",
-
tyler.frankenstein over 3 yearsIt appears I had to use
"keystoreType": "jks"
here. -
Leandro Gamarra about 3 yearsThe only guide that helped me, THANK YOU MAN!