Question

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

Was it helpful?

Solution

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" 

OTHER TIPS

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'

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.

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.

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.

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 :)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top