从 https://developer.chrome.com/devtools/docs/remote-debugging 我们可以知道在android 4.4+可以通过在apk中使用下面的代码开启webview的chrome远程调试
1
2
|
WebView
.
setWebContentsDebuggingEnabled
(
<
span
class
=
"keyword"
>
true
<
/
span
>
)
;
|
但我们开发中接触的apk往往是第三方的,没谁会为我们开启webContentsDebuggingEnabled。而Xposed能强制做到这一点
Xposed
Xposed能够勾住(Hook) Android应用程序对象的方法,实现AOP,一个简单的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<
span
class
=
"keyword"
>
public
<
/span> class
pan
>
WebViewHook
<
span
class
=
"keyword"
>
implements
<
/
span
>
IXposedHookLoadPackage
{
<
span
class
=
"comment"
>
// handleLoadPackage 会在android加载每一个apk后执行
<
span
class
=
"keyword"
>
public
<
/
span
>
void
handleLoadPackage
(
LoadPackageParam
lpparam
)
throws
Throwable
{
<
span
class
=
"comment"
>
// 可以从lpparam中获取当前apk的名字
<
span
class
=
"keyword"
>
if
<
/span> (! lpparam.packageName.equals("com.tencent.mobileqq"
pan
>
)
)
{
<
span
class
=
"keyword"
>
return
<
/span>;
}
XposedBridge.log("WebViewHook handleLoadPackage: "
pan
>
+
lpparam
.
packageName
)
;
<
span
class
=
"comment"
>
// 勾住 WebView 所有的构造器
XposedBridge
.
hookAllConstructors
(
WebView
.
<
span
class
=
"keyword"
>
class
<
/span>, new
pan
>
XC_MethodHook
(
)
{
@
Override
<
span
class
=
"keyword"
>
protected
<
/
span
>
void
beforeHookedMethod
(
MethodHookParam
param
)
throws
Throwable
{
<
span
class
=
"comment"
>
// 打开webContentsDebuggingEnabled
XposedHelpers
.
callStaticMethod
(
WebView
.
<
span
class
=
"keyword"
>
class
<
/span>, "setWebContentsDebuggingEnabled"
pan
>
,
<
span
class
=
"keyword"
>
true
<
/span>);
XposedBridge.log("WebViewHook new WebView(): "
pan
>
+
packageName
)
;
}
}
)
;
}
}
|
上面的代码可以为QQ打开WebView的webContentsDebuggingEnabled
Xposed工作原理可以参考文档: https://github.com/rovo89/XposedBridge/wiki/Development-tutorial
没有详细的API页面, 因为API也就几个,可以查看源代码: https://github.com/rovo89/XposedBridge/tree/master/src/de/robv/android/xposed
馋图
拿来主义
1、需要android 4.4+ Root手机
2、安装 Xposed框架
3、已开启QQ WebView的Apk: webviewdebughook.Apk