Access Private Data on Android
According to the official developer documentation, here are the options for data storage on Android:
- Shared Preferences — Store private primitive data in key-value pairs.
- Internal Storage — Store private data on the device memory.
- External Storage — Store public data on the shared external storage.
- SQLite Databases — Store structured data in a private database.
- Network Connection — Store data on the web with your own network server.
Aside from the network connection option, the other options are all persisting data on device. Both SharedPreferences and SQlite Databases are private data stored on device. These files are not accessible to other applications or users unless you are on an emulator or rooted device. Here are a few ways how I access and inspect these private data for debugging during development.
Access private files
On an emulator or rooted device
If you are running an emulator or rooted device. You should have access to the private storage of the device. You can access the files from Android Studio GUI or via command line:
- GUI — In Android Studio, launch Android Device Monitor from the menu: Tools/Android/Android Device Monitor. Navigate to the File Explorer tab, then data/data/<your app package name>/. Find the file you are looking for, and you can push and pull a file from there.
- Command line — you can also push the file to device or pull the file from device via adb:
adb pull remote-dir local-dir <-- Copy from device to local machine
adb push local-dir remote-dir <-- Copy from local machine to device
On a non-rooted device
If you are not running on an emulator or rooted device. From command line:
Now you can access the files on the internal storage of the device. To exit the shell, type exit.
exit <-- exit out of adb shell
Sqlite database file
Once you are able to access the SQLite database file on an emulator, rooted device or via adb shell / run as [package name], there are a few options to inspect the schema and your SQLite database on device.
Inspect SQLite db via a GUI tool
Pull the file from device first, then use a GUI software to look the schema and content. I use SQLite browser which allows you to see the database schema, table content, as well as executing some simple SQL scripts.
Inspect SQLite db via sqlite3 command line tool
For me the easier option is to use sqlite3 command line tool to inspect the database from adb shell -
cd data/data/<your package-name>/databases/
Alternatively you can use sqlite3 locally instead of within a shell, after pulling the database file from the device:
adb pull <db-name>
Wiping off app data
So when you work with SharedPreferences or SQLite db on Android, you often need to wipe out the data and start over. So here are a few options:
- Uninstall and reinstall the app
- Go to device Settings/Apps/Find your app and click on it/Storage/Clear Data
There is a much easier way to assist your development and debugging process: install an Android Studio plug-in called ADB Idea. To install the plug-in, go to Android Studio/Preferences/Plugins, click on Browse repositories and search for “ADB Idea”. Install and restart Android Studio and you will see the plug-in option show up under Tools/Android/ADB Idea. Give it a try. I find this plugin super helpful when I need to wipe off app data.
Use a library
There are other options for accessing and inspecting private files on Android devices by using a library.
If you would like to inspect network connections in addition to database and SharedPreferences, use Stetho an open source debugging tool developed by Facebook. When using Stetho, your app needs to be in debug mode but there is no need for root access. It’s a powerful tool that allows you to debug your app using Chrome Developer Tool.