How to retrieve the app version, app name, package name, version code and version number in Cordova

Although normally the user won't be interested in the version of your application, the weird package name and other kind of information that you can find in the build settings, it may be useful for geeks that really care about it, like you.

If you need this information in your hybrid application, today is your lucky day as someone already wrote a plugin that implements the native code for you.

1. Install cordova-plugin-app-version

The cordova plugin app version reads the version of your app from the target build settings. You can install this plugin by switching to your project directory with the terminal and running the following cordova command:

cordova plugin add cordova-plugin-app-version

After the installation you will be able to access the getAppVersion object in the cordova global object in the window. This object exposes the methods of the plugin. This plugin was written by the guys at @Whiteoctober and works for iOS and Android. For more information about this plugin, please visit the official repository at Github here.

2. Using the plugin

The plugin allows you to retrieve the information in 2 ways according to the way you work. As in cordova most of the JS code is asynchronous for obvious reasons, the plugin implements not only callbacks but Promises too, so if you don't know what Promises in JavaScript are, please read this article first.

With Callbacks

The tipical procedure uses callbacks as any other plugin:

1. Get App Name

cordova.getAppVersion.getAppName(function(name){
    // My App Name
    console.log("App Name", name);
});

2. Get Package Name

cordova.getAppVersion.getPackageName(function(pkgname){
    // com.companyname.appname
    console.log("Package Name", pkgname);
});

3. Get Version Code

cordova.getAppVersion.getVersionCode(function(version){
    // 10000
    console.log("version code", version);
});

4. Get Version Number

cordova.getAppVersion.getVersionNumber(function(versionNumber){
    // 1.0.0
    console.log("version number", versionNumber);
});

With the Promises Interface

The plugin uses promises (if available) to return the value, so you can add a Promise polyfill to add support to this feature:

1. Get App Name

cordova.getAppVersion.getAppName().then(function(name){
    // My App Name
    console.log("App Name", name);
});

2. Get Package Name

cordova.getAppVersion.getPackageName().then(function(pkgname){
    // com.companyname.appname
    console.log("Package Name", pkgname);
});

3. Get Version Code

cordova.getAppVersion.getVersionCode().then(function(version){
    // 10000
    console.log("version code", version);
});

4. Get Version Number

cordova.getAppVersion.getVersionNumber().then(function(versionNumber){
    // 1.0.0
    console.log("version number", versionNumber);
});

Happy coding !

Become a more social person