Android WebView 诊断与排查问题的方法和技巧

0
回复
252
查看
[复制链接]

40

主题

60

帖子

3386

安币

管理员

Rank: 9Rank: 9Rank: 9

发表于 2020-5-6 16:00:01 | 显示全部楼层 |阅读模式
如果对本篇文章感兴趣,请前往,原文地址:http://www.apkbus.com/blog-720372-84012.html

WebView,是安卓中很重要的一个组件,我们的应用中集成WebView后,可能会遇到各种各样的问题,这里简单介绍一些Android WebView 诊断与排查问题的方法,希望对于大家有这方面的问题的朋友有所帮助。
开启DiagnosableWebViewClient日志输出

[color=#93A1A1 !important]1[color=#93A1A1 !important]2[color=#93A1A1 !important]3[color=#93A1A1 !important]4[color=#93A1A1 !important]5[color=#93A1A1 !important]6[color=#93A1A1 !important]7[color=#93A1A1 !important]8[color=#93A1A1 !important]9[color=#93A1A1 !important]10[color=#93A1A1 !important]11[color=#93A1A1 !important]12[color=#93A1A1 !important]13[color=#93A1A1 !important]14[color=#93A1A1 !important]15[color=#93A1A1 !important]16[color=#93A1A1 !important]17[color=#93A1A1 !important]18[color=#93A1A1 !important]19[color=#93A1A1 !important]20[color=#93A1A1 !important]21[color=#93A1A1 !important]22[color=#93A1A1 !important]23[color=#93A1A1 !important]24[color=#93A1A1 !important]25[color=#93A1A1 !important]26[color=#93A1A1 !important]27[color=#93A1A1 !important]28[color=#93A1A1 !important]29[color=#93A1A1 !important]30[color=#93A1A1 !important]31[color=#93A1A1 !important]32[color=#93A1A1 !important]33[color=#93A1A1 !important]34[color=#93A1A1 !important]35[color=#93A1A1 !important]36[color=#93A1A1 !important]37[color=#93A1A1 !important]38[color=#93A1A1 !important]39[color=#93A1A1 !important]40[color=#93A1A1 !important]41[color=#93A1A1 !important]42[color=#CB4B16 !important]package [color=#B58900 !important]com.droidyue.webview.webviewclient[color=#268BD2 !important]import [color=#268BD2 !important]android.[color=#268BD2 !important]net.[color=#268BD2 !important]http.[color=#268BD2 !important]SslError[color=#268BD2 !important]import [color=#268BD2 !important]android.[color=#268BD2 !important]webkit.*[color=#268BD2 !important]import [color=#268BD2 !important]com.[color=#268BD2 !important]droidyue.[color=#268BD2 !important]common.[color=#268BD2 !important]debugMessage[color=#268BD2 !important]import [color=#268BD2 !important]com.[color=#268BD2 !important]droidyue.[color=#268BD2 !important]webview.[color=#268BD2 !important]ext.[color=#268BD2 !important]toSimpleString[color=#93A1A1 !important]/**[color=#93A1A1 !important] * 诊断(错误信息)的WebViewClient,会以日志输出形式输出错误信息,便于发现网页的问题[color=#93A1A1 !important] */[color=#268BD2 !important]open [color=#CB4B16 !important]class [color=#D33682 !important]DiagnosableWebViewClient : [color=#268BD2 !important]WebViewClient() {    [color=#CB4B16 !important]override [color=#CB4B16 !important]fun [color=#268BD2 !important]onReceivedError([color=#268BD2 !important]view: [color=#268BD2 !important]WebView?, [color=#268BD2 !important]errorCode: [color=#268BD2 !important]Int, [color=#268BD2 !important]description: [color=#268BD2 !important]String?, [color=#268BD2 !important]failingUrl: [color=#268BD2 !important]String?) {        [color=#268BD2 !important]super.[color=#268BD2 !important]onReceivedError([color=#268BD2 !important]view, [color=#268BD2 !important]errorCode, [color=#268BD2 !important]description, [color=#268BD2 !important]failingUrl)        [color=#268BD2 !important]debugMessage([color=#2AA198 !important]"onReceivedError", [color=#2AA198 !important]"errorCode", [color=#268BD2 !important]errorCode, [color=#2AA198 !important]"description", [color=#268BD2 !important]description,            [color=#2AA198 !important]"failingUrl", [color=#268BD2 !important]failingUrl, [color=#2AA198 !important]"webview.info", [color=#268BD2 !important]view?.[color=#268BD2 !important]toSimpleString())    }    [color=#CB4B16 !important]override [color=#CB4B16 !important]fun [color=#268BD2 !important]onReceivedError([color=#268BD2 !important]view: [color=#268BD2 !important]WebView?, [color=#268BD2 !important]request: [color=#268BD2 !important]WebResourceRequest?, [color=#268BD2 !important]error: [color=#268BD2 !important]WebResourceError?) {        [color=#268BD2 !important]super.[color=#268BD2 !important]onReceivedError([color=#268BD2 !important]view, [color=#268BD2 !important]request, [color=#268BD2 !important]error)        [color=#268BD2 !important]debugMessage([color=#2AA198 !important]"onReceivedError", [color=#2AA198 !important]"request", [color=#268BD2 !important]request?.[color=#268BD2 !important]toSimpleString(), [color=#2AA198 !important]"error", [color=#268BD2 !important]error?.[color=#268BD2 !important]toSimpleString(),            [color=#2AA198 !important]"webview.info", [color=#268BD2 !important]view?.[color=#268BD2 !important]toSimpleString())    }    [color=#CB4B16 !important]override [color=#CB4B16 !important]fun [color=#268BD2 !important]onSafeBrowsingHit([color=#268BD2 !important]view: [color=#268BD2 !important]WebView?, [color=#268BD2 !important]request: [color=#268BD2 !important]WebResourceRequest?, [color=#268BD2 !important]threatType: [color=#268BD2 !important]Int, [color=#268BD2 !important]callback: [color=#268BD2 !important]SafeBrowsingResponse?) {        [color=#268BD2 !important]super.[color=#268BD2 !important]onSafeBrowsingHit([color=#268BD2 !important]view, [color=#268BD2 !important]request, [color=#268BD2 !important]threatType, [color=#268BD2 !important]callback)        [color=#268BD2 !important]debugMessage([color=#2AA198 !important]"onSafeBrowsingHit", [color=#2AA198 !important]"request", [color=#268BD2 !important]request?.[color=#268BD2 !important]toSimpleString(), [color=#2AA198 !important]"threatType", [color=#268BD2 !important]threatType,            [color=#2AA198 !important]"webview.info", [color=#268BD2 !important]view?.[color=#268BD2 !important]toSimpleString())    }    [color=#CB4B16 !important]override [color=#CB4B16 !important]fun [color=#268BD2 !important]onReceivedHttpError([color=#268BD2 !important]view: [color=#268BD2 !important]WebView?, [color=#268BD2 !important]request: [color=#268BD2 !important]WebResourceRequest?, [color=#268BD2 !important]errorResponse: [color=#268BD2 !important]WebResourceResponse?) {        [color=#268BD2 !important]super.[color=#268BD2 !important]onReceivedHttpError([color=#268BD2 !important]view, [color=#268BD2 !important]request, [color=#268BD2 !important]errorResponse)        [color=#268BD2 !important]debugMessage([color=#2AA198 !important]"onReceivedHttpError", [color=#2AA198 !important]"request", [color=#268BD2 !important]request, [color=#2AA198 !important]"errorResponse", [color=#268BD2 !important]errorResponse?.[color=#268BD2 !important]toSimpleString(),            [color=#2AA198 !important]"webview.info", [color=#268BD2 !important]view?.[color=#268BD2 !important]toSimpleString())    }    [color=#CB4B16 !important]override [color=#CB4B16 !important]fun [color=#268BD2 !important]onReceivedSslError([color=#268BD2 !important]view: [color=#268BD2 !important]WebView?, [color=#268BD2 !important]handler: [color=#268BD2 !important]SslErrorHandler?, [color=#268BD2 !important]error: [color=#268BD2 !important]SslError?) {        [color=#268BD2 !important]super.[color=#268BD2 !important]onReceivedSslError([color=#268BD2 !important]view, [color=#268BD2 !important]handler, [color=#268BD2 !important]error)        [color=#268BD2 !important]debugMessage([color=#2AA198 !important]"onReceivedSslError", [color=#2AA198 !important]"error", [color=#268BD2 !important]error, [color=#2AA198 !important]"webview.info", [color=#268BD2 !important]view?.[color=#268BD2 !important]toSimpleString())    }}

举个例子

WebView页面出现了白屏,不展示任何内容,如下图

利用上面支持的内容,我们查看错误输出日志
[color=#93A1A1 !important]1[color=#93A1A1 !important]2[color=#93A1A1 !important]3D debugMessage: ConcreteWebViewClient;onReceivedSslError error primary error: 3 certificate: Issued to: [color=#268BD2 !important]CN=sni.cloudflaressl.com,O=Cloudflare[color=#DC322F !important]\, Inc.,L=San Francisco,ST=CA,C=US;D debugMessage: Issued by: [color=#268BD2 !important]C=NZ,ST=Auckland,L=Auckland,O=XK72 Ltd,OU=https://charlesproxy.com/ssl,CN=Charles Proxy CA (4 Sep 2018[color=#DC322F !important]\, bogon);D debugMessage:  on URL: https://droidyue.com/ webview.info [color=#268BD2 !important]url=https://droidyue.com/;originalUrl=null

通过查找源码(SslError.java)我们了解到

  • errorCode 为 3,代表证书不信任。
这其中的缘由是

  • 我们在设备上安装的charles证书,属于用户添加的证书
  • 出于应用安全的目的,Android 7及之后默认不信任用户添加的证书(Android 7 之前是默认信任用户添加的证书)
  • 当我们将App的编译目标提到24及其以上,系统就会激活这一安全限制。
所以,我们按照这篇文章解决Android手机连接Charles Unknown问题的方案,允许App在debug版本下信任用户证书就可以解决问题了。
Console日志查看

比如,我们有这样一段Javascript代码处理console输出。
[color=#93A1A1 !important]1[color=#93A1A1 !important]2[color=#93A1A1 !important]3[color=#93A1A1 !important]4[color=#93A1A1 !important]5[color=#B58900 !important]console.[color=#B58900 !important]debug([color=#2AA198 !important]"console.debug");[color=#B58900 !important]console.[color=#B58900 !important]log([color=#2AA198 !important]"console.log");[color=#B58900 !important]console.[color=#B58900 !important]info([color=#2AA198 !important]"console.info");[color=#B58900 !important]console.[color=#B58900 !important]warn([color=#2AA198 !important]"console.warn");[color=#B58900 !important]console.[color=#B58900 !important]error([color=#2AA198 !important]"console.error")

我们使用
[color=#93A1A1 !important]1adb logcat | grep [color=#2AA198 !important]"chromium" --line-buffered --color=always | grep CONSOLE --color=always

可以过滤出WebView CONSOLE的日志输出
[color=#93A1A1 !important]1[color=#93A1A1 !important]2[color=#93A1A1 !important]3[color=#93A1A1 !important]4[color=#93A1A1 !important]5 I chromium: [INFO:CONSOLE(2)] [color=#2AA198 !important]"console.debug", [color=#859900 !important]source:  (2) I chromium: [INFO:CONSOLE(3)] [color=#2AA198 !important]"console.log", [color=#859900 !important]source:  (3) I chromium: [INFO:CONSOLE(4)] [color=#2AA198 !important]"console.info", [color=#859900 !important]source:  (4) I chromium: [INFO:CONSOLE(5)] [color=#2AA198 !important]"console.warn", [color=#859900 !important]source:  (5) I chromium: [INFO:CONSOLE(6)] [color=#2AA198 !important]"console.error", [color=#859900 !important]source:  (6)

但是这样也有一个不足,就是没有打印出Console的消息级别(都展示成了INFO:CONSOLE)。
如果想要解决上面的不足或者自定义日志输出关键字的话,可以重写实现WebChromeClient的onConsoleMessage方法
[color=#93A1A1 !important]1[color=#93A1A1 !important]2[color=#93A1A1 !important]3[color=#93A1A1 !important]4[color=#93A1A1 !important]5[color=#93A1A1 !important]6[color=#93A1A1 !important]7[color=#93A1A1 !important]8[color=#93A1A1 !important]9[color=#93A1A1 !important]10[color=#93A1A1 !important]11[color=#93A1A1 !important]12[color=#93A1A1 !important]13[color=#93A1A1 !important]14[color=#93A1A1 !important]15[color=#93A1A1 !important]16[color=#93A1A1 !important]17[color=#93A1A1 !important]18[color=#93A1A1 !important]19[color=#CB4B16 !important]package [color=#B58900 !important]com.droidyue.webview.chromeclient[color=#268BD2 !important]import [color=#268BD2 !important]android.[color=#268BD2 !important]webkit.[color=#268BD2 !important]ConsoleMessage[color=#268BD2 !important]import [color=#268BD2 !important]android.[color=#268BD2 !important]webkit.[color=#268BD2 !important]WebChromeClient[color=#268BD2 !important]import [color=#268BD2 !important]com.[color=#268BD2 !important]droidyue.[color=#268BD2 !important]common.[color=#268BD2 !important]debugMessage[color=#268BD2 !important]import [color=#268BD2 !important]com.[color=#268BD2 !important]droidyue.[color=#268BD2 !important]webview.[color=#268BD2 !important]ext.[color=#268BD2 !important]toSimpleString[color=#268BD2 !important]open [color=#CB4B16 !important]class [color=#D33682 !important]DiagnosableChromeClient: [color=#268BD2 !important]WebChromeClient() {    [color=#CB4B16 !important]override [color=#CB4B16 !important]fun [color=#268BD2 !important]onConsoleMessage([color=#268BD2 !important]message: [color=#268BD2 !important]String?, [color=#268BD2 !important]lineNumber: [color=#268BD2 !important]Int, [color=#268BD2 !important]sourceID: [color=#268BD2 !important]String?) {        [color=#93A1A1 !important]//不需要调用super方法        [color=#268BD2 !important]debugMessage([color=#2AA198 !important]"onConsoleMessage", [color=#2AA198 !important]"message", [color=#268BD2 !important]message, [color=#2AA198 !important]"lineNumber", [color=#268BD2 !important]lineNumber, [color=#2AA198 !important]"sourceID", [color=#268BD2 !important]sourceID)    }    [color=#CB4B16 !important]override [color=#CB4B16 !important]fun [color=#268BD2 !important]onConsoleMessage([color=#268BD2 !important]consoleMessage: [color=#268BD2 !important]ConsoleMessage?): [color=#268BD2 !important]Boolean {        [color=#268BD2 !important]debugMessage([color=#2AA198 !important]"onConsoleMessage", [color=#2AA198 !important]"message", [color=#268BD2 !important]consoleMessage?.[color=#268BD2 !important]toSimpleString())        [color=#93A1A1 !important]//返回true,不再需要webview内部处理        [color=#CB4B16 !important]return [color=#CB4B16 !important]true    }}

[color=#93A1A1 !important]1[color=#93A1A1 !important]2[color=#93A1A1 !important]3[color=#93A1A1 !important]4[color=#93A1A1 !important]5D debugMessage: ConcreteWebChromeClient;onConsoleMessage message [color=#268BD2 !important]messageLevel=TIP;message=console.debug;sourceId=;[color=#268BD2 !important]lineNumber=1D debugMessage: ConcreteWebChromeClient;onConsoleMessage message [color=#268BD2 !important]messageLevel=LOG;message=console.log;sourceId=;[color=#268BD2 !important]lineNumber=2D debugMessage: ConcreteWebChromeClient;onConsoleMessage message [color=#268BD2 !important]messageLevel=LOG;message=console.info;sourceId=;[color=#268BD2 !important]lineNumber=3D debugMessage: ConcreteWebChromeClient;onConsoleMessage message [color=#268BD2 !important]messageLevel=WARNING;message=console.warn;sourceId=;[color=#268BD2 !important]lineNumber=4D debugMessage: ConcreteWebChromeClient;onConsoleMessage message [color=#268BD2 !important]messageLevel=ERROR;message=console.error;sourceId=;[color=#268BD2 !important]lineNumber=5

开启 WebView 远程调试

从Android Kitkat(4.4)开始,WebView 支持与Chrome 连接执行远程调试。
开启很简单,如下代码
[color=#93A1A1 !important]1[color=#93A1A1 !important]2[color=#93A1A1 !important]3[color=#93A1A1 !important]4[color=#93A1A1 !important]5[color=#CB4B16 !important]fun [color=#268BD2 !important]WebView.[color=#268BD2 !important]enableRemoteDebugging() {    [color=#CB4B16 !important]if ([color=#268BD2 !important]Build.[color=#268BD2 !important]VERSION.[color=#268BD2 !important]SDK_INT >= [color=#268BD2 !important]Build.[color=#268BD2 !important]VERSION_CODES.[color=#268BD2 !important]KITKAT && [color=#268BD2 !important]BuildConfig.[color=#268BD2 !important]DEBUG) {        [color=#268BD2 !important]WebView.[color=#268BD2 !important]setWebContentsDebuggingEnabled([color=#CB4B16 !important]true)    }}

但需要注意两点

  • 一定要限定运行设备大于等于4.4系统
  • 强烈建议限定在Debug编译(或等同条件)包下开启,不建议Release包也启用该功能
配置完成后,启动App,打开Chrome,输入chrome://inspect
可以调试的功能有

  • 审查元素
  • 执行Javascript
  • 查看网页资源
  • 进行性能分析
  • 其他功能
  继续阅读全文



想在安卓巴士找到更多优质博文,可移步博客区

如果对本篇文章感兴趣,请前往,
原文地址:
http://www.apkbus.com/blog-720372-84012.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )