Build android release apk on Phonegap 3.x CLI
This is for Phonegap 3.0.x to 3.3.x. For PhoneGap 3.4.0 and higher see below.
Found part of the answer here, at Phonegap documentation. The full process is the following:
Open a command line window, and go to /path/to/your/project/platforms/android/cordova.
Run
build --release
. This creates an unsigned release APK at /path/to/your/project/platforms/android/bin folder, called YourAppName-release-unsigned.apk.Sign and align the APK using the instructions at android developer official docs.
Thanks to @LaurieClark for the link (http://iphonedevlog.wordpress.com/2013/08/16/using-phonegap-3-0-cli-on-mac-osx-10-to-build-ios-and-android-projects/), and the blogger who post it, because it put me on the track.
Phonegap / Cordova - Build Apk
To check whether or not you have everything installed that is required to build an APK first run this command:
$ cordova requirements
This should give something similar to the following result:
Requirements check results for android:
Java JDK: installed .
Android SDK: installed
Android target: installed android-19,android-21,android-22,android-23,Google Inc.:Google APIs:19,Google Inc.:Google APIs (x86 System Image):19,Google Inc.:Google APIs:23
Gradle: installed
You need to make sure that you have the latest Java JDK installed and the Android SDK. You will also need to make sure that Java is in you computer's PATH.
Then you can run this command:
$ cordova build android
Follow this guide:
https://cordova.apache.org/docs/en/latest/guide/cli/
phonegap build android does nothing
Its seems related to NodeJS 5.0... In my case the Cordova Build command seems to be failling since the update.
After some investigation its look like you can by pass the problem by running the "build script" in your platform/build folder
./platforms/android/cordova/build
How to create a signed APK file using Cordova command line interface?
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.
Related Topics
What Is More Efficient? Static, Data Passing, Shared Preferences, Database...
Couldn't Get Connection Factory Client - Fighting with Google Maps
How to Clear the Volley Cache Automatically
How to Hide App Title in Android
Failed to Find Style 'Mapviewstyle' in Current Theme
Activity.Finish() Called But Activity Stays Loaded in Memory
How to Parse JSON Array Without Any Object in Retrofit
Android: How to Implement First Time Tutorial Like Go Launcher in My App
Is Fcm (Firebase Cloud Messaging) Token for One Device or for One Account
Cordova Cannot Add Android Failed with Exit Code Enoent
Android Studio - Cannot Resolve Symbol 'Firebase'
Android: HTML Anchor Link Works Only Once in Webview
What Is Metadata ? and What Is The Use of It in Android
App Crashes on Startup Due to Java.Lang.Illegalargumentexception: Column '_Id' Does Not Exist
Android Browser Bug? Div Overflow Scrolling
Why Did The Listview Repeated Every 6Th Item
How to Set The Font Style to Bold, Italic and Underlined in an Android Textview