35

I am currently working on a Cordova project and had the problem that 8 was appended mysteriously to the versionCode of my project. For example: My original Version code was 100, the new Version code is 1008

I tracked the problem through my whole build pipeline and found the responsible code in the Gradle Build script.

if (Boolean.valueOf(cdvBuildMultipleApks)) {
    productFlavors {
        armv7 {
            versionCode cdvVersionCode ?: defaultConfig.versionCode + 2
            ndk {
                abiFilters "armeabi-v7a", ""
            }
        }
        x86 {
            versionCode cdvVersionCode ?: defaultConfig.versionCode + 4
            ndk {
                abiFilters "x86", ""
            }
        }
        all {
            ndk {
                abiFilters "all", ""
            }
        }
    }
} else if (!cdvVersionCode) {
  def minSdkVersion = cdvMinSdkVersion ?: privateHelpers.extractIntFromManifest("minSdkVersion")
  // Vary versionCode by the two most common API levels:
  // 14 is ICS, which is the lowest API level for many apps.
  // 20 is Lollipop, which is the lowest API level for the updatable system webview.
  if (minSdkVersion >= 20) {
    defaultConfig.versionCode += 9
  } else if (minSdkVersion >= 14) {
    defaultConfig.versionCode += 8
  }
}

I am not sure why cordova thinks it is correct to change my version code, and additionally, I am not sure if this behaviour is correct ... shouldnt it add the number and not append it?

Can I just remove the corresponding section in the build.gradle or is there some hidden sense behind this behaviour?

Paul Weber
  • 6,518
  • 3
  • 43
  • 52

4 Answers4

22

O.K. seems like this is a major unresolved bug in Cordova Version 5 and up. Here is the link to the ticket.

I had no problem when removing the offending code from my build.gradle

jakub.g
  • 38,512
  • 12
  • 92
  • 130
Paul Weber
  • 6,518
  • 3
  • 43
  • 52
  • This is nice. I thought I made a mistake, it's still in 6.0.0 – Mathijs Segers Mar 21 '16 at 11:30
  • 1
    It seems the fix was [merged recently](https://github.com/apache/cordova-android/pull/270) to cordova-android recently but not yet released (cordova-android 5.1.1 doesn't have the fix) – jakub.g May 19 '16 at 12:37
  • 7
    This seems to be fixed in Cordova 6.2.0. However, now that the extra digit is not appended to the version code any longer, the APKs I build using this version of Cordova always have smaller version code than the previous ones and hence google play does not accept these APKs. – Ehsan Khaveh Jul 12 '16 at 13:02
  • To clarify, this is fixed in cordova-android 5.2.0; the cordova CLI version doesn't matter. – JW. Nov 05 '16 at 21:55
7

The platforms\android\build.gradle script will add 4, 2, 8 or 9 to the version file dependent on targeted architecure - arm / x86 or the targeted api version of android .

I had a situation where my project had a "8" appended to the version number, and this was uploaded to the Play store. Further builds seemed to have dropped the 8, which meant I was unable to upload further updates - a cordova prepare command recreates the AndroidManifest.xml file, overriding manual changes to this.

The version issue can be addressed by creating a platforms\android\gradle.properties file with the contents cdvVersionCode=13008

Alternatively, in my case, I inserted a android-versionCode attribute into the config.xml:

<widget xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps" id="uk.co.my.app" android-versionCode="103008" version="1.3.0" xmlns="http://www.w3.org/ns/widgets" defaultlocale="en-GB">

The desired android version, in my case 103008 is then correctly written to the AndroidManifest.xml file used for the build.

Downside is having to manually update the android version, upside, can upload the apk!

ChilledFlame
  • 107
  • 1
  • 2
6

For those who want to keep the end '8', I have write a after_prepare hook to make it easy, no need to maintain the android-versionCode in config.xml manually mentioned by @ChilledFlame.

Note: if you do not keep the end '8', when you submit your app to the appstore, your android version code is smaller then previous which built by Cordova 5, you may encounter "version code downgrade issue".

create a file under folder hooks/after_prepare/, add the following code.

#!/usr/bin/env node

var path = require('path');
var fs = require('fs');
var xml2js = require('xml2js');

function xmlFileToJs(filename, cb) {
    var filepath = path.normalize(path.join(__dirname, filename));
    fs.readFile(filepath, 'utf8', function (err, xmlStr) {
        if (err) throw (err);
        xml2js.parseString(xmlStr, {}, cb);
    });
}

function jsToXmlFile(filename, obj, cb) {
    var filepath = path.normalize(path.join(__dirname, filename));
    var builder = new xml2js.Builder();
    var xml = builder.buildObject(obj);
    fs.writeFile(filepath, xml, cb);
}

var androidManifestRPath = '../../platforms/android/AndroidManifest.xml';
xmlFileToJs(androidManifestRPath, function(error, data) {
  var config = data;
  config.manifest.$['android:versionCode'] += '8';
  jsToXmlFile(androidManifestRPath, config)
});

or download from this link: append_8_to_version_code.js

Leon
  • 1,935
  • 3
  • 23
  • 36
  • thanks for the hook. Can you tell me how can I add it to my build. I am still facing the same issue. Have you found any other solution? – krv Oct 13 '17 at 04:32
  • create this file in your project: hooks/after_prepare/append_8_to_version_code.js. @krv – Leon Oct 13 '17 at 07:39
  • I miss understood I wanted to remove the 8. Why is the 8 being added and why would anyone want to keep it for whom you wrote this post? – krv Oct 14 '17 at 03:34
  • @krv it's for old apps which have been posted to application store with the 8. As I have claimed, `if you do not keep the end '8', when you submit your app to the appstore, your android version code is smaller then previous which built by Cordova 5, you may encounter "version code downgrade issue".` – Leon Oct 16 '17 at 03:59
  • Yes, Thanks. Currently, the versionCode issue is solved by adding it to the build CLI command as follows `cordova build android -- --versionCode=007` – krv Oct 16 '17 at 04:02
  • Yeah, this solution is just for those who want to keep 8 to solve the app upload issue. It seems you are facing different issue. Anyway, you fix it. – Leon Oct 16 '17 at 04:05
0

After removing versionCode modifications from ./platforms/android/build.gradle, an "8" was still being appended to the versionCode in my APK.

A "cordova clean" was required before the newly generated APK would have the correct versionCode in it.

J. McNerney
  • 576
  • 4
  • 15