stetho README

The project's README from github.com/facebook/stetho. As one markdown file: README.md.

Stetho Build Status

Stetho is a sophisticated debug bridge for Android applications. When enabled, developers have access to the Chrome Developer Tools feature natively part of the Chrome desktop browser. Developers can also choose to enable the optional dumpapp tool which offers a powerful command-line interface to application internals.

Once you complete the set-up instructions below, just start your app and point your laptop browser to chrome://inspect. Click the "Inspect" button to begin.

Set-up

Download

Download the latest JARs or grab via Gradle:

implementation 'com.facebook.stetho:stetho:1.6.0'

or Maven:

<dependency>
  <groupId>com.facebook.stetho</groupId>
  <artifactId>stetho</artifactId>
  <version>1.6.0</version>
</dependency>

Only the main stetho dependency is strictly required; however, you may also wish to use one of the network helpers:

implementation 'com.facebook.stetho:stetho-okhttp3:1.6.0'

or:

implementation 'com.facebook.stetho:stetho-urlconnection:1.6.0'

You can also enable a JavaScript console with:

implementation 'com.facebook.stetho:stetho-js-rhino:1.6.0'

For more details on how to customize the JavaScript runtime see stetho-js-rhino.

Putting it together

Integrating with Stetho is intended to be seamless and straightforward for most existing Android applications. There is a simple initialization step which occurs in your Application class:

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initializeWithDefaults(this);
  }
}

Also ensure that your MyApplication Java class is registered in your AndroidManifest.xml file, otherwise you will not see an "Inspect" button in chrome://inspect/#devices :

<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        ...>
        <application
                android:name="MyApplication"
                ...>
         </application>
</manifest>                

This brings up most of the default configuration but does not enable some additional hooks (most notably, network inspection). See below for specific details on individual subsystems.

Enable network inspection

If you are using the popular OkHttp library at the 3.x release, you can use the Interceptors system to automatically hook into your existing stack. This is currently the simplest and most straightforward way to enable network inspection:

new OkHttpClient.Builder()
    .addNetworkInterceptor(new StethoInterceptor())
    .build()

Note that okhttp 2.x will work as well, but with slightly different syntax and you must use the stetho-okhttp artifact (not stetho-okhttp3).

As interceptors can modify the request and response, add the Stetho interceptor after all others to get an accurate view of the network traffic.

If you are using HttpURLConnection, you can use StethoURLConnectionManager to assist with integration though you should be aware that there are some caveats with this approach. In particular, you must explicitly add Accept-Encoding: gzip to the request headers and manually handle compressed responses in order for Stetho to report compressed payload sizes.

See the stetho-sample project for more details.

Going further

Custom dumpapp plugins

Custom plugins are the preferred means of extending the dumpapp system and can be added easily during configuration. Simply replace your configuration step as such:

Stetho.initialize(Stetho.newInitializerBuilder(context)
    .enableDumpapp(new DumperPluginsProvider() {
      @Override
      public Iterable<DumperPlugin> get() {
        return new Stetho.DefaultDumperPluginsBuilder(context)
            .provide(new MyDumperPlugin())
            .finish();
      }
    })
    .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
    .build())

See the stetho-sample project for more details.

Improve Stetho!

See the CONTRIBUTING.md file for how to help out.

License

Stetho is MIT-licensed. See LICENSE file for more details.

stetho (facebook/stetho)

Searching Database…

12.6K ·GitHub
archived
这是什么?Stetho 是一个用于 Android 应用的调试桥接工具,可以让你用 Chrome 开发者工具检查正在运行的应用。它还附带一个可选的命令行工具 dumpapp,用来探查应用内部状态。
这是要加到我应用里的库,还是独立的工具?它是一个 Java 库,通过 Gradle 或 Maven 集成到你的 Android 应用中。加入之后,你就可以在笔记本电脑的 Chrome 浏览器里检查正在运行的应用。
我该怎么把它加到我的 Android 项目里?通过 Gradle 或 Maven 添加 stetho 依赖,在你的 Application 类中调用初始化步骤,并在 AndroidManifest.xml 中注册该类。然后运行应用,在笔记本电脑上打开 chrome://inspect,点击 Inspect。
检查网络请求还需要额外的库吗?只需要核心的 stetho 依赖即可。如果要检查网络请求,还需要额外添加一个网络辅助构件,例如用于 OkHttp 3.x 的 stetho-okhttp3(用于 2.x 的是 stetho-okhttp),或者配合 HttpURLConnection 使用 StethoURLConnectionManager。
集成网络检查时有哪些需要注意的坑?使用 HttpURLConnection 时,你必须显式添加 Accept-Encoding: gzip 请求头,并手动处理压缩的响应,Stetho 才能正确报告数据大小。使用 OkHttp 时,拦截器的添加顺序也很重要。
这是给谁用的?现在还在维护吗?它面向希望通过浏览器调试应用的 Android 开发者。该仓库已归档,因此不再积极维护。
我能给 dumpapp 命令行工具添加自己的命令吗?可以。在 Stetho 的配置步骤中,可以方便地添加自定义的 dumpapp 插件,用你自己的功能扩展命令行界面。
我能自定义它提供的 JavaScript 控制台吗?可以。Stetho 支持启用可选的 JavaScript 控制台,你还可以通过 stetho-js-rhino 模块进一步自定义 JS 运行时。
它支持较旧的 OkHttp 版本吗?支持。OkHttp 2.x 和 3.x 都支持自动网络检查,不过 2.x 需要单独的 stetho-okhttp 构件,语法也与 stetho-okhttp3 略有不同。
Creator
facebook
Size
2.3 MB
Stars
12.6K
Age
11y
Commits · 90 Days