So, how to debug your Android database?
Short answer, Stetho! This has become far easy by Stetho, a project by Facebook which allows you to utilize the Chrome developing tools.
Facebook’s Stetho project enables you to use the Chrome debugging tools to troubleshoot areas like databases, and network traffic. The process is simple. You will need to have your device or emulator running, and use Chrome browser to inspect the device.
Connect your device or start the emulator, and then open Chrome and simply enter chrome://inspect in the Url box. This connects your device or emulator to utilize their powerful developer tools options.
Go to their website http://facebook.github.io/stetho/ and download the Jar file from there, or directly download the latest jars from their Github page here https://github.com/facebook/stetho/releases/latest
As an alternative, you can import Stetho dependencies from Maven Central in your Gradle or Maven.
This can be done by including the below dependency into your Gradle dependencies block.
//Stetho's Gradle dependency
<!-- Stetho's Maven dependency -->
In case you need to inspect network traffic, you will be using the Network Inspector.
Just use the main stetho dependency to utilize them all. You may also use the network helpers if you wish. There are more than one options. You may use either OkHttp, OkHttp3, or simply URLConnection.
Integrating Stetho in Android applications is simply straightforward. You will be adding the initialization code in your Main Application class.
This step initializes all of the default needed configurations. Anyway, you have to utilize other Classes seperately to include options like OkHttp, OkHttp2.x, OkHttp3.x, since they are subsystems and not default to Stetho.
Normally we use the Async Http Client for networking. This library uses the Apache HTTP Client, which Stetho is not supportive of. In case of Stetho, you need to use OkHttp library. To Enable Network Inspection in your Application, you have to include the dependencies as I said above, and then use the OkHttp library.
This is a simple and straightforward step.
If you’re on OkHttp 2.x,
OkHttpClient client = new OkHttpClient();
If you’re on OkHttp 3.x,
To inspect the Network traffic in real time, just click on the ‘Network’ tab.
To inspect a SQLite database, simply use the ‘Resources’ menu and then ‘Web SQL’ tab.
To inspect SharedPrefs, go to the ‘Resources’ tab and select LocalStorage. You will see the SharedPrefs files listed. You are also now authorized to edit the key-value pairs of the tables.
There is not just developer tools of Chrome to debug your Application, there is Stetho’s command line utility, dumpapp.
Downloading dumpapp is simple that you need to just clone the Stetho repository.
git clone https://github.com/facebook/stetho
You would need the latest version of Python to use dumpapp since it is based on Python script.
Now you can utilise the plugins to view and edit databases.
For example, consider using prefs plugin.
./dumpapp prefs print
This command helps you view and edit all the Shared Preferences.