How to examine SharedPreferences from adb shell?

37,546

Solution 1

Fine, I found the file just after I raised the question above. (It seem asking questions publicly stimulate me to search answers by myself much more diligently, since I don't want my dear peers to view me as a lazy programmer.)

It is an XML file under /data/data/your.app.package.name/shared_prefs, and the file name is your.app.package.name_preferences.xml. It is really easy to modify the preferences when you figure out that the content is just a key-value map.

Solution 2

If the app is debugable you could do:

$ adb shell
$ run-as <app-package-id>
$ cat /data/data/<app-package-id>/shared_prefs/prefs.xml

Note that the preference might be stored in another file so better check the directory to find it:

$ ls /data/data/<app-package-id>/shared_prefs/

Solution 3

I am using this convenient one-liner to pull, edit in vim, and push shared preferences for an app:

APP_ID=com.myapp; adb pull /data/data/${APP_ID}/shared_prefs/${APP_ID}_preferences.xml /tmp/${APP_ID}_preferences.xml && vim /tmp/${APP_ID}_preferences.xml && adb push /tmp/${APP_ID}_preferences.xml /data/data/${APP_ID}/shared_prefs/

Just set APP_ID to your application id.

Note that this assumes you are using the default file name for shared preferences, as obtained from PreferenceManager.getDefaultSharedPreferences(context). Also, ADB needs to be running in root mode: adb root

Solution 4

Helper bash function

function adb-pull-prefs {
    # ${1} - app package
    # ${2} - prefs name
    adb exec-out run-as ${1} cat /data/data/${1}/shared_prefs/${2}.xml
}

Solution 5

In case anyone else is running into "Permission Denied" errors using all of the above suggestions like I was, you may need to use exec-out like this:

adb exec-out run-as <package.name> cat /data/data/<package.name>/shared_prefs/<package.name>_preferences.xml
Share:
37,546

Related videos on Youtube

an0
Author by

an0

Updated on July 09, 2022

Comments

  • an0
    an0 almost 2 years

    Now that we can Examining sqlite3 Databases from a Remote Shell, is it possible to examine SharedPreferences from adb shell? Since it would be much more convenient to examine and manipulate SharedPreferences from command line when debugging.

    Or put in another way, in what files SharedPreferences are saved, and how to view and modify these files?

  • susparsy
    susparsy over 10 years
    As asked, how can you edit it?! VI and Nano editors are not installed. How do you edit it than???
  • Pedro Teran
    Pedro Teran over 9 years
    so what about when you have the flag mode_private when you write the preferences... where the file goes?
  • Jim
    Jim about 6 years
    Note that under some conditions, the file might end up in a different directory, for instance /data/user_de/0/your.app.package.name/shared_prefs. Don't be afraid of searching a bit
  • WindRider
    WindRider almost 6 years
    On newer versions the default path is /data/user/0/<app-package-id>...
  • Ender
    Ender over 5 years
    package debuggable is needed
  • Olkunmustafa
    Olkunmustafa over 4 years
    It gives permission denied error on Android emulator.
  • Oush
    Oush almost 3 years
    You need root access for this to work, otherwise you will get a Permission denied
  • Setaa
    Setaa almost 3 years
    @Oush do you mean the Android device itself must be rooted or is it sufficient to use ** adb root**?
  • Phani Rithvij
    Phani Rithvij about 2 years
    only this has worked for me among all the answers listed here.