Why does Cordova/Phonegap append 8 to my Android Version Code?

爷,独闯天下 提交于 2019-11-29 00:58:36
Paul Weber

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

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!

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!