Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap not firing deviceready on Android 4.2

I had teh same problem on my Nexus 7 with Android 4.2 but it works when I change the AndroidManifest targetSdkVersion to "16"

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />   (Before it was ="17")

I hope this works for someone.

Diego


It's 1 am and I finally have my app running again. Android 4.2 includes some apparently security related changes to webView.addJavascriptInterface, which stops the default bridge mode from working.

I expect there will be a better fix in the next version, but if you need a quick fix, try this update to CordovaWebView.exposeJsInterface

private void exposeJsInterface() {
    int SDK_INT = Build.VERSION.SDK_INT;
    boolean isHoneycomb = (SDK_INT >= Build.VERSION_CODES.HONEYCOMB && SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2);
    if (isHoneycomb || (SDK_INT < Build.VERSION_CODES.GINGERBREAD)) {
        Log.i(TAG, "Disabled addJavascriptInterface() bridge since Android version is old.");
        // Bug being that Java Strings do not get converted to JS strings automatically.
        // This isn't hard to work-around on the JS side, but it's easier to just
        // use the prompt bridge instead.
        return;            
    } else if (SDK_INT < Build.VERSION_CODES.HONEYCOMB && Build.MANUFACTURER.equals("unknown")) {
        // addJavascriptInterface crashes on the 2.3 emulator.
        Log.i(TAG, "Disabled addJavascriptInterface() bridge callback due to a bug on the 2.3 emulator");
        return;

    } else if (SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {            
        Log.i(TAG, "Disabled addJavascriptInterface() bridge callback for 4.2");
        return;


    }
    this.addJavascriptInterface(exposedJsApi, "_cordovaNative");
}

In my case auto generated index.html was referring to phonegap.js

so instead of

<script type="text/javascript" src="phonegap.js"></script>

I put

<script type="text/javascript" src="cordova.js"></script>

Apparently this is fixed in Phonegap 2.3.0.

Also encountered this problem on Android 4.2, my project was using Phonegap 2.2.

Updated following this simple upgrade procedure - problem solved.

Hence, after the upgrade to 2.3.0, setting android:targetSdkVersion="17" is ok again.