47

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?

yAnTar
  • 4,269
  • 9
  • 47
  • 73
an0
  • 17,191
  • 12
  • 86
  • 136

8 Answers8

53

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.

an0
  • 17,191
  • 12
  • 86
  • 136
  • 3
    As asked, how can you edit it?! VI and Nano editors are not installed. How do you edit it than??? – susparsy Feb 10 '14 at 13:41
  • 2
    so what about when you have the flag mode_private when you write the preferences... where the file goes? – Pedro Teran Dec 04 '14 at 20:06
  • 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 – Jim May 21 '18 at 09:12
46

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/
joecks
  • 4,539
  • 37
  • 48
13

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

friederbluemle
  • 33,549
  • 14
  • 108
  • 109
6

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
}
eleven
  • 6,779
  • 2
  • 32
  • 52
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
CFL_Jeff
  • 2,589
  • 3
  • 31
  • 49
1

First pull shared preferences file from device. This requires root permision.

adb pull /data/data/org.test/shared_prefs/MyKeys.xml MyKeys.xml

Now MyKeys.xml is stored in current directory of your system.

Modify values by

vim MyKeys.xml

After editing file save changes and push to device.

adb push MyKeys.xml /data/data/org.test/shared_prefs/MyKeys.xml

To Verify your changes

adb shell
cat /data/data/org.test/shared_prefs/MyKeys.xml
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
1

If you are using shared_preferences for Flutter, the file is /data/data/your.app.package.name/shared_prefs/FlutterSharedPreferences.xml. Note that if you edit the file, you must restart your app for your changes to be visible to your app. Doing a hot reload/hot restart doesn't expose your manual changes to your app.

user2233706
  • 6,148
  • 5
  • 44
  • 86
0

If you'd like to edit Shared Preferences from adb scripts, please see how to get root on Android emulator here. (If using 'adb root' is not enought in your case).

If you wish to install vi editor/busybox on Android go here. [OPTIONAL]

To edit a shared preference value, you need to first COPY app's xml file to SDstorage, copy it to your local filesystem, edit and then upload back to the phone.

adb shell
su
cp /data/data/com.your.package.name/shared_prefs/com.your.package.name_preferences.xml /storage/emulated/0/
adb pull /storage/emulated/0/com.your.package.name_preferences.xml
nano com.your.package.name_preferences.xml
adb push com.your.package.name_preferences.xml /storage/emulated/0/com.your.package.name_preferences.xml
cp /storage/emulated/0/com.your.package.name_preferences.xml /data/data/com.your.package.name/shared_prefs/com.your.package.name_preferences.xml

Don't forget to RESTART the app to see the results.