远程调试 Android WebView

使用 Microsoft Edge 开发人员工具在本机 Android 应用中调试 Android WebView,如下所示:

  • 在本机 Android 应用中启用 Android WebView 调试;在 Microsoft Edge DevTools 中调试 Android WebViews。
  • 若要显示已启用调试的 Android WebView 列表,请转到 edge://inspect
  • 调试 Android WebViews 的方式与通过 远程调试调试网页的方式相同。

详细信息如下所示。

在 Android 4.4 (KitKat) 或更高版本上,使用 DevTools 调试本机 Android 应用中的 WebView 内容。

Android WebView 与 Microsoft Edge WebView2 无关。

配置 Android WebViews 以调试

必须在应用中启用 Android WebView 调试。 若要启用 Android WebView 调试,请在 类上运行 WebViewsetWebContentsDebuggingEnabled 静态方法。

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

该设置适用于应用的所有 Android WebView。

提示

Android WebView 调试不受应用清单中标志状态 debuggable 的影响。 如果只想在标志为 truedebuggable启用 Android WebView 调试,请在运行时测试标志。

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

在 DevTools 中打开 Android WebView

若要显示在设备上运行的已启用调试的 Android WebView 列表,请转到 edge://inspect

若要开始调试,请在要调试的 Android WebView 下,单击“ 检查”。 使用 DevTools 的方式与使用远程浏览器选项卡的方式相同。

疑难解答

如果 Android WebView 未显示在 edge://inspect 页面上:

  • 验证是否已为应用启用 Android WebView 调试。

  • 在设备上,使用要调试的 Android WebView 打开应用。 然后,刷新 edge://inspect

注意

此页面的某些部分是根据 Google 创建和共享的作品所做的修改,并根据 Creative Commons Attribution 4.0 International License 中描述的条款使用。 原始页面 在此处 找到,由 Meggin Kearney 创作。

Creative Commons 许可证 本作品根据 Creative Commons Attribution 4.0 International License 获得许可。