How do I get the version string I defined in the config.xml
If you are not interested in using any third party plugin to achieve this, then I feel Cordova hooks is the way to go. You can come up with a before_build
hook to read the version from config.xml file and make use of the same in app.
Fortunately, this hook is readily available for you in the following link where they are reading version from config.xml and injecting it as a dependency wherever required.
Suggest you to also have a look at the official documentation of cordova hooks for better understanding.
In fact, you can also try out reading version info from config.xml and directly put it in some placeholder that you can create in your index.html
to display the version info. This can be done before cordova build using hooks which in turn uses windows batch file that does the file operations. You can checkout a working sample of cordova hook that uses batch file in my github page.
Based on the script that @Gandhi pointed to in his answer, I updated the script to make it work for me with the current cordova@8: I use it as "after_prepare" hook, not "before_build", as in the later case the changed file got overwritten again when cordova copied the stuff to the platform directory soon after the hook has been executed ...
The script uses xml2js, so be sure to exec npm i xml2js --save-dev
to make xml2js available.
#!/usr/bin/env node
// taken from https://www.bram.us/2015/01/04/cordova-build-hook-script-for-displaying-build-version-in-your-app/
// see https://stackoverflow.com/a/42650842
// This plugin replaces text in a file with the app version from config.xml.
// be sure to exec `npm i xml2js --save-dev` to make xml2js available
var wwwFileToReplace = "js/config.js";
var fs = require('fs');
var path = require('path');
var xml2js = require('xml2js');
function loadConfigXMLDoc(filePath) {
var json = "";
try {
var fileData = fs.readFileSync(filePath, 'ascii');
var parser = new xml2js.Parser();
parser.parseString(fileData.substring(0, fileData.length), function (err, result) {
//console.log("config.xml as JSON", JSON.stringify(result, null, 2));
json = result;
});
console.log("File '" + filePath + "' was successfully read.");
return json;
} catch (ex) {
console.log(ex)
}
}
function replace_string_in_file(filename, to_replace, replace_with) {
var data = fs.readFileSync(filename, 'utf8');
var result = data.replace(new RegExp(to_replace, "g"), replace_with);
fs.writeFileSync(filename, result, 'utf8');
//console.log("replaced in ", filename, "(", to_replace, " -> ", replace_with);
var data2 = fs.readFileSync(filename, 'utf8');
console.log(data2);
}
module.exports = function(context) {
// var rootdir = process.argv[2]; // old cordova version
var rootdir = context.opts.projectRoot;
console.log("projectRoot=", rootdir);
var configXMLPath = "config.xml";
var rawJSON = loadConfigXMLDoc(configXMLPath);
var version = rawJSON.widget.$.version;
console.log("Version:", version);
// var currentBuildPlatforms = process.env.CORDOVA_PLATFORMS.split(","); // old cordova version
var currentBuildPlatforms = context.opts.cordova.platforms;
//console.log(JSON.stringify(context));
console.log("Current build platforms: ", currentBuildPlatforms);
if (rootdir) {
currentBuildPlatforms.forEach(function(val, index, array) {
var wwwPath = "";
switch(val) {
case "ios":
wwwPath = "platforms/ios/www/";
break;
case "android":
wwwPath = "platforms/android/assets/www/";
break;
default:
console.log("Unknown build platform: " + val);
}
var fullfilename = path.join(rootdir, wwwPath + wwwFileToReplace);
if (fs.existsSync(fullfilename)) {
replace_string_in_file(fullfilename, "%%VERSION%%", version);
console.log("Replaced version in file: " + fullfilename);
}
});
}
}
Include this hook in your config.xml
:
<hook src="scripts/after_prepare_read_app_version.js" type="after_prepare" />