Home Debugging with Stetho
Post
Cancel

Debugging with Stetho

Stetho is facebook open sourced debug tool that brings in chrome developer features natively for android applications with the help of okhttp interceptors.

ScreenShot ScreenShot

Getting started

To get started add dependency in your gradle file and intialize stetho in the onCreate method of your application class.

Adding dependency:

   compile 'com.facebook.stetho:stetho-urlconnection:1.3.1'
 

Code changes:

  Stetho.initialize(Stetho.newInitializerBuilder(mContext)
   .enableDumpapp(Stetho.defaultDumperPluginsProvider(mContext))
   .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(mContext))
   .build());
 
OkHttpClient client =  new OkHttpClient.Builder()
                  .addNetworkInterceptor(new StethoInterceptor())
                  .build();

Side notes: (i)If you have never used okHttpClient,enqueue callback returns the response in background thread, make sure you communicate to the main thread,i have used handler for the purpose.
(ii)If you are using Picasso to display the images in your application make sure you use the same okhttp instance that has StethoInterceptor configured in it. personally i would recommend using Singleton pattern to get instance of okhttp.

	picasso = new Picasso.Builder(mContext)
                .downloader(new OkHttp3Downloader(okHttpClient))
                .build();

make a note that i have used OkHttp3Downloader.

(iii)Do not configure okhttp in the following way

	OkHttpClient client = new OkHttpClient();
	client.networkInterceptors().add(new StethoInterceptor())

in okhttp3 the networkInterceptors list is made unmodifiableList. hence will throw unsupported Operation Exception.

The video gives the quickoverview on how stetho helps in monitoring network calls, and inspecting elements in the screens.

Notes

You can additionally view the shared preferences value and the database values using the stetho which i have not included in the video.
In the sample application i have created two dummy methods.fillDummyValuesInSharedPreferences and fillDummyValuesInDB which fills value in SharedPreferences. Realm db contents will not be shown,while shared preferences will be shown under local storage option, and also note that stetho also provides custom dumpapp plugins, which i have not covered in this blog post.

ScreenShot

More Reading:

1.Also there is awesome screencast by Donn felker called caster.io.
2.Do checkout the sample code in the stetho repo.

This post is licensed under CC BY 4.0 by the author.