View and modify Realm database on Android
published 12.4.2020
Did you ever need to check what your Realm database contains during development? Do you need to modify Realm data and verify the changes in your emulator?
I'll show you how to find the file(s) where Realm database is stored on Android. Then, how to make a local copy, edit it, and push back to Android filesystem. I only tried this on android emulator, so I don't know how this applies on physical device, or iOS emulator.
Make sure you have Realm Studio and adb installed.
Find .realm file location
By default, this file is called default.realm
. To find it on Android file system, you can ask Realm to output the location once it connects to the file:
Realm.open({
schema: [...]
}).then(realm => {
console.log('Realm file location:', realm.path);
});
Run the app and check logs output. In my case, this shows up: Realm file location: /data/data/com.amazingapp/files/default.realm
If you use Android Studio, you can now open View -> Tool Windows -> Device File Explorer and you should be able to locate that file (among other files your app works with).
You can download the file to your local filesystem from Device File Explorer. However, opening the file directly from Android Studio doesn't work (at least not for me). Device File Explorer fails with this error: Unable to open file "..." in editor Yet, I was still able to get around this inconvenience:
Get a reference to emulator instance
While having your app running, find out its identifier for adb, by running following in the console:
$ adb devices -l
List of devices attached
emulator-5554 device product:sdk_phone_x86_64 model:Android_SDK_built_for_x86_64 device:generic_x86_64 transport_id:15
Download default.realm to your local filesystem
$ adb -s emulator-5554 pull /data/user/0/com.amazingapp/files/default.realm default.realm
adb: error: failed to stat remote object '/data/user/0/com.amazingapp/files/default.realm': Permission denied
Unfortunately, this didn't go so well. Let's try again with adb running under root:
$ adb root
restarting adbd as root
$ adb -s emulator-5554 pull /data/user/0/com.amazingapp/files/default.realm default.realm
/data/data/com.amazingapp/files/default.realm: 1 file pulled, 0 skipped. 12.6 MB/s (32768 bytes in 0.002s)
Push back to emulator file system
Now, open the file you just pulled in Realm Studio, and do your changes.
To push it back to the emulator:
$ adb -s emulator-5554 push default.realm /data/data/com.amazingapp/files/default.realm
here.realm: 1 file pushed, 0 skipped. 2.7 MB/s (32768 bytes in 0.012s)