How to examine SharedPreferences from adb shell?

Viewed 39163

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?

8 Answers

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.

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.

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.

Related