Weex Devtools 能够方便调试 Weex 页面,但此功能离不开 Native 的支持。如何让你的 App 也集成 Devtools,在本章将会详细说明 Android 端如何接入 Weex Devtools。
Android 应用接入
添加依赖
可以通过 Gradle 或者 Maven 添加对 devtools aar 的依赖,也可以直接对源码依赖。强烈建议使用最新版本,因为 Weex SDK 和 devtools 都在快速的迭代开发中,新版本会有更多惊喜,同时也修复老版本中一些问题。最新的 release 版本可在这里查看。所有的 release 版本都会发布到 jcenter repo。
* Gradle 依赖
1. dependencies {
2. compile 'com.taobao.android:weex_inspector:${version}'
3. }
* Maven依赖
1.
2. com.taobao.android
3. weex_inspector
4. ${version}
5. pom
6.
* 源码依赖
需要复制 inspector 目录到你的 App 的同级目录,然后在工程的 settings.gradle 文件下添加 include ":inspector",此过程可以参考 playground 源码的工程配置及其配置,然后在 App 的 build.gralde 中添加依赖。
1. dependencies {
2. compile project(':inspector')
3. }
另外 weex_inspector 中有一部分包是以 provided 的方式引入,接入方需要自行解决依赖和版本冲突。
* provided方式引用的包
1. dependencies {
2. provided 'com.google.code.findbugs:jsr305:2.0.1'
3. provided 'com.android.support:appcompat-v7:23.1.1'
4. provided 'com.taobao.android:weex_sdk:0.8.0'
5. provided 'com.alibaba:fastjson:1.1.45+'
6. ...
7. }
* 反射引用的包(0.8.0.0以上版本)
1. dependencies {
2. compile 'com.squareup.okhttp:okhttp:2.3.0'
3. compile 'com.squareup.okhttp:okhttp-ws:2.3.0'
4. ...
5. }
或者
1. dependencies {
2. compile 'com.squareup.okhttp:okhttp:3.4.1'
3. compile 'com.squareup.okhttp:okhttp-ws:3.4.1'
4. ...
5. }
版本兼容
| weex sdk |
weex inspector |
Debugger Server |
| 0.13+ |
0.12+ |
0.2.39+ |
| 0.8.0.1+ |
0.0.8.1+ |
0.2.39+ |
| 0.7.0+ |
0.0.7.13 |
0.2.38 |
| 0.6.0+ |
0.0.2.2 |
- |
| 0.16.0+ |
0.12.1 |
- |
| 0.17.0+ |
0.13.2 |
- |
| ### 添加 Debug 模式开关 |
|
|
| 控制调试模式的打开和关闭的关键点可以概括为三条规则。 |
|
|
规则一:通过sRemoteDebugMode 和 sRemoteDebugProxyUrl 来设置开关和 Debugger Server 地址。 |
|
|
Weex SDK 的 WXEnvironment 类里有一对静态变量标记了 Weex 当前的调试模式是否开启分别是: |
|
|
| 1. public static boolean sRemoteDebugMode; // 是否开启 debug 模式,默认关闭 |
|
|
| 2. public static String sRemoteDebugProxyUrl; // DebugServer的websocket地址 |
|
|
无论在 App 中无论以何种方式设置 Debug 模式,都必须在恰当的时机调用类似如下的方法来设置 WXEnvironment.sRemoteDebugMode 和 WXEnvironment.sRemoteDebugProxyUrl。
更详细和合适的位置,初始化 inspector 的方法请参考 Playground.
1. private void initDebugEnvironment(boolean enable, String host) {
2. WXEnvironment.sRemoteDebugMode = enable;
3. WXEnvironment.sRemoteDebugProxyUrl = "ws://" + host + ":8088/debugProxy/native";
4. }
规则二:修改sRemoteDebugMode 后一定要调用`WXSDKEngine.reload()。
一般來說,在修改了 WXEnvironment.sRemoteDebugMode 以后调用了 WXSDKEngine.reload() 方法才能够使 Debug模式生效。WXSDKEngine.reload() 用来重置 Weex 的运行环境上下文,在切换调试模式时需要调用此方法来创建新的 Weex 运行时和 DebugBridge 并将所有的 JS 调用桥接到调试服务器执行。在 reload 过程中会调用 launchInspector,这就是 SDK 控制 Debug 模式最核心一个方法,其传入参数即为 sRemoteDebugMode,若为 true 则该方法中尝试以反射的方式获取 DebugBridge 用来在远端执行 JS,否则在本地运行。
1. private void launchInspector(boolean remoteDebug) {
2. if (WXEnvironment.isApkDebugable()) {
3. try {
4. if (mWxDebugProxy != null) {
5. mWxDebugProxy.stop();
6. }
7. HackedClass