I've built a signed apk with cordova build --release android, should I zipalign it or did cordova already do all the job?
I know this is an old question, but figured I'd pitch the answer:
Cordova let's you hook on the "after_build" event, in which you can run a .bat file which will handle the zipalign.
Example (config.xml):
<hook type="after_build" src="zipalign_release.bat" />
The "src" is relative to project root (ie: same place config.xml resides).
My .bat:
if exist platforms\android\app\build\outputs\apk\release (
cd platforms\android\app\build\outputs\apk\release
zipalign -f -v 4 app-release.apk app-release-zipaligned.apk
) else (
echo Debug build.
)
no, you should zipalign it using the command prompt zipalign -v 4 app-release-unsigned.apk app-release.apk
Related
Walkthrought with react-native tutorial (react-native 0.30):
I created app which couldn't be installed on android device here are steps:
react-native init demo
cd android
gradlew assembleRelease
Copying unsigned apk to mobile device ( htc m8 mini 2 )
and got result : Application demo was not installed
Based on this similiar question apk didn't run on mobile android react native:
Unsigned apk won't install on mobile.
But why I am able to run unsiged application made by Ionic 2?
Whats the problem?
In Ionic 2 you run a debug apk, its not possible to run an unsigned apk.
What you can do in react is sign your apk and run.
The first setup can be a little complex, but after you complete the setup is very easy to generate a signed apk when you need it.
You can follow these instructions to sign your apk:
Generating a signing key
You can generate a private signing key using keytool.
$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
This command prompts you for passwords for the keystore and key, and to provide the Distinguished Name fields for your key. It then generates the keystore as a file called my-release-key.keystore.
The keystore contains a single key, valid for 10000 days. The alias is a name that you will use later when signing your app, so remember to take note of the alias.
Note: Remember to keep your keystore file private and never commit it to version control.
Setting up gradle variables
Place the my-release-key.keystore file under the android/app
directory in your project folder.
Edit the file ~/.gradle/gradle.properties and add the following
(replace ***** with the correct keystore password, alias and key
password),
MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=*****
MYAPP_RELEASE_KEY_PASSWORD=*****
These are going to be global gradle variables, which we can later use in our gradle config to sign our app.
Note about saving the keystore: Once you publish the app on the Play
Store, you will need to republish your app under a different package
name (losing all downloads and ratings) if you want to change the
signing key at any point. So backup your keystore and don't forget the
passwords. Note about security: If you are not keen on storing your
passwords in plaintext and you are running OSX, you can also store
your credentials in the Keychain Access app. Then you can skip the two
last rows in ~/.gradle/gradle.properties.
Adding signing config to your app's gradle config
Edit the file android/app/build.gradle in your project folder and add the signing config,
...
android {
...
defaultConfig { ... }
signingConfigs {
release {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}
...
Generating the release APK
Simply run the following in a terminal:
$ cd android && ./gradlew assembleRelease
Gradle's assembleRelease will bundle all the JavaScript needed to run your app into the APK. If you need to change the way the JavaScript bundle and/or drawable resources are bundled (e.g. if you changed the default file/folder names or the general structure of the project), have a look at android/app/build.gradle to see how you can update it to reflect these changes.
The generated APK can be found under android/app/build/outputs/apk/app-release.apk, and is ready to be distributed.
Testing the release build of your app
Before uploading the release build to the Play Store, make sure you test it thoroughly. Install it on the device using:
$ cd android && ./gradlew installRelease
Note that installRelease is only available if you've set up signing as described above.
You can kill any running packager instances, all your and framework JavaScript code is bundled in the APK's assets.
#EvghenyKalkutin if you are beginner in react-native then just follow this simple step
Download chocolatey from your cmd just write this code in cmd #powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
after successfully install the chocolatey now install node.js and Python2 using chocolatey. for this use this code for install node.js choco install nodejs.install and install Python# use this code choco install python2
ok now open node.js command promt and run this code npm install -g react-native-cli to start react-native
ok now download sample project using this code react-native init AwesomeProject or react-native init HelloWorld :)
Now go to the directory where this project downloaded using cd AwesomeProject
Start android stduio and open this project and correct your gradle error if it occurs(and clean project) and connect your device with usb debugging
and check your device is available or not using this code adb devices
if device is available then convert it's port for the react native using this code adb reverse tcp:8081 tcp:8081
now perform this code npm start for the build node.js bridge
after build the node.js bridge now start your project using this code react-native run-android and here you go your sample project is run on your device
**And for unsign apk or assembleRelease just go through below step **
just write this code in node.js command prompt cd android && gradlew clean && gradlew assembleRelease
I use windows os so all this instructions are for windows os I don't know this code is same for mac os or linux os
i hope this wiil help you for start up in react-native. if any problem just comment it.
WINDOWS USERS
FOLLOW THESE INSTRUCTIONS ....IT WORKED FOR ME
Prerequisite - You must have a keystore file, If you don't have then Open cmd run keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000 follow instructions and you are done.You will have a keystore file. Now follow these steps.
1 .On windows cd android and then run gradlew assembleRelease in Editor Terminal or Command Prompt in your project directory.
2 .Find APK at this location
android/app/build/outputs/apk/release/app-release-unsigned.apk
3 .Copy this APK to bin folder of jdk installation directory ( for me directory was C:\Program Files\Java\jdk1.8.0_181\bin ) [ Basically in this step we are trying to go to the same directory as jarsigner]
4 .Also Copy your keystore file to this ( C:\Program Files\Java\jdk1.8.0_181\bin ) directory.
5 .Now Open cmd in Administrator mode and run
cd C:\Program Files\Java\jdk1.8.0_181\bin
6 .Now run
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <<your keystore file name >> <<your apk file name>> alias_name
Here goes your apk , now find your signed apk here cd C:\Program Files\Java\jdk1.8.0_181\bin . Install it, Now it should install.
I am trying to get my Ionic app published. However, when trying to zipalign the apk with the following command
/Users/bertcarremans/Library/Android/sdk/build-tools/23.0.3/zipalign zipalign -v 4 android-release-unsigned.apk chartly.apk
I get the message below:
Zip alignment utility
Copyright (C) 2009 The Android Open Source Project
Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip
zipalign -c [-v] <align> infile.zip
<align>: alignment in bytes, e.g. '4' provides 32-bit alignment
-c: check alignment only (does not modify file)
-f: overwrite existing outfile.zip
-p: page align stored shared object files
-v: verbose output
-z: recompress using Zopfli
How can I make the zipalign tool to work? Thanks!
I found the solution.
Copy the zipalign tool to the apk folder of your app. On my computer the zipalign tool was located in /Users/bertcarremans/Library/Android/23.0.3
Then run the command ./zipalign -v 4 android-release-unsigned.apk chartly.apk
There is no need to copy zipalign file anywhere, if it is installed through command line a symbolic link will be created and then you can run it from anywhere.
You are not using the right syntax of the zipalign tool. Please check again, you have written zipalign twice so you are passing zipalign as a parameter to zipalign command which is not correct.
I am using ionic framework to generate apk for android platform.
After running ionic build android, an android-debug.apk is generated. How can I generate a non-debug apk which is smaller and faster?
This is my android release shell script
IFY
clear
gulp
ionic build --release android
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore "path/to/your/keystore" "platforms/android/build/outputs/apk/android-release-unsigned.apk" "keystore alias" -storepass xxx -keypass xxx
/path/to/android-sdk/build-tools/23.0.2/zipalign -v 4 "platforms/android/build/outputs/apk/android-release-unsigned.apk" "android-release.apk"
rm "platforms/android/build/outputs/apk/android-release-unsigned.apk"
In cordova 6.2.0 you can release an apk with follwoing commands. Since ionic is a wrapper around cordova, this should work.
cd cordova/ #change to root cordova or ionic 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
As ionic CLI is based on cordova CLI, you can use, directly:
ionic build android --release
Also, if you are worried about optimization, you might find interesting usign zipalign, in order to align data on 4-byte boundaries, thus causing the app to reduce the amount of RAM used. Once you have build the release apk with the previous command, you can run:
zipalign -v 4 yourReleaseApp.apk zipalignedReleaseApp.apk
Basically, as Jan commented, you should use the command: cordova build android --release. You can take a look at the official guide from Ionic about how to publish your app.
1. To release build for Android, we can use the following cordova cli command
ionic cordova build --release android
2. Build apk is unsigned. Need to sign it. That’s why create private key with keytool of JDK. we can use following cli command
keytool -genkey -v –keystore mykey.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
keep the mykey.keystore file in a safe place for future use.
if the keytool is not work then copy path of this file and set it in the system environment variable.
3. Now sign the unsigned apk with the following command
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore mykey.keystore projectpath\platforms\android\build\outputs\apk\android-release-unsigned.apk alias_name
4. At last optimize the apk file.
zipalign -v 4 projectpath\platforms\android\build\outputs\apk\android-release-unsigned.apk projectpath\platforms\android\build\outputs\apk\android-release.apk
for more details you can visit following url
Build Release APK of Android Application from ionic
I'm trying to generate my release apk to deploy it on Play Store.
I'm running this command
Sudo cordova build android --release
Which is generating me a file named Android-Release-Unsigned.apk
I tried many solutions, like creating keystore
keytool -genkey -v -keystore key-name.keystore -alias alias-name -keyalg RSA -keysize 2048 -validity 10000
or the trick of creating a Ant.properties file like here and none of them worked at all.
Any other possible solution for this ? Cordova version is 5.1.1
Create a file called release-signing.properties and put in APPFOLDER\platforms\android folder Cordova used to use ant.properties but now ignores that file.
Contents of the file: edit after = for all except 2nd line
storeFile=C:/yourlocation/app.keystore
storeType=jks
keyAlias=aliasname
keyPassword=aliaspass
storePassword=password
Now running cordova build android --release should produce a signed version.
If you have keystore file generated, this should work.
You may also need to remove console plugin if the same apk directly goes to production.
In cordova 6.2.0
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
Sorry I couldn't comment. Possible duplicate?
How to create a signed APK file using Cordova command line interface?
This worked perfect for me.
I am finding it extremely difficult to get my heard around signing my app under the new cordova 5.0 guideline
this is what i have done
(bongoapp)project root
->build.json
->phistoKey.keystore
->www
this is what i have inside my build.json file
{
"android": {
"release": {
"keystore": "phistoKey.keystore",
"storePassword": "",
"alias": "phistoKey",
"password" : "",
"keystoreType": ""
}
}
}
when i try
cordova build --release
or
cordova build android --release
I get error stating
Keystore file does not exist: C:\wamp\www\towncrier\platforms\android\..\..\phistoKey.keystore
I will be glad if anyone can help cos I am on a deadline TODAY. thank you
The way I do it in new Cordova CLI (with gradle) is using the options that cordova give us for doing it without make our own script or doing it manually in different steps. In my case, I have created a file in platforms/android directory, with the name release-signing.properties. The content of this should be the configuration for signing your apk, something like:
key.store=/PATH/TO/YOUR/KEYSTORE
key.alias=your_alias
key.store.password=key_store_pass
key.alias.password=key_store_alias
With this file created, you just need to run the standard command, cordova build android --release, and it will generate new release APK in your output directory (platforms/android/outputs/apk/yourapp-release.apk)
In cordova 6.2.0
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
I do the following in a shell script to build and sign an APK. This assumes that the app is named "bongoapp" and that your Cordova project and keystore file (phistoKey.keystore) are located in the same folder as the shell script.
# Run this script in the bongoapp folder to build and sign a release APK.
# Hint: the password is "bongoapp"
# Build the release APK
echo "Building Android release APK"
cordova build android --release
# Sign the APK
echo "Signing Android release APK"
jarsigner -verbose -certs -sigalg SHA1withRSA -digestalg SHA1 -keystore phistoKey.keystore platforms/android/ant-build/MainActivity-release-unsigned.apk bongoapp_alias
jarsigner -verbose -certs -verify platforms/android/ant-build/MainActivity-release-unsigned.apk
zipalign -v 4 platforms/android/ant-build/MainActivity-release-unsigned.apk platforms/android/ant-build/bongoapp.apk
echo "Done, output file is located here -> platforms/android/ant-build/bongoapp.apk"