166

I'm using shared preferences to store certain values for my app. I would like to see the file where the info is actually stored on my phone. I found many ways to do this on Eclipse, but I'm debugging on Android Studio. My phone is rooted. I read that having root access is important to read these types of files. If there is no way, then I will look up how to access the info through my program then output it to log cat. Hopefully, though, I can just view the file on the phone directly as it is much simpler. Thanks.

JayUser
  • 1,661
  • 2
  • 11
  • 3
  • Install ES File Explorer and enable root browsing. You will be able to open it directly on the phone then. – CurlyPaul May 13 '14 at 15:37
  • 5
    try using adb pull to fetch the file to the desktop. `adb pull /data/data//shared_prefs/prefs.xml` – Lal May 13 '14 at 15:51
  • 2
    OR goto file explorer and in the file tree, find your app's data folder under /data/data/com.your-package/shared_prefs. The preference file will be there, as an XML. Copy it from the device and enjoy. In the upper right corner of the File Explorer view, there are two icon buttons - one with a disk and one with a mobile phone. These buttons will allow you to copy files from and to the phone/emulator, respectively. – Lal May 13 '14 at 15:53
  • 1
    For some reason my prefs xml file is called _preferences.xml – JPM Jan 07 '16 at 17:18

15 Answers15

178

The Device File Explorer that is part of Android Studio 3.x is really good for exploring your preference file(s), cache items or database.

  1. Shared Preferences /data/data//shared_prefs directory

enter image description here

It looks something like this

enter image description here

To open The Device File Explorer:

Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar.

enter image description here

phnmnn
  • 12,813
  • 11
  • 47
  • 64
120

From Android Studio , start Android Device Monitor, go to File Explorer, and browse "/data/data/< name of your package >/shared_prefs/". You will find the XML there... and also you can copy it for inspection.

If you have a non-rooted device it's not possible to do that directly from Android Studio. However, you can access the file with adb shell as long as your application is the debug version.

adb shell
run-as your.app.id
chmod 777 shared_prefs/your.app.id_preferences.xml
exit # return to default user
cp /data/data/your.app.id/shared_prefs/your.app.id_preferences.xml /sdcard

After that you can pull the file from /sdcard directory with adb.

Cory Petosky
  • 12,458
  • 3
  • 39
  • 44
George Dima
  • 2,707
  • 3
  • 17
  • 19
  • I can´t access this folder from Device Monitor. Is Root required? – Marian Klühspies Jul 22 '15 at 07:48
  • yeah. You cant access share_prefs xml without device root access. – balachandarkm Jul 22 '15 at 12:23
  • 2
    Second method works for non-rooted devices. After you copy the file to sd card, you can exit adb and run adb pull "/sdcard/your_preferences_file" and you'll get an xml file in your current directory you can inspect. – Bebop_ Jun 15 '16 at 18:29
  • 4
    You can actually grab this info from an emulator without root, if you can debug that way. If you are on a physical device, you will need root. – Booger Jun 29 '16 at 21:15
  • @Booger I reached the shared pref xml file but when I double-click it, it won't open. I'm doing it via Emulator + Android Device Monitor. – Woppi Nov 17 '16 at 11:03
  • 4
    look for device file explorer in the bottom right corner of AndroidStudio. It will provide you easy access to device file system – Maksim Turaev Aug 21 '18 at 06:10
  • 3
    Using Device File Explorer, I can access the shared preferences without root access – Devansh Maurya Jun 11 '19 at 13:55
50

UPDATE: Flipper

Flipper is a newer alternative from facebook. It has more features. And actively maintained

Stetho

You can use http://facebook.github.io/stetho/ for accessing your shared preferences while your application is in the debug mode. No Root

features:

  1. view and edit sharedpreferences
  2. view and edit sqLite db
  3. view view heirarchy
  4. monitor http network requests
  5. view stream from the device's screen
  6. and more....

enter image description here

Basic setup:

  1. in the build.gradle add compile 'com.facebook.stetho:stetho:1.5.0'
  2. in the application's onCreate() add Stetho.initializeWithDefaults(this);
  3. in Chrome on your PC go to the chrome://inspect/

You can also use @Jeffrey suggestion:

  • Open Device File Explorer (Lower Right of screen)
  • Go to data/data/com.yourAppName/shared_prefs
Maksim Turaev
  • 4,115
  • 1
  • 29
  • 42
  • Can this work on emulators? I can't seem to make it work – EduardoMaia Dec 05 '17 at 21:54
  • @EduardoMaia It should. I use it with emulator every day. You can create a question with screenshots, so I will try to help you. – Maksim Turaev Dec 06 '17 at 07:51
  • @EduardoMaia You can try to do the following: 1. Clone facebook stetho repository. 2. Import this project to LATEST Android Studio (File->Import->"choose settings.gradle") 3. agree to update gradle plugin. 4. Start Android Emulator (in my case API 24) 5. Launch stetho-sample (select it from run configuration menu next to green arrow 6. On your PC open latest Chrome browser 7. go by chrome://inspect address 8. chose your emulator in list. 9. In opened window go to Resources tab. 10. Find your shared preferences under LocalStorage menu on the left side. 11. In application play with boolean flag – Maksim Turaev Dec 06 '17 at 08:26
  • I fixed my problem yesterday without needing to view the shared prefs. But I'll try this step-by-step another day. Thanks. – EduardoMaia Dec 06 '17 at 13:24
  • Stetho is outdated. You would need to use Flipper or some other tools for this. – S.Javed Dec 06 '21 at 13:21
26

Android Studio -> Device File Explorer (right bottom corner) -> data -> data -> {package.id} -> shared-prefs

Note: You need to connect mobile device to android studio and selected application should be in debug mode

Niranjan
  • 1,879
  • 2
  • 22
  • 28
20

This is an old post, but I though I should put a graphical answer here as the question is about viewing the SharedPreferences.xml using Android Studio. So here it goes.

Go to the Tools -> Android Device Monitor. Open the device monitor by clicking it.

enter image description here

Then you need to select the File Explorer tab in the device monitor. Find the data folder and find another data folder inside it. It will contain a folder having the name of your application package and there will be the desired SharedPreferences.xml.

enter image description here

Select the SharedPreferences.xml file and then pull and save the file in your computer using the button marked at the top-right corner of the image above.

I've used a device emulator.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • 2
    Thanks to your post I learned about https://developer.android.com/studio/debug/device-file-explorer which is the new way to do the same you're explaining here. – malarres May 31 '18 at 12:00
14

You could simply create a special Activity for debugging purpose:

@SuppressWarnings("unchecked")
public void loadPreferences() {
// create a textview with id (tv_pref) in Layout.
TextView prefTextView;
prefTextView = (TextView) findViewById(R.id.tv_pref);
    Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(
            context).getAll();
    for (String key : prefs.keySet()) {
        Object pref = prefs.get(key);
        String printVal = "";
        if (pref instanceof Boolean) {
            printVal =  key + " : " + (Boolean) pref;
        }
        if (pref instanceof Float) {
            printVal =  key + " : " + (Float) pref;
        }
        if (pref instanceof Integer) {
            printVal =  key + " : " + (Integer) pref;
        }
        if (pref instanceof Long) {
            printVal =  key + " : " + (Long) pref;
        }
        if (pref instanceof String) {
            printVal =  key + " : " + (String) pref;
        }
        if (pref instanceof Set<?>) {
            printVal =  key + " : " + (Set<String>) pref;
        }
        // Every new preference goes to a new line
        prefTextView.append(printVal + "\n\n");     
    }
}
// call loadPreferences() in the onCreate of your Activity.
OlatunjiYSO
  • 98
  • 15
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
7

Another simple way would be using a root explorer app on your phone.

Then go to /data/data/package_name/shared_preferences_folder/name_of your_preferences.xml, you can use ES File explorer, and go to the root of your device, not SD card.

nagendra nag
  • 1,142
  • 2
  • 15
user5291072
  • 129
  • 1
  • 4
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). - [From Review](/review/low-quality-posts/10973140) – Luca Detomi Jan 21 '16 at 08:19
  • @LucaDetomi you know what I'm out of here – user5291072 Jan 21 '16 at 14:32
  • Your answer was flagged as too short. Answers should contain details that let users to understand "why" it could be a good answer and maybe the best one. I suggest you to add more details that help to understand principles and maybe to adapt it to other similar issues – Luca Detomi Jan 21 '16 at 14:41
7

To open shared preference in android studio

Open device explorer file from right side- data>data> >shared_prefs

find the attached image for more description

Android
  • 1,420
  • 4
  • 13
  • 23
striker
  • 560
  • 6
  • 13
6

In the Device File Explorer follow the below path :-

/data/data/com.**package_name**.test/shared_prefs/com.**package_name**.test_preferences.xml
neelkanth_vyas
  • 192
  • 1
  • 7
4

Single or Multiple Shared Preference Files

If you have multiple Shared Preference files, then here is a good way to show all of them, but you can just pass in 1 filename, too.

  • loadSharedPrefs("pref_name");

  • loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");

Choose one of the following to suit your needs...

Single-Type Values

public void loadSharedPrefs(String ... prefs) {

    // Logging messages left in to view Shared Preferences. I filter out all logs except for ERROR; hence why I am printing error messages.

    Log.i("Loading Shared Prefs", "-----------------------------------");
    Log.i("----------------", "---------------------------------------");

    for (String pref_name: prefs) {

        SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
        for (String key : preference.getAll().keySet()) {

            Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                  preference.getString(key, "error!"));

        }

        Log.i("----------------", "---------------------------------------");

    }

    Log.i("Finished Shared Prefs", "----------------------------------");

}

Multiple-Type Values

public void loadSharedPrefs(String ... prefs) {

    // Define default return values. These should not display, but are needed
    final String STRING_ERROR = "error!";
    final Integer INT_ERROR = -1;
    // ...
    final Set<String> SET_ERROR = new HashSet<>(1);

    // Add an item to the set
    SET_ERROR.add("Set Error!");

    // Loop through the Shared Prefs
    Log.i("Loading Shared Prefs", "-----------------------------------");
    Log.i("------------------", "-------------------------------------");

    for (String pref_name: prefs) {

        SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
        Map<String, ?> prefMap = preference.getAll();

        Object prefObj;
        Object prefValue = null;

        for (String key : prefMap.keySet()) {

            prefObj = prefMap.get(key);

            if (prefObj instanceof String) prefValue = preference.getString(key, STRING_ERROR);
            if (prefObj instanceof Integer) prefValue = preference.getInt(key, INT_ERROR);
            // ...
            if (prefObj instanceof Set) prefValue = preference.getStringSet(key, SET_ERROR);

            Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                  String.valueOf(prefValue));

        }

        Log.i("------------------", "-------------------------------------");

    }

    Log.i("Loaded Shared Prefs", "------------------------------------");

}

}

Logcat Output

My Shared Preference values are all String, but this is the output using either of the 2 methods above...

I/Loading Shared Prefs﹕ -----------------------------------
I/------------------﹕ -------------------------------------
I/Shared Preference : FAVORITE - 135397﹕ Jurassic World
I/Shared Preference : FAVORITE - 87101﹕ Terminator Genisys
I/Shared Preference : FAVORITE - 177677﹕ Mission: Impossible – Rogue Nation
I/------------------﹕ -------------------------------------
I/Shared Preference : WATCHED - 177677﹕ Mission: Impossible – Rogue Nation
I/Shared Preference : WATCHED - 157336﹕ Interstellar
I/Shared Preference : WATCHED - 135397﹕ Jurassic World
I/Shared Preference : WATCHED - 87101﹕ Terminator Genisys
I/------------------﹕ -------------------------------------
I/Shared Preference : WILL_WATCH - 211672﹕ Minions
I/Shared Preference : WILL_WATCH - 102899﹕ Ant-Man
I/------------------﹕ -------------------------------------
I/Loaded Shared Prefs﹕ ------------------------------------
Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58
4

Run the application in Emulator after you insert some data, just close the application.

Now open the DDMS or Android Monitor and select your emulator, on the right side you can see the File Explorer, look for Data folder in it and look for your application package that you have created, in that you can find the shared preference file open it , you can see the XML file, click it and click the pull a file from the device button in the top right corner.

The XML file will be saved in your desired location, then you can open it using any editor like notepad++ and can view the data you have entered.

Naren
  • 99
  • 3
  • In **Android Studio 3.0**, I found it in **Device File Explorer** > data > data > (package name) > shared_prefs > (package name)_preferences.xml – Josselin Dec 17 '17 at 14:39
4

In Android Studio 3:

  • Open Device File Explorer (Lower Right of screen).
  • Go to data/data/com.yourAppName/shared_prefs.

or use Android Debug Database

Jeffrey
  • 1,998
  • 1
  • 25
  • 22
2

If you're using an emulator you can see the sharedPrefs.xml file on the terminal with this commands:

  • adb root
  • cat /data/data/<project name>/shared_prefs/<xml file>

after that you can use adb unroot if you dont want to keep the virtual device rooted.

Rodrigo García
  • 1,357
  • 15
  • 26
1

I always find these commands useful in console:

  1. Find the correct file name

    adb shell

    ls /data/data/com.your.package/shared_prefs

  2. Get the file to local directory

    adb pull /data/data/com.your.package/shared_prefs/the_file_you_want $local_dir

  3. Check it in your $local_dir.

Regular Jo
  • 5,190
  • 3
  • 25
  • 47
Rose
  • 81
  • 3
  • If your emulator/device is not rooted and an application is not debuggable, `ls` command will print: `ls: /data/data/com.your.package/shared_prefs: Permission denied`. You can change `ls` to `cd`. But the next command also cannot receive a file. Tested on emulator. To fix you should root the emulator/device. Of course, rooting is a dangerous operation. Then you should unroot. – CoolMind Mar 24 '21 at 08:51
0

Shared Preferences File path for Android Emulator in Mac

/Users/"UserName"/Documents/AndroidStudio/DeviceExplorer/"EmulatorName"/data/data/com.app.domain/shared_prefs/
Sajid Zeb
  • 1,806
  • 18
  • 32