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.