This article is from the end of last year, now it is carried to the nuggets, to share with you.

With only half a month left in 2020, have you accomplished all the goals you set for this year?

Around May or June of this year, I once revealed in my official account that I planned to develop a new open source project this year. The basic idea was pretty much there at the time, but with both Open source projects LitePal and PermissionX to work on and original articles to write from time to time, I wasn’t sure if I could actually finish the new open source project this year.

In the last month of this year, I am very happy to tell you that I have almost completed this new project. There’s still a lot to be said for this, but I believe we have the first beta release ready. Of course, there will be a lot of bugs in the first version, and I will continue to improve the code based on your feedback. This will be another open source project that I will support for a long time.

So let’s talk more about what this open source project is.

one

In fact, I have done Android development for so many years, there has always been a very inhumane place, is that developers do not have a simple and direct view of the current application database files, this problem leads to the development and debugging of Android database has been more difficult.

For example, we write a piece of code to query for a piece of data in a database, but fail to find it. So is the query wrong? Or is it because the data doesn’t exist? How to locate and solve this problem is a headache, because we can not directly see the actual data in the current database.

So how did people solve it in the past?

This is really the eight immortals across the sea, each of their powers. My preferred approach is to view the database directly with SQL commands, go to the console with adb shell, open a database file with sqlite3, and then view the data in the database with traditional SQL statements. Unfortunately, this method has been banned since Android 7.0, but the main concern is data security.

In addition, some friends may use some third-party tools, such as SQLite Expert. The tool is used to view database files on a computer, so you need to find a way to export the database files from your phone to the computer (not an easy step, since files with built-in storage are hard to export), and then open the file with SQLite Expert to view the data.

Either way, it doesn’t seem like a simple task. Sometimes I encounter some database problems in the development process, and I would rather change the way to solve the problem at the thought of using such tedious steps to view the data in the database.

Google has not provided an easy solution for database debugging in the past, which I find very inhumane.

The good news is that the Database Inspector tool is finally built into Android Studio 4.1, which largely solves the problem of Database debugging. And I think Google should have provided this tool a long time ago.

My new open source project is also aimed at solving this problem.

two

The idea for such an open source project was inspired by LeakCanary. LeakCanary is sure to be used by many of us, we just need to integrate the LeakCanary library into the project and LeakCanary will automatically detect memory leaks in the current project and show the developers the memory leaks via a visual interface.

Then I thought, if I can develop an open-source repository, when any project integration, after the open source library will automatically scan the current internal and external storage space of the project, all the database file scanning, and also provide a visual interface for developers at any time to check the data in the database.

With this library, when we encounter database problems in the development process, directly through the visual interface to see what the real data in the database is like, where the problem at a glance.

While this may sound like some overlap with Database Inspector’s capabilities, their target scenarios are completely different. The Database Inspector connects your phone to your computer and then checks the data in your Database in Android Studio. The open source library I developed does not need to be connected to a computer, but can be viewed on a phone (somewhat similar to the relationship between Profiler and LeakCanary).

I’ve named this open source library Glance, which means Glance. I came up with the name because I wanted developers to be able to identify database problems in development with a quick glance.

With a project name and design idea in mind, I started to develop it. In fact, Glance has had a relatively smooth development, probably due to a lot of database experience from developing LitePal.

In addition, Glance is not only a tool to help check database content, but also a very good learning project. When compiling Glance, I deliberately used many new technologies recommended by Google at present, including Kotlin, coroutine, Paging3, App Startup, MVVM and so on, which were basically implemented in accordance with the most standard project development specifications. So, learning the source code of this project I believe will also have a very big help to your development level.

I started writing the project around mid-August, and by November I had almost all the major features developed. There was also a small round of internal testing, and several enthusiastic group members helped me find several quite serious bugs, which I fixed one by one.

Now, I think this is a relatively stable version. However, since it is a new open source library, I was afraid to release version 1.0.0 directly, so I will release version 1.0.0-Alpha01. If you find any problems in the process of using it, it is normal, just feedback to me, I will repair it as soon as possible.

three

Ok, now let’s introduce the specific use of Glance. It’s really, really easy, just use the following statement to introduce Glance into your project:

Dependencies {debugImplementation 'com. Glance. Guolindev: glance: 1.0.0 - alpha01'}Copy the code

And then it’s over.

Yes, it’s as simple as that. Glance doesn’t provide any external apis, so you don’t need to do any code interfacing, just introduce the dependent libraries into your project, and the interfacing is complete.

Note that the debugImplementation keyword we use in the above reference statement is the debugImplementation keyword, which I believe you use less, usually we use the implementation keyword. So what does debugImplementation mean? This means that Glance will only be introduced into your project if your project is a Debug release, and the Glance library will not be included in the release.

This is the safest approach, as introducing Glance libraries in a release creates a backdoor into your application that can cause database security issues.

Just run your app as usual, and you’ll see an additional Glance image on your phone’s desktop, as shown below.

This icon is automatically generated after the Glance library is introduced. Click this icon to open the Visual interface of Glance, as shown below.

From here, Glance will automatically start scanning the current application’s internal and external storage space to find and list all database documents.

For example, demo1.db in the figure above is a database file in my current project, and internal storage means that it is stored in the built-in storage space.

Click demo1.db to open the database file and list all the tables in the database, as shown below.

Of course, not all of the tables listed here are created by us, such as android_metadata, sqlite_sequence tables are automatically generated. Glance doesn’t distinguish between them, but lists them all.

Next click on the Magazine table to view the data in the table. Scroll horizontally to see all columns, scroll vertically to see all rows, as shown in the figure below.

Pagination is used here to load the data in the table, so even if you have a million entries in the table, it will load very quickly. Glance paging technology is the use of Paging3 implementation, interested in Paging3 friends can refer to this part of the source code.

At this point, it is very convenient to view the data in the current application database at any time. Isn’t it a thousand times easier than trying to export a database file, send it to a computer, and then use a third-party tool to view it?

Even when compared to Database Inspector, I think Glance has a big advantage because you don’t need a computer or open Android Studio to view it.

In addition, Glance can quickly detect changes to your database files. Let’s say we add a new database file to the external storage space of the current application, as shown in the figure below.

As you can see, when you return to the Glance visual interface, you will quickly find that a demo2.db is added. External storage indicates that the demo2.db is stored in the storage space.

Then we can see all the data in demo2.db at once.

So that’s about it. After all, its main function is to be a helper tool, not a library to be developed and docked with, so the overall usage is very simple.

Next I’d like to talk about Glance’s limitations, which, at least in this first release, need to be reminded of.

First of all, Glance only provides the function of viewing the database, but does not provide the function of modifying or deleting. Of course, I don’t support modification and deletion for any particular reason, but simply because I don’t have the time. In order to make the first version available this year, I chose to build only the core viewing function.

The ability to modify and delete databases will most likely be added in the next version update.

Second, Glance only supports, and will only support, projects using AndroidX architecture. If your project is still using the Support Library architecture, you will unfortunately not be able to use Glance.

I’ve been thinking a lot about this limitation, because the ability to view the database itself has nothing to do with AndroidX architecture. But I use a lot of the latest Google technologies in Glance, such as coroutine, Paging3, App Startup, etc. These new technologies only support AndroidX architecture. So whether to use these new technologies for better compatibility or to use these new technologies to make Glance more learnable, I finally choose the latter.

I also believe that AndroidX will eventually be the trend, and even though I’m still using Support Library projects, I will definitely switch to AndroidX architecture in the near future, so I’m not too worried about that.

four

Well, that’s all for the first Glance release. I am also glad that I have achieved the last small goal I set for this year. This year is really fruitful.

Finally attached Glance open source library address, want to learn source code friends do not miss oh. In addition, please help me to click a star, thank you.

Github.com/guolindev/G…

If you want to learn about Kotlin and the latest on Android, check out my new book, Line 1, Version 3. Click here for more details.

Pay attention to my technical public account “Guo Lin”, high-quality technical articles push.