如何远程调试webview?

问题描述 投票:0回答:7

如何调试/检查 apk webview 的元素。
我已经尝试过this,但它仅对 chrome 有帮助,对 apk 没有帮助。

请推荐我

android debugging android-webview inspect
7个回答
91
投票

试试这个:

  1. 在设备中启用开发者选项(设置-->关于手机-->在版本号上点击 7 次)

  2. 打开开发者选项并启用USB调试(在开发者选项中)

  3. 在您的自定义应用程序类或加载 Web 视图的 Activity 中添加此行

    // 如果您的构建处于调试模式,请启用 Web 视图检查

     if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
         WebView.setWebContentsDebuggingEnabled(true);
     }
    
  4. 打开 Chrome 并输入

    chrome://inspect/#devices
    ,您应该会在远程目标列表中看到您的设备

  5. 点击inspect进行调试。

更新: 您可以像下面这样简化它:

WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG)

12
投票

这对我有用,重写

onCreate
中的方法
MainActivity.java
并在方法
WebView.setWebContentsDebuggingEnabled(true)
中添加此行。

这是我的代码:

package com.myapp;

import com.facebook.react.ReactActivity;
import android.webkit.WebView;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "myapp";
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        //added this line with necessary imports at the top.
        WebView.setWebContentsDebuggingEnabled(true);
    }
}

构建您的代码,在手机中打开您的应用程序并转到 chrome://inspect ,您会看到您的应用程序列在那里。单击检查链接。


5
投票

如果您正在寻找一种为您没有有源代码的应用程序打开WebView调试的方法,那么可以这样做,但您需要反编译并重新编译该应用程序。

有关如何执行此操作的说明可以在这里找到:https://blog.speedfox.co.uk/articles/1524219174-Android_WebView_Hackery


3
投票

在react-native-webview中他们给出了针对IOS和Android的解释。

https://github.com/react-native-community/react-native-webview/blob/master/docs/Debugging.md

在 IOS 上帮助了我。


3
投票

要在Android应用程序中调试WebView,您必须在WebviewActivity中设置WebView.setWebContentsDebuggingEnabled(true)

打开 chrome://inspect/#device 进行调试。使用端口转发。 https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews


2
投票

我看到有一章是关于webView的,你尝试一下吗? https://developers.google.com/chrome-developer-tools/docs/remote-debugging#debugging-webviews

似乎需要:

  • 运行 Android 4.4 或更高版本的 Android 设备或模拟器,已启用 USB 调试,如 2. 在设备上启用 USB 调试。

  • Chrome 30 或更高版本。


0
投票

首先,您必须在您的应用程序中启用调试,正如其他人所展示的那样。 通过 USB 或 TCP 将手机连接至 comp。 然后打开基于 Chromium 的浏览器。 打开 chrome://inspect/#devices。你会看到下面的图片。 devtools 打开开发工具。 浏览“Elements”选项卡并找到具有此类 id 的容器。复制这部分。elements tab 运行“adb转发tcp:9222 localabstract:[复制的部分]”

现在您可以将端口 9222 转发到远程计算机并在那里打开调试器。

© www.soinside.com 2019 - 2024. All rights reserved.