Reputation: 2223
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?
Upvotes: 187
Views: 329552
Reputation: 41
At first, create a keystore: keytool -genkeypair -keystore my.keystore -alias alias_name -keyalg RSA -keysize 3072 -validity 10000 -v
According to the documentation --packageType
define the file extension apk/bundle. --
to indicate that these are platform-specific arguments
Inside the project folder run:
cordova build android --release -- --keystore=../../../key.keystore --storePassword="1234567890" --alias=alias_name --packageType=apk --password="1234567890"
A log will appear with the apsolute file's path.
example: /path/project/platforms/android/app/build/outputs/apk/release/app-release.apk
Sign the APK:
jarsigner -verbose -sigalg SHA256withRSA -digestalg SHA-256 -keystore key.keystore project.apk alias_name
Verify signature: jarsigner -verify -verbose -certs project.apk
Use zipalign
zipalign -v 4 project.apk project.sign.apk
In case the error "Failed to extract native libraries, res=-2" when installing:
zipalign -p 4 project.apk project.sign.apk
Final step is to delete the history because leaving clear text passwords in your history is a security issue.
Upvotes: 1
Reputation: 1671
Great news!
cordova 10 include the new android version with app-release.aab
.
Upvotes: 1
Reputation: 6541
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
Upvotes: 135
Reputation: 337
For Windows, I've created a build.cmd
file:
(replace the keystore path and alias)
For Cordova:
@echo off
set /P spassw="Store Password: " && set /P kpassw="Key Password: " && cordova build android --release -- --keystore=../../local/my.keystore --storePassword=%spassw% --alias=tmpalias --password=%kpassw%
And for Ionic:
@echo off
set /P spassw="Store Password: " && set /P kpassw="Key Password: " && ionic build --prod && cordova build android --release -- --keystore=../../local/my.keystore --storePassword=%spassw% --alias=tmpalias --password=%kpassw%
Save it in the ptoject's directory, you can double click or open it with cmd.
Upvotes: 0
Reputation: 426
##Generated signed apk from commandline
#variables
APP_NAME=THE_APP_NAME
APK_LOCATION=./
APP_HOME=/path/to/THE_APP
APP_KEY=/path/to/Android_key
APP_KEY_ALIAS=the_alias
APP_KEY_PASSWORD=123456789
zipalign=$ANDROID_HOME/build-tools/28.0.3/zipalign
#the logic
cd $APP_HOME
cordova build --release android
cd platforms/android/app/build/outputs/apk/release
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore $APP_KEY ./app-release-unsigned.apk $APP_KEY_ALIAS <<< $APP_KEY_PASSWORD
rm -rf "$APK_LOCATION/$APP_NAME.apk"
$zipalign -v 4 ./app-release-unsigned.apk "$APK_LOCATION/$APP_NAME.apk"
open $APK_LOCATION
#the end
Upvotes: 3
Reputation: 283
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)
Upvotes: 2
Reputation: 5352
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.
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.
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
Note : We have our keystore keystoreNAME-mobileapps.keystore
in this Git Repo, if you want to create another, please proceed with the following steps.
keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>
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
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:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename> <Unsigned APK file> <Keystore Alias name>
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.
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.
Upvotes: 332
Reputation: 2085
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
Upvotes: 60
Reputation: 8047
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.
Goto your cordova project for 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
if u used ant-build
yourproject/platforms/android/ant-build/Example-release-unsigned.apk
OR
if u used gradle-build
yourProject/platforms/android/build/outputs/apk/Example-release-unsigned.apk
keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>
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.)
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
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:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename <Unsigned APK file> <Keystore Alias name>
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".
D:\projects\Phonegap\Example\platforms\android\ant-build> 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.
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
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
Now we have our final release binary called example.apk and we can release this on the Google Play Store.
Upvotes: 2
Reputation: 99
On Mac (osx), I generated two .sh files, one for the first publication and another one for updating :
#!/bin/sh
echo "Ionic to Signed APK ---- [email protected] // Benjamin Rathelot\n"
printf "Project dir : "
read DIR
printf "Project key alias : "
read ALIAS
cd $DIR/
cordova build --release android
cd platforms/android/build/outputs/apk/
keytool -genkey -v -keystore my-release-key.keystore -alias $ALIAS -keyalg RSA -keysize 2048 -validity 10000
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore android-release-unsigned.apk $ALIAS
zipalign -v 4 android-release-unsigned.apk signedApk.apk
And to update your app:
#!/bin/sh
echo "Ionic to Signed APK ---- [email protected] // Benjamin Rathelot\n"
printf "Project dir : "
read DIR
printf "Project key alias : "
read ALIAS
cd $DIR/
cordova build --release android
cd platforms/android/build/outputs/apk/
rm signedApk.apk
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore android-release-unsigned.apk $ALIAS
zipalign -v 4 android-release-unsigned.apk signedApk.apk
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 :
./ionicToApk.sh # or whatever depending of the name of your file, in CLI
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
Upvotes: 7
Reputation: 1717
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
Upvotes: 11
Reputation: 4389
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
Upvotes: 20