Search code examples
macosandroid-studioenvironment-variablesadbrealm

Viewing Realm database and defining adb as environment variable on macOS


I am new to using Android Studio, also new to using mac os and new to using realm.

My problem is I need to view my realm DB using the realm monitor for mac os, to do that I need to extract the DB file from my emulator using the terminal inside android studio, but I can't because I need to configure the system variables, I tried to do that using the mac os terminal but it didn't work, I searched and tried many tips online but got nothing.

If anyone can give me a detailed step by step guide to do this it will be very appreciated.

edit: I need to view the realm DB file from my android application, I tried the solution in this link

How to view my Realm file in the Realm Browser?

so I need to extract the realm file from the emulator using the adb command, I tried that using the android studio terminal but could not recognize the adb command, after some research I found this links

Not able to access adb in OS X through Terminal, "command not found"

and

Setting ANDROID_HOME enviromental variable on Mac OS X

So I need to define ANDROID_HOME environment variable and path using mac terminal. I tried that - I opened mac terminal and wrote the following command directly:

export ANDROID_HOME=/Users/apple/Library/Android/sdk

export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

Then I opened the android studio terminal and tried to use adb command but still not found, then reopened mac terminal and wrote the following command: echo $ANDROID_HOME to check the value but I got nothing. It seems I am missing something here I don't know what it is.

The path of my android sdk file is: /Users/apple/Library/Android/sdk


Solution

  • First Things First

    You want to view your database information using Realm Browser.
    In order to do so, you will need to extract the .realm file (the file that stores your realm database) from your android phone or emulator.
    It seems like you need to define adb in your macOS environmental variables so you will be able to execute the suitable command to extract the file, as mentioned above.


    Including adb as an Environmental Variable on macOS

    In order to include adb command as an environmental variable you will need to do the following:
    First, you need to open Terminal and type open ~/.bash_profile and then press enter.
    You will be prompted with the TextEdit app with the .bash_profile file.
    You need to add adb to the PATH in that file.
    Copy the following to the file and save it (cmd + S):

    #set adb:
    ADB=/Users/YOUR_USERNAME/Library/Android/sdk/platform-tools
    export ADB
    
    #set PATH:
    PATH=$PATH:$ADB
    export PATH
    

    Close the file, log out from your user (Apple menu => Log out) and then log in again.
    In order to check that your PATH now contains the adb command, you can open Terminal and type echo $PATH. Now you should see your adb path as well.
    You can also enter an adb command to see if you can run one (such as adb devices).


    Extracting the realm File From Your Android Phone/Emulator

    Realm Browser doesn't support accessing the existing databases on your device directly from the device itself. There are three options to view a database on Realm Browser:

    1. Open Realm File
    2. Open Realm URL
    3. Connect to Object Server

    If you wish to view an existing database on your device, only the first option is relevant to you in that case. You will need to copy the database from the phone/emulator to your computer so you can view it.
    In order to extract the database file from the device, please follow this:

    • Open Terminal, then navigate to a folder in which you would like to save the database file. Navigate to that folder using the cd command.
      For example, if you wish to save it to a folder "realmDB" on your desktop you need to type cd /Users/YOUR_USERNAME/Desktop/realmDB/.
    • Then type adb pull /data/data/APPLICATION_ID/files/ .
      (where your applicationID can be found inside the build.gradle file under the defaultConfig block)

    This will pull all realm files to that folder, where the database file is usually saved under the name default.realm. Now you can open that file using Realm Browser and see your database. Note that the adb pull will only work on an emulator or on a rooted device.

    Another possible solution suggested on one of the links you mentioned on your question is to use Stetho with realm plugin for Stetho in order to view your realm database.
    That answer suggested installing and defining Stetho and then viewing the database file through chrome://inspect page under Resources => Web Sql => default.realm instead of viewing it using Realm Browser.
    You can try that one too if you don't want to pull your database file for every change you make.