1

I am building an Android application with Cordova which is running inside a webview. I know I can access the webview via chrome://inspect but it only works for debug build. It can't inspect the chrome in release APK. So is there a way to get the chrome console log via adb command?

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • Are you using a particular framework or tool to build your app or are you just putting HTML5/Javascript into a simple native (Java/Kotlin) Android app wrapping a WebView? – Morrison Chang Mar 24 '19 at 23:11
  • I am using `Cordova` and I have updated the question. Thanks. – Joey Yi Zhao Mar 24 '19 at 23:16
  • This question/answers may help [Debugging WebView in apps](https://stackoverflow.com/questions/47711418/debugging-webview-in-react-native-apps/48572075#48572075) – Jon Goodwin Mar 25 '19 at 00:52

2 Answers2

5

Couldn't find this anywhere on SO, here's what worked for me:

adb -d logcat chromium:I *:S

The -d indicating a physical device in my case. If all else fails just dump the results of adb logcat into a text file and do a search for "CONSOLE", that will give you the provider for your logcat filter. It seems this changes over time, and depending on your particular dev environment.

Ben Brian
  • 220
  • 3
  • 6
2

For getting the console log via adb see: Debugging a WebView (Ionic) app on Android via logcat

Try the above first as I seem to misread your question about debugging a WebView via remote debugging, which is what follows.

For a simple native Java/Kotlin Android app wrapping a WebView, the documentation is quite clear that all one needs to do is to enable the feature:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    WebView.setWebContentsDebuggingEnabled(true);
}

and additional code is required to limit it to debug version:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
    { WebView.setWebContentsDebuggingEnabled(true); }
}

Looking at what is in the Cordova Android repo:

Cordova Android - ... SystemWebViewEngine.java

the relevant code is here:

//Determine whether we're in debug or release mode, and turn on Debugging!
ApplicationInfo appInfo = webView.getContext().getApplicationContext().getApplicationInfo();
if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
    enableRemoteDebugging();
}

I also found this open ISSUE 500: Release version still have debugging enabled which implies that Android Emulators are allow for debugging even though the APK is packaged for release.

So my suggestions for remote debugging a WebView on a release APK are:

  1. Try to use the emulator for debugging with a release APK as it appears to be possible.

  2. Consider forking the Cordova Android code and create a special debug mode so that on device it would be possible to debug the contents of a WebView. Enabling such a feature could be hidden via a custom system property: How to define and use a system property in Android? For example say you defined the flag to be named com.example.myapp.debugwebview. You could set the property via command like adb setprop com.example.myapp.debugwebview 1 and on start/restart the app would enable WebView debugging. Note that this does require a more detailed understanding of Android to support such a feature, as well as the effort of supporting a fork.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77