stetho 调试数据库_使用Stetho轻松调试Android应用程序

stetho 调试数据库

Many times while developing android applications I face problems in debugging. It is really hard to know if the database is working fine, the entries are getting recorded in the correct table and in the correct format. Also debugging network calls using logs is quite cumbersome.

在开发android应用程序时,很多时候我在调试中遇到问题。 真的很难知道数据库是否运行良好,是否以正确的表和正确的格式记录了条目。 另外,使用日志调试网络呼叫非常麻烦。

Recently I came to know about Stetho and I tried it. It 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.

最近,我开始了解Stetho,并尝试了一下。 它是用于Android应用程序的复杂调试桥。 启用后,开发人员可以访问Chrome桌面浏览器本地提供的Chrome开发者工具功能。 开发人员还可以选择启用可选的dumpapp工具,该工具为应用程序内部提供了强大的命令行界面。

I just loved it because it really eased my development and debugging process. We can see the databases, shared preferences and networks with a user interface so it’s really easy to see whether everything is working fine or not. Also on top of that integrating it into our app is really simple and straightforward. With a single line of code, it is up and running.

我喜欢它,因为它确实简化了我的开发和调试过程。 我们可以通过用户界面查看数据库,共享的首选项和网络,因此很容易查看一切是否正常。 此外,将其集成到我们的应用程序中确实非常简单明了。 只需一行代码,它就可以启动并运行。

Let’s see how to integrate it into our android app.

让我们看看如何将其集成到我们的android应用中。

积分: (Integration:)

建立: (Setup:)

Add stetho dependency in the build.gradle file:

在build.gradle文件中添加stetho依赖项:

// Stetho
implementation 'com.facebook.stetho:stetho:1.5.1'

Then initialize it in the application class’s onCreate method:

然后在应用程序类的onCreate方法中对其进行初始化:

override fun onCreate() {
super.onCreate()
Stetho.initializeWithDefaults(this)
}

And it's done😁

完成了😁

Connect your device (or emulator if you want) and run your app. Then on your browser go to “chrome://inspect”. This will open a window like this:

连接您的设备(或仿真器,如果需要)并运行您的应用程序。 然后在浏览器上转到“ chrome:// inspect” 。 这将打开一个这样的窗口:

Image for post

There you can see your device and a list of your debuggable applications. Just click on inspect for whichever application you want to debug and another window will open like this:

在这里,您可以看到您的设备和可调试应用程序列表。 只需单击检查要调试的应用程序,然后将打开另一个窗口,如下所示:

Image for post

There you can see there are many features including our databases and shared prefs in proper tabular format.

在这里,您可以看到许多功能,包括我们的数据库和正确格式的共享首选项。

You can also have a look at the queries that you are making and their results. In the elements tab it shows all the UI elements present on the screen:

您还可以查看正在执行的查询及其结果。 在elements选项卡中,它显示了屏幕上存在的所有UI元素:

Image for post
Image for post

启用网络检查: (Enabling network Inspection:)

If you want to enable the network inspection also then add the network helpers dependencies in the build.gradle:

如果您还想启用网络检查,则在build.gradle中添加网络助手依赖项:

implementation 'com.facebook.stetho:stetho-okhttp3:1.5.1'
orimplementation'com.facebook.stetho:stetho-okhttp:1.5.1'
orimplementation'com.facebook.stetho:stetho-urlconnection:1.5.1'

Then add stetho interceptor in your HTTP client:

然后在您的HTTP客户端中添加Stetho拦截器:

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

Then you can see the data flowing through the network in the network tab:

然后,您可以在网络标签中查看流经网络的数据:

Image for post

自定义dumpapp插件: (Custom dumpapp Plugins:)

Dumpapp extends beyond the DevTools UI features shown above to provide a much more extensible, command-line interface to application components. A default set of plugins is provided:

Dumpapp超越了上面显示的DevTools UI功能,为应用程序组件提供了更加可扩展的命令行界面。 提供了一组默认的插件:

Image for post

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:

自定义插件是扩展dumpapp系统的首选方法,可以在配置过程中轻松添加。 只需这样替换您的配置步骤:

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());

Javascript控制台: (Javascript Console:)

Stetho provides you a Javascript Console also which allows execution of javascript code that can interact with the application or even the Android SDK:

Stetho为您提供了一个Javascript控制台,该控制台还允许执行可以与应用程序甚至Android SDK进行交互的javascript代码:

Image for post

Feel free to comment if there are doubts or if you feel anything needs to be corrected😀.

如果有任何疑问或您觉得需要纠正任何问题,请随时发表评论😀。

翻译自: https://medium.com/swlh/debug-android-application-easily-using-stetho-c6ba37be452

stetho 调试数据库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值