您的位置:首页 > 博客中心 > APP开发 >

Android调试工具_ Stetho

时间:2022-03-12 10:17

是Facebook开源的一个Android平台调试工具。

Stetho能实如今不root手机的情况下,通过Chrome查看App的布局,Sqlite,SharedPreference。Network等。此外它还支持创建Dump文件。

使用Stetho非常重要的一点是要明确Stetho是一个调试工具。理论上是仅仅能用于Debug包的,假设应用到Release包上,你的app的数据就所有暴露出来了。

我们的代码就是以这个为中心来实现的。


核心代码实现:

首先要加入Stetho的引用

Gradle 文件里直接加入Stetho的依赖。

compile 'com.facebook.stetho:stetho-okhttp:1.1.1'
debugCompile 'com.facebook.stetho:stetho:1.1.1'

Stetho的依赖。仅仅须要Debug模式下打包依赖,Release模式打包不须要。
Stetho-okhttp是获取Network数据须要的依赖。因为project中有依赖。仅仅能加入全部模式的依赖。能够參见后面代码。

其次要在特定位置创建一个用于Debug包的AndroidManifest和Application

位置例如以下图所看到的,在主projectsrc文件夹下,创建debug文件夹。假设不须要Dump功能的话仅仅须要新建 AndroidManifest 文件和Application 文件就可以。
AndroidManifest中指明Debug从DebugApplication 启动app。

<?xml version="1.0" encoding="utf-8"?

> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.debug.xsldebug"> <application tools:replace="android:name" android:name=".activity.message.XSLDebugApplication" /> </manifest>


DebugApplication继承应用程序原先的的Application,并加入Stetho初始化相关代码。

<pre name="code" class="java">public class XSLDebugApplication extends XSLApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                        .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                        .build());
    }

    private static class XSLDumperPluginsProvider implements DumperPluginsProvider {
        private final Context mContext;

        public XSLDumperPluginsProvider(Context context) {
            mContext = context;
        }

        @Override
        public Iterable<DumperPlugin> get() {
            ArrayList<DumperPlugin> plugins = new ArrayList<DumperPlugin>();
            for (DumperPlugin defaultPlugin : Stetho.defaultDumperPluginsProvider(mContext).get()) {
                plugins.add(defaultPlugin);
            }
            plugins.add(new XSLDumperPlugin());
            return plugins;
        }
    }
}



经过以上的设置,app已经支持Stetho的基础功能。 能够通过Chrome的设备检查或者 直接訪问 chrome://inspect/#devices 来查找当前连接的设备

技术分享                   技术分享public String okHttpPost(String url, List<BasicNameValuePair> nameValuePairs) { String result = StringUtils.EMPTY_STRING; OkHttpClient client = OkHttpClientFactory.createDefault(); if (BuildConfig.DEBUG) { //debug 包里面,加入Stetho-okhttp的支持 client.networkInterceptors().add(new com.facebook.stetho.okhttp.StethoInterceptor()); } Request request = new Request.Builder() .url(url) .post(getFormEncodingBuilder(nameValuePairs).build()) .build(); try { Response response = client.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); } } catch (Exception e) { e.printStackTrace(); } return result; }仅仅有Debug包才须要支持Stetho。Release包不须要。

可是即使是Release包。这行代码也会Build,所以最開始的依赖里面。Stetho-Okhttp的依赖要是Compile的而不是debugCompile。


效果图预览:

1. 查看布局
技术分享

2. 查看Sqlite,并支持运行Sql语句。注意:命令行不是打在 Console窗体的哦。

技术分享

3. 查看Network请求。


技术分享

4. 创建Dump. 事实上这个功能本人没有去尝试,由于没有dump的需求。


技术分享




                                                                                                                                                                          杏树林研发  梁建彬

相关推荐

电脑软件

本类排行

今日推荐

热门手游