Specify signing config for Gradle and Cordova 5

2019-01-31 13:13发布

问题:

In correspondence with Cordova news version 5.0.0 is ready. I tried to upgrade all my Android projects like it was usually.

To update Cordova framework itself:

npm update -g cordova

To update library in Android project:

cordova platform update android

After building new version using "--release" option:

cordova build android --release

I got only unsigned version. Strange...
So how is it possible to reuse existing keystore (previously generated by keytool and used by Cordova 4.0.0) to get signed APK?

回答1:

It looks like Cordova 5.0.0 uses Gradle build automation system instead of Ant. So original settings for keystore defined in the file "/platforms/android/ant.properties" will not be used during building. So we will get unsigned APK as a result.

To solve this issue we need to tell Gradle to use existing keystore as well. Investigation of build.gradle gives us useful information that we need to provide signingConfigs.

But it is bad idea to do that in this file because it is labelled as auto-generated and it should be free of any editing.

So finally I have found the way to solve it. Object "signingConfigs" will be constructed from file with filename stored in cdvReleaseSigningPropertiesFile. In correspondence with documentation the default value of this variable is "release-signing.properties". So we need just to create new file with such name 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

Path in example is specified for keystore saved in the project root directory. It has Windows style... In case of Linux you will need to use single slashes (not double backslashes like in above example).

In addition you can set your own path for signing settings file. To read more about that check the edge version of cordova documentation.



回答2:

According to official documentation (v5.1.1+) I've just added a build.json

{
     "android": {
         "release": {
             "keystore": "android.keystore",
             "storePassword": "storepassword",
             "alias": "mykey",
             "password" : "password",
             "keystoreType": ""
         }
     }
 }

and my existing android.keystore file in the root cordova folder.

cordova build android --release will do the trick, no hooks needed, the singned apk-file can then be found in ./platform/android/build/outputs/apk as android-release.apk .



回答3:

For cordova 5.1.1+, refer to Ronny Elflein r11lein answer https://stackoverflow.com/a/30980293/2163398

The current answer is good (by @maxim), but we want to keep platform-specific folder untouched, so we can maintain it out of version control. So I created this Hook to automatically copy the "release-signing.properties" file in android folder.

So, first, place your keystore in your project root. Then create the "release-signing.properties" in a the folder config, in the root of your cordova project. Contents (storeFile path in example is specified for keystore saved in the project root directory. It has Windows style... In case of Linux you will need to use single slashes):

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

Then, add if not exists the "after_prepare" folder inside the "hooks" folder, and crete in it a file called "copy_assets.js".Contents:

#!/usr/bin/env node

// Files to be copied, with source and destination

var ncp = require('ncp').ncp,
    transfers = [
      {
        'source': './config/android/release-signing.properties',
        'destination': './platforms/android/release-signing.properties'
      }
    ];

ncp.limit = 16;

transfers.forEach(function(transfer) {
  ncp(transfer.source, transfer.destination, function (err) {
    if (err) {
      return console.error(err);
    }
    console.log('====== Assets moved from ' + transfer.source + ' to ' + transfer.destination + ' ======');
  });
});

If you don't hace ncp in your project, open a command promt in the project folder root and run the command (it's posible you need to create a a packages.json file in your project root):

npm install ncp

Then, you can run:

cordova build android --release


回答4:

To automate build release process you should create "build.json" on your project's folder and include the following content:

{
 "android": {
     "release": {
         "keystore": "android.keystore",
         "storePassword": "storepassword",
         "alias": "mykey",
         "password" : "password",
         "keystoreType": ""
     }
 }
}

The keytore is generated using

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

If you already have a keystore file just run the command below to know the required content

keytool -list -keystore .keystore

Then you just have to run

cordova build android --release


回答5:

Steps - 1) Generate keystore certificate

Syntax -  keytool -genkey -v -keystore C:\DIR\APPNAME.keystore -alias APPNAME -keyalg RSA -keysize 2048 -validity 10000
  • 2) Copy generated keystore file and paste in cd platforms\android
  • 3) Specify signing config for Gradle Create file property file - release-signing.properties (file name) Write bellow keys and values

    Syntax

            storeFile=..\\..\\some-keystore.keystore
            storeType=jks (pkcs12 for p12,jks for keystore)
            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
    

Example

        storeFile= abc.keystore
        storePassword=mypass
        storeType=jks
        keyAlias=abc
        keyPassword=mypass
  • 4) Build cordova project

cordova build android --release

  • 5) Run cordova project

cordova run android --release

  • 6) Relese APK created in following path -

platforms\android\build\outputs\apk



回答6:

In the latest gradle version it is also possible to create a file named "release-signing.properties" at the "\platforms\android\" folder. The contents should be:

storeFile=<your key>.keystore
storeType=jks
keyAlias=<your_alias>
keyPassword=<your_passwd>
storePassword=<your_passwd>

The file ".keystore" should be in the same folder.