Build android release apk on Phonegap 3.x CLI
Asked Answered
D

9

108

How can I build an android app locally using the Phonegap 3.x CLI, ready to release? I check the bin folder generated inside the platforms/android directory of the project, and only has .debug APKs.

By the way, I use this command: phonegap local build android

Disenfranchise answered 5/12, 2013 at 14:38 Comment(0)
D
153

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:

  1. Open a command line window, and go to /path/to/your/project/platforms/android/cordova.

  2. Run build --release. This creates an unsigned release APK at /path/to/your/project/platforms/android/bin folder, called YourAppName-release-unsigned.apk.

  3. 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.

Disenfranchise answered 26/12, 2013 at 22:42 Comment(4)
I had to manually modify my AndroidManifest.xml file where I had <application android:debuggable="true"... I needed to change to <application android:debuggable="false"Montes
You also have to zip align the thing after: zipalign -v 4 your-unaligned.apk your-aligned.apkImpropriety
I'm running Cordova 3.4.1-0.1.0 and the file is created in /path/to/your/project/platforms/android/ant-buildDiley
It seems that, being specific, this is for phonegap 3.0.x to 3.3.x, from 3.4.x and ahead this may vary.Amaryl
C
112

In PhoneGap 3.4.0 you can call:

cordova build android --release

If you have set up the 'ant.properties' file in 'platforms/android' directory like the following:

key.store=/Path/to/KeyStore/myapp-release-key.keystore
key.alias=myapp

Then you will be prompted for your keystore password and the output file (myapp-release.apk) ends up in the 'platforms/android/ant-build' directory already signed and aligned and ready to deploy.

Cohosh answered 10/4, 2014 at 6:6 Comment(15)
But there is command "phonegap build". Why this command build only debug apk package?Load
You may be able to add --release to the command so you have phonegap build android --release. I haven't tested this though. In Phonegap 3.4.0 the docs state to use 'cordova' commands, not 'phonegap'. It all can be a little confusing :(Cohosh
The current 3.4.0 release of Cordova doesn't include an 'ant-properties' file.Fab
@Fab yes that is true. So what I do is have a hook on 'after_prepare' that copies a custom 'ant.properties' file to 'platforms/android' directory.Cohosh
Created ant.properties in platforms/android and just dropped these two properties into it. Works perfectly, thanks!Cavalryman
If you're unsure about the keystore/alias or how to create them full details are at developer.android.com/tools/publishing/app-signing.htmlAdaxial
Warning! With cordova 3.4.1-0.1.0 calling above statement "cordova build android --release" deleted the complete contents of assets\www folder of my project, and so the complete app!! Do a backup before calling this. I do not know why this happens currently, I will investigate. But this is reproducible.Pekin
@Thilo this will be because when cordova builds, it runs the 'prepare' logic which prepares and copies your project root 'www' directory to the platform www directory. This is expected behaviour for using the CLI development route. In practice you should only make changes to the project root 'www'.Cohosh
Also note that you can specify key.store.password and key.alias.password properties in this file, so you won't have to enter it each time you build the release. Of course be cautious and do not commit this file into version system.Mamelon
If using windows, then you have to have double-backslashes like this: key.store=C:\\Users\\android.keystore key.alias=android - or else you get the error : Value for 'keystore' is not valid. It must resolve to a single pathCushman
@vsb how do you exclude this file from the apk?Barfuss
@Barfuss I didn't do anything special, I don't think this file should be packaged inside apk. I unzipped my apk and didn't find any properties files there.Mamelon
@vsb I dont see it either. Was your comment just a beware or a real problem?Barfuss
@Barfuss I wrote about versioning system like Git or Subversion. You could accidentally commit this file e.g. into github and everyone with access to the repository will be able to see your password. That might be unappropriate and you need to exclude this file by adding it to .gitignore. This doesn't have anything with APK. Sorry if my comment wasn't clear enough.Mamelon
For cordova 5.0.0 and above create a build.json on the root of your project and fill it with { "android": { "release": { "keystore": "/path/to/your.keystore", "alias": "youalias" } } } instead of the ant.properties file. FYI cordova.apache.org/docs/en/edge/…Arboreal
K
19

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

Previous answer:

According to cordova 5.0.0

{
     "android": {
         "release": {
             "keystore": "app-release-key.keystore",
             "alias": "alias_name"
         }
     }
 }

and run ./build --release --buildConfig build.json from directory platforms/android/cordova/

keystore file location is relative to platforms/android/cordova/, so in above configuration .keystore file and build.json are in same directory.

keytool -genkey -v -keystore app-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

Komi answered 23/7, 2015 at 11:5 Comment(0)
S
15

Following up to @steven-anderson you can also configure passwords inside the ant.properties, so the process can be fully automated

so if you put in platform\android\ant.properties the following

key.store=../../yourCertificate.jks
key.store.password=notSoSecretPassword
key.alias=userAlias
key.alias.password=notSoSecretPassword
Stillman answered 26/8, 2014 at 9:56 Comment(1)
On Windows you must escape the slashes... '... key.store=C:\\OpenSSL-Win32\\certs\\android_key_store.keystore ...'Autism
P
4

just wondered around a lot because I got the same issue but in my installation the command "cordova" was never available and "phone gap build android --release" just ignored the platform/android/ant.properties.

so looking inside my platform filter I found a folder named "cordova" and inside of it there was an "build" binary that accepted the --release argument, it asked me for the key chains and I ended with a signed and ready for production APK.

this was never documented in any part of the phone gap site and frankly speaking now I kinda hate phonegap :( it was supposed to make the things easier but everything was just complicated :(

Presentable answered 23/5, 2014 at 18:10 Comment(0)
C
2

Building PhoneGap Android app for deployment to the Google Play Store

These steps would work for Cordova, PhoneGap or Ionic. The only difference would be, wherever a call to cordova is placed, replace it with phonegap or ionic, for your particular scenario.

Once you are done with the development and are ready to deploy, follow these steps:

  1. Open a command line window (Terminal on macOS and Linux OR Command Prompt on Windows).

  2. Head over to the /path/to/your/project/, which we would refer to as the Project Root.

  3. While at the project root, remove the "Console" plugin from your set of plugins.

    The command is: cordova plugin rm cordova-plugin-console

  4. While still at the project root, use the cordova build command to create an APK for release distribution.

    The command is: cordova build --release android

  5. The above process creates a file called android-release-unsigned.apk in the folder ProjectRoot/platforms/android/build/outputs/apk/

  6. Sign and align the APK using the instructions at https://developer.android.com/studio/publish/app-signing.html#signing-manually

    At the end of this step the APK which you get can be uploaded to the Play Store.

Note: As a newbie or a beginner, the last step may be a bit confusing as it was to me. One may run into a few issues and may have some questions as to what these commands are and where to find them.

Q1. What are jarsigner and keytool?
Ans: The Android App Signing instructions do tell you specifically what jarsigner and keytool are all about BUT it doesn't tell you where to find them if you run into a 'command not found error' on the command line window.

Thus, if you've got the Java Development Kit(JDK) added to your PATH variable, simply running the commands as in the Guide would work. BUT, if you don't have it in your PATH, you can always access them from the bin folder of your JDK installation.

Q2. Where is zipalign?
Ans: There is a high probability to not find the zipalign command and receive the 'command not found error'. You'd probably be googling zipalign and where to find it?

The zipalign utility is present within the Android SDK installation folder. On macOS, the default location is at, user-name/Library/Android/sdk/. If you head over to the folder you would find a bunch of other folders like docs, platform-tools, build-tools, tools, add-ons...

Open the build-tools folder. cd build-tools. In here, there would be a number of folders which are versioned according to the build tool-chain you are using in the Android SDK Manager. ZipAlign is available in each of these folders. I personally go for the folder with the latest version on it. Open Any.

On macOS or Linux you may have to use ./zipalign rather than simply typing in zipalign as the documentation mentions. On Windows, zipalign is good enough.

Cacography answered 19/3, 2018 at 15:36 Comment(0)
I
1

i got this to work by copy pasting the signed app in the same dir as zipalign. It seems that aapt.exe could not find the source file even when given the path. i.e. this did not work zipalign -f -v 4 C:...\CordovaApp-release-unsigned.apk C:...\destination.apk it reached aapt.exeCordovaApp-release-unsigned.apk , froze and upon hitting return 'aapt.exeCordovaApp-release-unsigned.apk' is not recognized as an internal or external command, operable program or batch file. And this did zipalign -f -v 4 CordovaApp-release-unsigned.apk myappname.apk

Irma answered 9/1, 2015 at 16:36 Comment(1)
Thanks for the tip. You can also include the path to zipalign's folder in the PATH environment variable.Amaryl
G
0

I know this question asks about Phonegap 3.X specifically, but just for reference any Phonegap version above 4.0.0 uses Gradle instead of Ant to build by default. To use Ant instead of Gradle you can add this to your config.xml:

<preference name="android-build-tool" value="ant" />

When using Gradle the keystore signing information now needs to go into a new location (as outlined in this post). Create new file called 'release-signing.properties' in the same folder as "build.gradle" file and put inside the following content:

storeFile=..\\..\\some-keystore.keystore
storeType=jks
keyAlias=some-key
// if you don't want to enter the password at every build, you can store it with this
keyPassword=your-key-password
storePassword=your-store-password
Germano answered 15/11, 2015 at 16:10 Comment(0)
C
-7

You could try this command, it should build and run the app (so .apk should be created) :

phonegap local run android

Concretize answered 5/12, 2013 at 18:51 Comment(3)
It still create only the debug APKs.Amaryl
No, it is just that it creates debug signed APKs, not release APKs.Amaryl
Found the solution here (3/4 way down : "Finalize apk for Google Play Upload") : iphonedevlog.wordpress.com/2013/08/16/…Concretize

© 2022 - 2024 — McMap. All rights reserved.