如何调试/检查 apk webview 的元素。
我已经尝试过this,但它仅对 chrome 有帮助,对 apk 没有帮助。
请推荐我
试试这个:
在设备中启用开发者选项(设置-->关于手机-->在版本号上点击 7 次)
打开开发者选项并启用USB调试(在开发者选项中)
在您的自定义应用程序类或加载 Web 视图的 Activity 中添加此行
// 如果您的构建处于调试模式,请启用 Web 视图检查
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
WebView.setWebContentsDebuggingEnabled(true);
}
打开 Chrome 并输入
chrome://inspect/#devices
,您应该会在远程目标列表中看到您的设备
点击inspect进行调试。
更新: 您可以像下面这样简化它:
WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG)
这对我有用,重写
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 ,您会看到您的应用程序列在那里。单击检查链接。
如果您正在寻找一种为您没有有源代码的应用程序打开WebView调试的方法,那么可以这样做,但您需要反编译并重新编译该应用程序。
有关如何执行此操作的说明可以在这里找到:https://blog.speedfox.co.uk/articles/1524219174-Android_WebView_Hackery
在react-native-webview中他们给出了针对IOS和Android的解释。
https://github.com/react-native-community/react-native-webview/blob/master/docs/Debugging.md
在 IOS 上帮助了我。
要在Android应用程序中调试WebView,您必须在WebviewActivity中设置WebView.setWebContentsDebuggingEnabled(true)
打开 chrome://inspect/#device 进行调试。使用端口转发。 https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews
我看到有一章是关于webView的,你尝试一下吗? https://developers.google.com/chrome-developer-tools/docs/remote-debugging#debugging-webviews
似乎需要:
运行 Android 4.4 或更高版本的 Android 设备或模拟器,已启用 USB 调试,如 2. 在设备上启用 USB 调试。
Chrome 30 或更高版本。