Home » Android » cordova – PhoneGap Android get the app version code

cordova – PhoneGap Android get the app version code

Posted by: admin May 14, 2020 Leave a comment


I am building a phonegap android app with jQueryMobile.

I want to build a function that checks the version code of the application with a webservice in order to suggest to update, if the app is outdated, prompting to the google play store.

From my searches, it looks like PhoneGap does not have a method for finding the version code.

Is there a way to do it:

  • without writing anything in the Java classes?
  • without manually storing an updated variable everytime I push a new version?

Or, if writing in the classes is essential, how do I call it from PhoneGap?

EDIT: I edited the accepted answer with a working solution! I hope everybody can profit from it.

EDIT (2): and here is the full plugin repo with instructions, feel free to fork it 🙂 https://github.com/gcatalfamo/Version

How to&Answers:

I know you asked w/o writing Java Classes but one way or another you need to write one. You need to write PhoneGap Plugin as Andre stated.

Create a custom PhoneGap plugin:

Create a new class in your app’s package:

package com.phonegap.plugins; //your package name

import org.json.JSONArray;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;

public class Version extends Plugin {

    public final String ACTION_GET_VERSION_NAME = "GetVersionName"; //not used in this version
    public final String ACTION_GET_VERSION_CODE = "GetVersionCode";

    public PluginResult execute(String action, JSONArray args, String callbackId) {
        PluginResult result = new PluginResult(Status.INVALID_ACTION);
        PackageManager packageManager = this.cordova.getActivity().getPackageManager();
        if(action.equals(ACTION_GET_VERSION_CODE)) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(this.cordova.getContext().getPackageName(), 0);
                result = new PluginResult(Status.OK, packageInfo.versionCode);
            catch (NameNotFoundException nnfe) {
                result = new PluginResult(Status.ERROR, nnfe.getMessage());

        return result;

In the plugins.xml, add the following line:

<plugin name="Version" value="your.package.Version" />

In your deviceready event add the following code:

var Version = function() {};
Version.prototype.getVersionCode = function(successCallback, failureCallback) {
    return cordova.exec(successCallback, failureCallback, 'Version', 'GetVersionCode', []);

if(!window.plugins) {
    window.plugins = {};
if (!window.plugins.version) {
    window.plugins.version = new Version();

Then, you can get the versionCode attribute by doing:

    function(versionCode) {
        //do something with versionCode
    function(errorMessage) {
        //do something with errorMessage


a little change required .. class should extend ” extends CordovaPlugin

then “this.cordova.getActivity().getPackageName()” will work.

In the plugins.xml, add the following line:

BUT for Cardova > 2.0 , you need to add in config.xml inside xml folder