Cordova 2.9.0: “[ERROR] Error initializing Cordova: Class not found”


When I preview a page of my Android app with cordova.js (2.9.0) in Google Chrome, the page appears with an alert message saying,

The page at file://localhost/ says:
[ ]
gap:["Device","getDeviceInfo","Device1830682625"]
Cancel, OK

If I click on OK another alert appears, saying:

The page at file://localhost/ says:
[ ]
gap:["Device","getDeviceInfo","Device889844386"]
Cancel, OK

When I click on OK, the alert stops with no issues. These alerts pop up only on pages where cordova.js (2.9.0) are called, such as when the page contains external links and invokes the InAppBrowser. No other pages are affected.

I get these alerts on the Mac and PC. I’ve never received these alerts before 2.9.0.

On my Android Nexus 7 device these alerts don’t show. Instead, I get this alert on pages containing cordova.js:

Alert
[ERROR] Error initializing Cordova: Class not found
OK

I filed a bug report with Jira. Still, I wanted to figure this out so I could get my app on the store.

Since these are alert messages, they must be generated from cordova.js, I reasoned. So I opened the file and did a search for “alert.” For the alerts found on Chrome, I found this on line 5139:

Device.prototype.getInfo = function(successCallback, errorCallback) {
argscheck.checkArgs('fF', 'Device.getInfo', arguments);
exec(successCallback, errorCallback, "Device", "getDeviceInfo", []);
};

I commented out these four lines with /* and */ before and after the section and tried again in Chrome. The pages worked with no problems.

What about “Error initializing Cordova”? I found this function containing the device’s alert on line 5126:

function Device() {
this.available = false;
this.platform = null;
this.version = null;
this.uuid = null;
this.cordova = null;
this.model = null;

var me = this;

channel.onCordovaReady.subscribe(function() {
me.getInfo(function(info) {
var buildLabel = info.cordova;
if (buildLabel != CORDOVA_JS_BUILD_LABEL) {
buildLabel += ' JS=' + CORDOVA_JS_BUILD_LABEL;
}
me.available = true;
me.platform = info.platform;
me.version = info.version;
me.uuid = info.uuid;
me.cordova = buildLabel;
me.model = info.model;
channel.onCordovaInfoReady.fire();
},function(e) {
me.available = false;
utils.alert("[ERROR] Error initializing Cordova: " + e); // line 5126
});
});
}

I decided to comment out line the entire section that contains the function above:

function Device() {
this.available = false;
this.platform = null;
this.version = null;
this.uuid = null;
this.cordova = null;
this.model = null;

var me = this;

channel.onCordovaReady.subscribe(function() {
me.getInfo(function(info) {
var buildLabel = info.cordova;
if (buildLabel != CORDOVA_JS_BUILD_LABEL) {
buildLabel += ' JS=' + CORDOVA_JS_BUILD_LABEL;
}
me.available = true;
me.platform = info.platform;
me.version = info.version;
me.uuid = info.uuid;
me.cordova = buildLabel;
me.model = info.model;
channel.onCordovaInfoReady.fire();
},function(e) {
me.available = false;
utils.alert("[ERROR] Error initializing Cordova: " + e);
});
});
}

/**
* Get device info
*
* @param {Function} successCallback The function to call when the heading data is available
* @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
*/

Device.prototype.getInfo = function(successCallback, errorCallback) {
argscheck.checkArgs('fF', 'Device.getInfo', arguments);
exec(successCallback, errorCallback, "Device", "getDeviceInfo", []);
};

module.exports = new Device();

});

Chrome tools just shows:

Uncaught SyntaxError: Unexpected token /

… the same line in which I had placed my final */

I created another signed app and loaded it in my device. The alert did not show. However, the InAppBrowser (IAB) failed to work; external links opened in a browser. Clearly, IAB depends on the code I commented out. This workaround is therefore unsatisfactory.

I removed the comments and just commented out this line that shows on the device:

//    utils.alert("[ERROR] Error initializing Cordova: " + e);

Pulling up the page in Chrome with Tools shows me this:

XMLHttpRequest cannot load file://localhost/Users/.../assets/www/cordova_plugins.json. Cross origin requests are only supported for HTTP.

That’s a known error and seems to be safe to ignore.

And this was in Tools console …

Failed to load resource
 file://localhost/Users/dragonhu/Documents/workspace/doubtbusters/assets/www/cordova_plugins.js

That is supposed to be fixed for the next release. And also in the console …

Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only. cordova.js:912

Line 912 in cordovo.js is the line in red, and is a console output:

androidExec.setJsToNativeBridgeMode = function(mode) { if (mode == jsToNativeModes.JS_OBJECT && !window._cordovaNative) {     console.log('Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only.'); mode = jsToNativeModes.PROMPT; } nativeApiProvider.setPreferPrompt(mode == jsToNativeModes.PROMPT); jsToNativeBridgeMode = mode; };

I commented out that line. I tested in my device and all is working, including IAB.

I am going to release my app with my fingers crossed! I uploaded it to Google Play, and for so many problems, I’m surprised it accepted the app on the very first upload!

6 thoughts on “Cordova 2.9.0: “[ERROR] Error initializing Cordova: Class not found”

  1. I have the same issue with the alerts popping up, it was because i was trying to use the android version of cordova.js for iOS. Check to make sure you have the correct version!

  2. Hi..After following your instructions i resolved the popup alerts..but now i got a error again as “cordova.js:843 Uncaught TypeError: androidExec.setJsToNativeBridgeMode is not a function” can you help me to resolve this error???
    Thankyou.

Leave a comment

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