Debugging Android WebViews in Cordova hybrid applications running on kitkat

Now that Android OS version 4.4 (kitkat) supports using chrome for its web views, this means that a cordova hybrid application can be debugged from the Chrome remote web debugger plugin ADB. The code below relies on having USB debugging turned on the target phone.

Firstly, change the android build target to level 19 (kitkat) so the code below will compile. This is will need changing in platforms/android/AndroidManifest.xml as follows:

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />

Also the target will need to be changed in platforms/android/project.properties:

target=android-19

Add the following lines to your onCreate() method in your cordova android main view (found under platforms/android/src/...):

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    if(0 != (getApplicationInfo().flags = ApplicationInfo.FLAG_DEBUGGABLE)){
        Log.i("Your app", "Enabling web debugging");
        WebView.setWebContentsDebuggingEnabled(true);
    }
}

If you haven't already you'll need to import the following libs:

import android.os.Build;
import android.util.Log;
import android.content.pm.ApplicationInfo;
import android.webkit.WebView;

Then rebuild and run your android code with:

cordova -d build android
cordova -d run android

If it worked you'll see the Enabling web debugging log message in logcat.

Last updated: 28/11/2013