How to Create a Signed APK File Using Cordova Command Line Interface

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.

Building Cordova release apk

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)

Generated signed Android APK (Cordova)

I have already solved my problem.
I am explaining its steps for the people who are still stuck in it:

  1. Create a unsigned apk.
    Run this command cordova --release android after getting to the location of the project.

  2. You can find the unsigned apk in
    project_name\platforms\android\build\outputs\apk\android-release-unsigned.apk

  3. Copy this apk and keystore tool in one folder.
    Navigate to the folder and sign it using jarsigner present in java.
    Run this command,
    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename <Unsigned APK file> <Keystore Alias name>

  4. After this you will be prompt to enter password of keystore.

  5. Go to location of zipalign.
    (it is present in Android\SDK\build-tools\version)

  6. Run this command
    zipalign -v 4 "location of signed apk" "location of aligned apk"

Android App Sign in Cordova

You are only getting an unsigned APK since gradle cannot find your keystore which is used to sign your app.

To solve the issue, you need to do the following (this is like you you do in Windows, should be the same on Mac):

  1. Create keystore via console using something like keytool -genkey -v -keystore C:\mykeystore\CordovaRelease.keystore -alias CordovaRelease -keyalg RSA -keysize 2048 -validity 10000 Please make sure you have JRE installed.
  2. You will be asked for CN, OU,... and a password to be set, choose one.
  3. If you want to use different keystores for Debug/Release you can create another keystore
  4. Create build.json file in your Cordova project folder and reference the keystore like

    "android": {
    "debug": {
    "keystore": "..\mykeystore\CordovaDebug.keystore",
    "storePassword": "secretpassword",
    "alias": "CordovaDebug",
    "password" : "secretpassword",
    "keystoreType": ""
    },
    "release": {
    "keystore": "..\mykeystore\CordovaRelease.keystore",
    "storePassword": "",
    "alias": "CordovaRelease",
    "password" : "secretpassword",
    "keystoreType": ""
    }
    }

    The relative path goes from your Cordova project folder to the keystore folder.

  5. Run Cordova build from console like cordova build android --release, gradle will now take the build.json file and finds your keystore.

Hope this helps.

How to build my ionic application into an apk?

i think you are asking a duplicated question ,
follow this How to create a signed APK file using Cordova command line interface?
contains full information to build apk and sign it

Android: what's the most simple way to sign APK?

You can sign using Android Studio.
If you need a release build then change the build type to release and then go to Build>Generate signed apk. Add the necessary details and your apk will be generated along with your keystore file.
For more details please visit Android Developer Site.

If you are using eclipse you may visit This So post.

How can I signing my apps in cordova 6

I finally made it work:

  1. In cordova CLI...

    cordova build --release android

  2. make a keystore by:

    keytool -genkey -v -keystore "E:/Renascence Soft/Running Projrct/apps keystore/renascence.keystore" -alias renascence -keyalg RSA -keysize 2048 -validity 10000
  3. copy renascence.keystore to C:/program file/java/jdk 1.6/bin file:

    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore renascence.keystore "E:/Renascence Soft/Running Projrct/Renascence App/platforms/android/build/outputs/apk/android-release-unsigned.apk" renascence
  4. copy android-release-unsigned.apk file in
    F:/Public/Android Sdk/sdk/build-tools/23.0.1

  5. Finally Run...

    zipalign -v 4 android-release-unsigned.apk Renascence.apk


Related Topics



Leave a reply



Submit