androidandroid-studioandroid-emulatorandroid-device-monitor

Mac, Android Studio, Emulator, SQLite database - actually find directory?


I am using Android Studio on a Mac, everything on the latest version. Say I do this:

SQLiteDatabase db;
db = yourAppContext.openOrCreateDatabase("db.sqlite3", Context.MODE_PRIVATE, null);

Thanks to this link:

http://www.c-sharpcorner.com/UploadFile/e14021/know-where-database-is-stored-in-android-studio/

it's possible to exactly find the directory holding your actual SQLite3 database on the emulator as it runs:

enter image description here

Great stuff.

Next you can click the Pull button, and pull that file to say your desktop on the Mac. Awesome.

But, how do you actually find literally that folder ("databases" in the example), on your Mac?

i.e., using a normal shell terminal, I want to "cd" to there.

Where is that folder, in the ordinary 'nix file structure of the host Mac computer?


Solution

  • adb (Android Debug Bridge) is needed to access an emulator device's filesystem via the command-line . If you don't already have it installed you can download it here.

    Once installed it's just a matter of using a few commands to view/access the files:

    1. Open Terminal.app.
    2. Execute adb devices (this should list your current emulator devices).
    3. Connect to the emulator device with adb shell .
    4. Change directory cd to the folder where your app databases are.
    5. Type ls to see which database files are present.

    In Terminal it would look something like this:

    List the emulator devices:

    $ adb devices
    List of devices attached
    emulator-5554  device
    

    Connect to device with adb shell:

    $ adb -s emulator-5554 shell
    

    Change directory (cd) into it's SQLite3 database location (package name is usually com...):

    adb shell
    $ cd /data/data/<your app package name>/databases/
    

    List the files in the databases directory:

    adb shell
    $ ls
    db.sqlite3
    

    That's really the basics of it! It's also possible to push/pull to copy files from the emulator (remote) to your (local) filesystem:

    adb pull /data/data/com.yourpackagename.app/databases/db.sqlite3 /local/save/path
    

    If Permission is denied run ADB in root mode:

    adb root
    

    More information: