Cordova build hook script for displaying build version in your app


#!/usr/bin/env node
// This plugin replaces text in a file with the app version from config.xml.

var wwwFileToReplace = "js/build.js";
var fs = require('fs');
var path = require('path');
var rootdir = process.argv[2];
function loadConfigXMLDoc(filePath) {
    var fs = require('fs');
    var xml2js = require('xml2js');
    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) {
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');
var configXMLPath = "config.xml";
var rawJSON = loadConfigXMLDoc(configXMLPath);
var version = rawJSON.widget.$.version;
console.log("Version:", version);
var rootdir = process.argv[2];
var currentBuildPlatforms = process.env.CORDOVA_PLATFORMS.split(",");
console.log("Current build platforms: ", currentBuildPlatforms);
if (rootdir) {
    currentBuildPlatforms.forEach(function(val, index, array) {
        var wwwPath = "";
        switch(val) {
            case "ios":
                wwwPath = "platforms/ios/www/";
            case "android":
                wwwPath = "platforms/android/assets/www/";
                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);

Essentially it string-replaces %%VERSION%% in js/build.js (or any file you want) with version as specified in config.xml. To use it in your app make it available in a constant which you then inject as a dependency where needed:

// js/build.js
angular.module('equipmentShare').constant('BUILD', {
    VERSION: "%%VERSION%%" // %%VERSION%% will be replaced with the actual version from config.xml

// appController.js
angular.module('equipmentShare').controller('AppController', function ($scope, BUILD) {
    $scope.appVersion = BUILD.VERSION;

Cordova build hook, to use build version from config.xml in your hybrid app →


Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Join the Conversation


  1. Hi, I have tried using build version hook in the solution but the version is still %VERSION% text, any ideas?
    Have added buildversion.js in hook directory in visual studio and added hook key in config.xml file.

  2. I don’t understand how to implement this. Can you break it down where all this code is going?

  3. Hooks are ok, but the main problem I face here is to find the path to the file. Apparently these are generated or copied under platforms with different random name ?

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.