blob: ce9d308b35f0ec245678765b67507ba2a8d42d4d [file] [log] [blame] [view]
Nate Fischerb8762b32019-02-12 00:14:411# Net debugging in WebView
2
Thomas44aabf02024-07-08 08:05:553This guide explains how to capture network logs [net logs](https://www.chromium.org/for-testers/providing-network-details/)
4for debugging WebView using DevTools. Net logs provide detailed information about
5network requests and responses made by your WebView, helping you diagnose
6network-related issues.
7
8## Net Logs In WebView DevTools
9
10*** note
11**Important**: Enabling net logging through DevTools requires enabling
12[setWebContentsDebuggingEnabled](https://developer.chrome.com/docs/devtools/remote-debugging/webviews)
13in your app. This setting is automatically enabled by default if you use a
14either a `userdebug` or `eng` Android image. it is also enabled by default if
15you use a debug app build. See [device setup](device-setup.md) and [commandline flags](commandline-flags.md)
16for more information.
17
18Net Logs in the DevTools are available from M128
19***
20
21### Steps:
22
231. Open [WebView DevTools](https://chromium.googlesource.com/chromium/src/+/a326b853919f482d7e9c67fe7e492ae060cb4851/android_webview/docs/developer-ui.md)
24and navigate to the "Flags" section.
251. Locate the "net-log" flag and enable it within the DevTools flags menu.
261. Launch your app and perform actions that trigger the network behavior you
27want to debug. Once you've reproduced the issue, close your app.
281. Locate and share the net log file generated using [Android's Quick Share](https://support.google.com/android/answer/9286773?hl=en).
29
30*** note
31Please note, there are file limitations:
32
33**File Size:** Net log files are limited to 100 MB each.
34
35**File Age:** Files older than 30 days will be automatically deleted.
36
37**Storage Capacity:** If the total net log storage exceeds 1 GB, older files
38will be deleted until the total storage is under the threshold.
39***
40
41## Manually setting the flag (WebView developers only)
Nate Fischerb8762b32019-02-12 00:14:4142
43WebView supports the `kLogNetLog` flag to log debugging network info to a JSON
44file on disk.
45
Nate Fischerb8762b32019-02-12 00:14:4146*** note
Thomas44aabf02024-07-08 08:05:5547**Important**: if you are unable to use net logs in WebView DevTools, all
48alternate approaches require applying commandline flags. **It's not typically
49possible for external reporters to apply commandline flags, so please do not
50ask them to follow this guide.**
Nate Fischer0f46ff82020-01-30 00:37:3851
52This guide is only for chromium developers who are set up for WebView
53development. Specifically, this guide requires the reader to use a `userdebug`
54or `eng` Android image, see [device setup](device-setup.md) and [commandline
55flags](commandline-flags.md) for more information.
Nate Fischerb8762b32019-02-12 00:14:4156***
57
Nate Fischerf58373a2019-09-24 02:33:5858### Python script
59
60If you have a chromium checkout, the preferred way to set the netlog flag is to
61use the `record_netlog.py` script like so:
62
Nate Fischer7d9828c2019-04-17 19:23:2763```shell
Nate Fischerf58373a2019-09-24 02:33:5864# Optional: set any flags of your choosing before running the script. Don't set
65# --log-net-log though; this is set by record_netlog.py.
Nate Fischer1531d3582019-08-06 21:31:1766$ build/android/adb_system_webview_command_line --enable-features=MyFeature,MyOtherFeature
Nate Fischer7d9828c2019-04-17 19:23:2767Wrote command line file. Current flags (in webview-command-line):
Nate Fischer1531d3582019-08-06 21:31:1768 005d1ac915b0c7d6 (bullhead-userdebug 6.0 MDB08M 2353240 dev-keys): --enable-features=MyFeature,MyOtherFeature
Nate Fischer7d9828c2019-04-17 19:23:2769
70# Replace "<app package name>" with your app's package name (ex. the
Nate Fischerf58373a2019-09-24 02:33:5871# WebView Shell is "org.chromium.webview_shell"). This script will set an
72# appropriate value for --log-net-log and handle setup/cleanup.
Nate Fischer7d9828c2019-04-17 19:23:2773$ android_webview/tools/record_netlog.py --package="<app package name>"
Nate Fischer7d9828c2019-04-17 19:23:2774Netlog will start recording as soon as app starts up. Press ctrl-C to stop recording.
75^C
76Pulling netlog to "netlog.json"
77```
78
Nate Fischerf58373a2019-09-24 02:33:5879Then import the JSON file (`netlog.json` in the working directory) into [the
80NetLog viewer][1].
Nate Fischer7d9828c2019-04-17 19:23:2781
Nate Fischer7d9828c2019-04-17 19:23:2782### Manual steps
83
Nate Fischera5a89622019-04-17 01:05:08841. Figure out the app's data directory
85 ```sh
86 # appPackageName is the package name of whatever app you're interested (ex.
87 # WebView shell is "org.chromium.webview_shell").
88 appDataDir="$(adb shell dumpsys package ${appPackageName} | grep 'dataDir=' | sed 's/^ *dataDir=//')" && \
89 ```
901. Pick a name for the JSON file. This must be under the WebView folder in the
91 app's data directory (ex. `jsonFile="${appDataDir}/app_webview/foo.json"`).
92 **Note:** it's important this is inside the data directory, otherwise
93 multiple WebView apps might try (and succeed) to write to the file
94 simultaneously.
Nate Fischerb8762b32019-02-12 00:14:41951. Kill the app, if running
Nate Fischerf58373a2019-09-24 02:33:58961. [Set the netlog flag](commandline-flags.md):
Nate Fischerb8762b32019-02-12 00:14:4197 ```sh
Nate Fischerf58373a2019-09-24 02:33:5898 FLAG_FILE=/data/local/tmp/webview-command-line
99 adb shell "echo '_ --log-net-log=${jsonFile}' > ${FLAG_FILE}"
Nate Fischerb8762b32019-02-12 00:14:41100 ```
1011. Restart the app. Reproduce whatever is of interest, and then kill the app
102 when finished
1031. Get the netlog off the device:
104 ```sh
Nate Fischerb8762b32019-02-12 00:14:41105 adb pull "${appDataDir}/app_webview/${jsonFile}"
Nate Fischerf58373a2019-09-24 02:33:58106 adb shell "rm '${appDataDir}/app_webview/${jsonFile}'"
Nate Fischerb8762b32019-02-12 00:14:41107 ```
Nate Fischerf58373a2019-09-24 02:33:581081. Optional: view the data in [the NetLog viewer][1]
1091. Optional: [clear the commandline flags](commandline-flags.md):
110 ```sh
111 FLAG_FILE=/data/local/tmp/webview-command-line
112 adb shell "rm ${FLAG_FILE}"
113 ```
114
115[1]: https://netlog-viewer.appspot.com/#import