使用Chrome调试安卓app

344 阅读1分钟

Stetho

抓包工具虽然好用,但是每次都要在手机设置代理.但是有了stetho,这些工具全部自带了,使用方便,无须root

首先Gradle进行依赖

dependencies {
  compile 'com.facebook.stetho:stetho:1.0.1'
}

然后在你的App的Application类里进行配置

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initialize(
      Stetho.newInitializerBuilder(this)
        .enableDumpapp(
            Stetho.defaultDumperPluginsProvider(this))
        .enableWebKitInspector(
            Stetho.defaultInspectorModulesProvider(this))
        .build());
  }
}

Chrome调试

打开Chrome浏览器
网址输入:chrome://inspect/#devices

使用OkHttp

这是最简单的一种方式,要求OkHttp的版本在2.2.x+,只需要添加如下代码, 这也是目前最简单的方法

dependencies {
  compile 'com.facebook.stetho:stetho-okhttp:1.0.1'
}

使用HttpURLConnection

如果你使用的自己写的或者其他http library底层是用HttpURLConnection实现的,你需要使用StethoURLConnectionManager来进行集成。然后必须声明Accept-Encoding: gzip的请求headers。具体用法见facebook stetho源码的sample。

dependencies {
  compile 'com.facebook.stetho:stetho-urlconnection:1.0.1'
}

当然不怕麻烦还是使用Fiddle,或者Charles(抓https需要安装ssl证书)