How to Debug Database in Android?
Last Updated :
11 Jul, 2021
The Android Debug Database library is a useful tool for troubleshooting databases and shared preferences in Android apps. In this article we would be looking forward to using this library and get our hand on it, so continue reading, and indulge. First thing's first,
What's Exactly an Android Debug Database?
Well, the answer to that is simple, In a very easy way, Android Debug Database allows you to examine databases and shared settings straight in your browser. Not only this, but the ADD also enables you to look at the structure of your database in a debuggable format, so that you could take action on your data, in a much more granular and sophisticated manner.
All these features work seamlessly, even without rooting your Android Device! Isn't that great?
Let's Dive in and See in Detail. What Android Debug Database is Capable of.
Here's a List of it:
Sounds Great, to Begin With, Let's Start!
Step #1: Adding the dependency to your Android Project
debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'
Step #2: Setting the Authentication
debug {
resValue("string", "DB_PASSWORD_GEEKSFORGEEKS", "password")
}
Note: If you wish to use another port than 8080, do so. Make the following changes to the buildTypes section of the app build.gradle file.
Figure 1. Running the Database Debugger.
In case you missed the address log in logcat to get the address with Toast. Because this library is auto-initialized, if you wish to retrieve the address log, add the following function and call it using reflection (we have to do this to avoid build errors in release builds because this library will not be included). Just simply pop out a toast by using this:
Kotlin
public static void showingDebug(Context context) {
if (BuildConfig.DEBUG) {
try {
Class<?> debugDB = Class.forName("com.GeeksforGeeks.DebugDB");
Method getAddressLog = debugDB.getMethod("getAddressLog");
Object value = getAddressLog.invoke(null);
Toast.makeText(context, (String) value, Toast.LENGTH_LONG).show();
} catch (Exception ignore) {
// A simple catch statement!
}
}
}
Figure 2. Viewing the Data.
If you want to add your own Database file then:
Kotlin
public static void applyCustomFileDBHere(Context c) {
if (BuildConfig.DEBUG) {
try {
Class<?> GeeksforGeeksDB = Class.forName("com.GeeksforGeeks.GeeksforGeeksDB");
Class[] argTypes = new Class[]{HashMap.class};
Method setCustomDatabaseFiles = GeeksforGeeksDB.getMethod("setCustomDatabaseFiles", argTypes);
HashMap<String, Pair<File, String>> customDatabaseFiles = new HashMap<>();
// set your custom database files
customDatabaseFiles.put(GfGDBHelper.DATABASE_NAME,
new Pair<>(new File(c.getFilesDir() + "/" + GfGDBHelper.DIR_NAME +
"/" + GfGDBHelper.DATABASE_NAME), ""));
setCustomDatabaseFiles.invoke(null, customDatabaseFiles);
} catch (Exception ignore) {
}
}
}
Figure 3. Editing the Data
And just like this, you've added your own Android Debug Database to Your Android Project, for more insight and information visit this GitHub repository here.
Similar Reads
How to Access SQLite Database in Android For Debugging? A software library that provides a relational database management system is called SQLite. It allows the users to interact with RDBMS. The lite in SQLite denotes the lightweight when it comes to setup, database administration, and resources required. In SQLite, a database is stored in a single file
10 min read
Android SQLite Database in Kotlin Android comes with an inbuilt implementation of a database package, which is SQLite, an open-source SQL database that stores data in form of text in devices. In this article, we will look at the implementation of Android SQLite in Kotlin. SQLite is a self-contained, high-reliability, embedded, full-
5 min read
How to Use Memory Heap Dumps Data in Android? When we design an Android application, the most prevalent concern among developers is the program's memory utilization. This is because, Â the majority of people use low-memory devices, if your program uses a lot of memory, you can lose users. The developers are attempting to locate each and every me
6 min read
Testing Room Database in Android using JUnit In this article, we are going to test the Room Database in android. Here we are using JUnit to test our code. JUnit is a âUnit Testingâ framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains an
5 min read
How to Install MongoDB on Android? MongoDB is a NoSQL document-oriented database used to store data in high volume. Instead of using columns and rows or tables that were used in traditional relational databases like SQL, MongoDB makes use of collections and documents. MongoDB is written in C++ language. MongoDB is developed and manag
9 min read
Python SQLite - Creating a New Database In this article, we will discuss how to create a Database in SQLite using Python. Creating a Database You do not need any special permissions to create a database. The sqlite3 command used to create the database has the following basic syntax Syntax: $ sqlite3 <database_name_with_db_extension>
3 min read