Android Development Use Chrome to debug your Android App

I have always had a soft spot for Chrome. In addition to being faster, Chrome is more friendly to developers. Built-in powerful Developer Tools, I believe that Web development simply can't put it down! And the Chrome Store provides a variety of plug-ins, not you can't use them, only you can't think of them. Nowadays, Chrome can do everything basically. Sometimes I wonder how convenient it would be if Chrome can be used to debug Android App. Now Facebook has just open sourced a tool Stetho , and Chrome debugging Android is no longer a dream.

Debugging tools

In addition to some official debugging tools in Android development, there are two tools I think are necessary.

  • Packet capture tool

The best one for windows platform should be Fiddle, and the best one for mac should be Charles. This should be a must for App development, whether it is Android or iOS.

  • Sqlite View

This tool is much more. In addition to the built-in sqlite3 tool, there are still some GUI tools that are more convenient. I will not list them one by one. You can search and find the tools you like. There are some browser plug-ins, and some Some clients of various platforms. What you need to know is that you need root if you want to view the sqlite files in the App.

Stetho

Although the packet capture tool is easy to use, it is troublesome to set up a proxy on the phone every time. It is even more troublesome to view the sqlite file as root. But with stetho, these tools are all built-in, easy to use, no root required, let's take a look at the usage of the official demo.

  • First, Gradle depends on
dependencies {
     
     
  compile 'com.facebook.stetho:stetho:1.0.1'
}
  • Then configure it in the Application class of your App
public class MyApplication extends Application {
     
     
  public void onCreate() {
     
     
    super.onCreate();
    Stetho.initialize(
      Stetho.newInitializerBuilder(this)
        .enableDumpapp(
            Stetho.defaultDumperPluginsProvider(this))
        .enableWebKitInspector(
            Stetho.defaultInspectorModulesProvider(this))
        .build());
  }
}

Then you can run the App for debugging, which can basically meet the debugging needs.

  • Chrome debugging

打开Chrome,输入 chrome://inspect 然后就可以在列表里看到有你的app可以用stetho进行调试的app,facebook官方也提供了一个基本的sample,以下是它的sample提供的调试截图

基本功能使用

  • 检测网络状态

  • 查看App本地数据库并且可以直接执行SQL

  • 查看App本地的SharedPreference文件并可以直接编辑

注意事项

值得注意的是如果你只是简单的进行配置下,检测网络状态的是没法查看,有两种方式:

  • 使用OkHttp

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

OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
  • 使用HttpURLConnection

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

其中你可能会依赖如下network helpers.

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

或者

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

最后,提供一个facebook stetho demo的一个下载。

Stetho Sample

Guess you like

Origin blog.csdn.net/xhf_123/article/details/50016165