How to get Cordova 3.4.0 to set Android version name/code on build?
Asked Answered
D

7

27

I am currently using Cordova 3.4.0 and the CLI to build my project to target android using the command:

cordova build android

OR

cordova build android --release

My config.xml has the following specified:

<widget id="com.example.myapp" 
        version="0.0.3" 
        versionCode="2" 
        ...
        >
</widget>

The resulting AndroidManifest.xml in myapp/platforms/android does not get updated with the version and version code specified in config.xml. It stays as the default:

<manifest 
        android:versionCode="1" 
        android:versionName="1.0"
        ...
</manifest>

Is this a bug? Or is there some other way I should specify the versionCode and versionName for Android in the config.xml? Even maybe, is there a hook I can use to update AndroidManifest.xml with the correct version before build?

I would rather not have to update this manually each time I want it to change as:

  1. It is error prone and fiddly
  2. It wont be an obvious thing to change for unfamiliar developers
  3. None of the platform folders are stored in source control

For these reasons I would like the version numbers to be set in the config.xml. Any ideas?

Thanks

Digiacomo answered 11/4, 2014 at 1:5 Comment(5)
android:versionName="1.5.3" is updated automatically from widget version in config.xml, so there's nothing to do. For versionCode, I haven't found anything and I think versionCode in config.xml is supported only for phonegap build but not for cordova or phonegap local builds. I don't know if a hook for that already exists but you should be able to build your own and put it in after_prepare.Mogador
I re-checked to see if the android:versionName="1.X.X" gets updated automatically and I found that it doesn't. I deleted the platform and started from scratch and with same result - android:versionName="1.0". Can you confirm this works on Cordova 3.4.0?Digiacomo
I confirm it works for me at least. Have you checked only after adding the platform, or also after a build?Mogador
It's a pain! What I've done is set up a grunt build process that replace/copies the entire manifest with one with the proper values.Prerequisite
@Josh could please share that build process? I have the same problemFianna
J
56

At last with Cordova 3.5.0-0.2.4 I added this attribute to the <widget> node config.xml

 android-versionCode="10"

and the AndroidManifest.xml was properly updated to

 android:versionCode="10" 
Jeaninejeanlouis answered 21/5, 2014 at 11:50 Comment(10)
I just tried this with 3.4.0 and it didn't work. I'll try with 3.5.0 later.Bombardier
affirming that it's working on 3.5.0, nice and clean solutionMorel
where to add ? This is the property of widget element?Holmes
Yes, exactly, as a property of widget.Jeaninejeanlouis
I have just tried this after updating Cordova 3.5.0 and... it works! It actually works! ThanksDigiacomo
If you upgrade to 3.5.0, and you don't have this prop in your config already (which is unlikely), cordova magically sets your versionCode to 10000, overwriting any existing value in AndroidManifest file.Putrid
it'd be great to have these things documented somewhere but...NOT!Fianna
Just in case someone gets here searching for this: in cordova 4.2, the widget property is android-versionCode. It's totally optional, though, as Cordova correctly generates a version number from the common version property (2.0.1 -> 20001).Pegpega
you do it like this inside the widget start tag <widget id="com.cordova.app" version="0.0.1" android-versionCode="1" android-versionName="1">Oys
using cordova 7.1.0 android:versionName doesn't being updated! so there's some other way to do that by cordova config.xml? or maybe need a custom hook ?Acorn
H
6

following the answer of MichaelOryl there is something that I implemented using cordova 3.4 and using the bash tool XMLStarlet

Basically it copies the value of the versionCode attribute from config.xml in AndroidManifest.xml

Instead of running this:

cordova build android --release

run [should become a script]:

cordova prepare android
# grab version code from config.xml
VERSION_CODE=$(xmlstarlet sel -t -v "//@versionCode" "config.xml")
# write correct version code in manifest
xmlstarlet ed -L -u "/manifest/@android:versionCode" -v "$VERSION_CODE" "platform/android/AndroidManifest.xml"
cordova compile android --release

In this way when when I upgrade to cordova 3.5, I can reuse the usual 'cordova build android --release'

Hildebrandt answered 23/7, 2014 at 9:0 Comment(0)
B
5

versionName shouldn't be a problem here, as it gets copied from config.xml to the AndroidManifest.xml file in platforms/android (at least on Cordova 3.4.0). That part I had no issue with.

Getting versionCode incremented, though, was quite a task. A series of tasks, actually, that I added to Grunt's Gruntfile.js.

This is the string-replace task. "grunt string-replace:versionCode" will increment the versionCode stored in package.json. "grunt string-replace:androidVersionCode" will take that value and put it into the platforms/android/AndroidManifest.xml file:

    // STRING-REPLACE
    'string-replace': {
        versionCode: { // update the version code stored in package.json
            options: {
                replacements: [
                    {
                        pattern: /['"]androidVersionCode['"]:.*?['"](\d*)['"]/ig,
                        replacement: function (match, p1, offset, string) {
                            return '"androidVersionCode": "' + (parseInt(p1) + 1) + '"';
                        }
                    }
                ]
            },
            files: {
                'package.json': 'package.json'
            }
        },

        androidVersionCode: { // update the version code stored in AndroidManifest.xml
            options: {
                replacements: [
                    {
                        pattern: /android:versionCode=['"](\d*)['"]/ig,
                        replacement: function (match, p1, offset, string) {
                            var pkg = grunt.file.readJSON('package.json');
                            grunt.log.writeln("pkg.androidVersionCode: " + pkg.androidVersionCode);
                            grunt.log.writeln('Returning: ' + 'android:versionCode="' + pkg.androidVersionCode + '"');
                            return 'android:versionCode="' + pkg.androidVersionCode + '"';
                        }
                    }
                ]
            },
            files: {
                'platforms/android/AndroidManifest.xml': 'platforms/android/AndroidManifest.xml'
            }
        }
    }

It requires this to be called from the Gruntfile.js file before use, of course (as well as npm install grunt-string-replace):

grunt.loadNpmTasks('grunt-string-replace');

You'll need to add a line such as the following to your package.json file for this all to work:

"androidVersionCode": "13", // or whatever value is current for you

It will get incremented by the string-replace:versionCode task above. I put the line in package.json after the line that starts with "version":

An important trick to getting this to work is to make sure that instead of calling "cordova build android" you call "cordova prepare android", then "grunt replace-string:androidVersionCode", and then "cordova compile android". Build is just a shortcut for calling "prepare" and then "compile", and between those two tasks is when you have to modify the AndroidManifest.xml file to prevent it from being overwritten, it seems.

My build process is much more complicated because I'm actually incrementing the version value in package.json with grunt-bump for Grunt and then injecting that into config.xml using xmlpoke for Grunt and my about page using string-replace. I use grunt-shell to actually call all of the Cordova build steps to get everything copied to where it goes and run in the right order.

Let me know if this helps.

Bombardier answered 12/5, 2014 at 15:12 Comment(1)
You can call cordova build android and then use an after_prepare hook to copy/replace values before the compile phasePropertius
C
1

i fixed this problem using cordova-custom-config plugin, then in config.xml inside the

<platform name="android">

put

<preference name="android-manifest/@android:versionCode" value="108" />

change the value to the wanted version code...

It worked perfectly to me.

Cobbler answered 5/1, 2016 at 17:27 Comment(1)
As of Cordova 6.5 this does not work...If added i get value as 1088Disparity
N
0

edit version code in AndroidManifest.xml(/platforms/android/AndroidManifest.xml)

<manifest android:hardwareAccelerated="true" android:versionCode="2"android:versionName="0.0.2" package="com.ionicframework.zinescreen841178"   xmlns:android="http://schemas.android.com/apk/res/android">

change the version code here not in the config.xml it wont update.then when you take a release build it will generate new version code for you.

Nynorsk answered 1/8, 2016 at 9:24 Comment(0)
C
0

In case someone is still stumbling over this one: it is still true on the currently latest cordova Version (6.4.0). If you don't have some Jenkins or other process to do this for you, you could put the version-code in its own line

<widget id="com.example.yourapp" 
    android-versionCode="1112" 
    version="1.1.3" 
    xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">

and then run this to up' it (e.g. use in your release- script):

VersionCode=`cat config.xml | grep android-versionCode | sed -e 's/.*android-versionCode=\"\(.*\)\"/\1/'`
VersionCode=$((VersionCode+1))
sed -i "s/.*android-versionCode=\"\(.*\)\"/    android-versionCode=\"$VersionCode\"/" config.xml

Yah, dirty but works :)

Caron answered 20/1, 2017 at 14:37 Comment(0)
H
0

You have no need to change AndroidManifest.xml as it is generated by cordova itself so every your changes in Manifest is reverted by cordova

And all of it is responsible is build.gradle located in platform folder

file --> platform/build.gradle

Search for

if (minSdkVersion >= 20) {defaultConfig.versionCode += 9} else if (minSdkVersion = 14) { defaultConfig.versionCode += 8 }

as your version code is updated from here you can change it or hard code it

Hallvard answered 20/3, 2017 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.