How to Examine and Modify SharedPreferences from adb Shell

How to Examine and Modify SharedPreferences from adb Shell

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Debugging and adjusting SharedPreferences directly from the adb shell can significantly streamline the development process. This guide will walk you through the steps to view and modify SharedPreferences files from the command line, making it easier to manage your app’s settings during development.

Understanding SharedPreferences File Location

SharedPreferences are stored as XML files in your app’s data directory. Specifically, you can find them under:

/data/data/your.app.package.name/shared_prefs

The filename typically follows the pattern: your.app.package.name_preferences.xml.

Viewing SharedPreferences

To view the contents of a SharedPreferences file, you can use the following command:


$ adb shell
$ run-as your.app.package.name
$ cat /data/data/your.app.package.name/shared_prefs/your.app.package.name_preferences.xml
  

This will display the XML content of the SharedPreferences file, allowing you to inspect the key-value pairs.

Modifying SharedPreferences

To modify the SharedPreferences file, you can pull it to your local machine, edit it, and push it back. Here’s a convenient one-liner that accomplishes this:


APP_ID=your.app.package.name
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/
  

This command sequence pulls the SharedPreferences file, opens it in vim for editing, and then pushes the modified file back to the device.

Handling Permissions

If you encounter “Permission Denied” errors, try using the exec-out command:


adb exec-out run-as your.app.package.name cat /data/data/your.app.package.name/shared_prefs/your.app.package.name_preferences.xml
  

This method ensures that you have the necessary permissions to access the file.

Conclusion

Managing SharedPreferences through the adb shell can be a powerful tool for debugging and development. By understanding where these files are stored and how to view and modify them, you can streamline your workflow and quickly make necessary adjustments.

Enhancing Your Testing Workflow with Repeato

For a more comprehensive and automated approach to testing your Android and iOS apps, consider using Repeato. Repeato is a no-code test automation tool that leverages computer vision and AI to create, run, and maintain automated tests efficiently. With features like built-in ADB support and script steps for executing ADB commands, Repeato can help you automate the process of inspecting and modifying SharedPreferences, along with other testing tasks.

Learn more about how Repeato can enhance your testing workflow by visiting our documentation.

Like this article? there’s more where that came from!