Sunday, May 15, 2022

 

Exploring the Database Inspector in Android Studio

The long-awaited tool for Android developers is here

Photo by Dennis Kummer on Unsplash.

Whether you’re working with an SQLite or Room database, every developer needs a tool to debug it. Recently, Android Studio’s team released a new version (Canary 4.1) with the database inspector feature. Keep in mind that it was only introduced in Canary 4.1 and is thus still in its early stages.

You can install Canary 4.1 from this link. Also, remember that you can run multiple versions of Android Studio on your laptop simultaneously (e.g. Android Studio’s stable version for professional development and Canary versions to explore upcoming features like the database and layout inspectors).

Invoking Database Inspector

Once you are finished with the IDE setup, the next step would be to open a project with a local database feature. If you don’t have any project with local DB, then use the sunflower app.

After opening the project, run the app in the emulator or device. Then to open the database inspector, navigate as shown below:

View -> Tool windows -> Database Inspector

Select Device and Database to Debug

Once you click on the database inspector, a new window opens up in the studio. First, you have to select the device if you connected multiple devices to the system. If your app has multiple databases, then you need to select the database that you want to debug. Have a look:

Database inspector

Executing Queries in Database Inspector

This is the exciting part where you can see the data in the local DB on the database inspector by executing queries. You can execute them in two ways — Dao queries and custom queries using the Run Query option.

Dao queries

We can actually run Dao functions using a database inspector and see the data. This feature comes in handy while you’re debugging. When we open any Dao class after invoking a database inspector, you’ll see a Run symbol beside each function to execute the queries in the database inspector. Have a look:

Executing Dao queries

Custom queries

Android Studio’s team has taken great care to cover basic needs in the database inspector. One of which is the ability to run custom queries. This can help to debug the database in multiple ways without actually writing anything in the Dao. Have a look:

Executing custom queries

Update Data

If you can’t update the data from the inspector, all the features above won’t be much of use. So the Android Studio team also included a way to alter or add data to the database from the inspector itself. You can see the changes in the device in a matter of seconds if you’re using Room and observing data through livedata/flow.

This feature is useful in many situations while debugging. Let’s say we have the name of a plant with 10 characters, and you want to increase the length to 40 characters to check UI. In this use case, you can directly update the name from the inspector and check the UI.

To update the data, you just need to double-click on the field and add/remove the data. After that, press Enter to update it in the database. Have a look:

Updating data from inspector

Live Updates

This is one of my favorite features both in the database and the recently released layout inspector. With this feature, if there any changes in the database, we can see them in the inspector instantly.

To enable this feature first, we need to select the “Live updates” checkbox in the database inspector. When “Live updates” is checked, the database inspector automatically displays any changes your app makes to its database. This feature brings robust behavior to the database inspector.

Enable live updates in database inspector

No comments:

Post a Comment