How to Examine and Modify SharedPreferences from ADB Shell

How to Examine and Modify SharedPreferences from ADB Shell

30 November 2024 Stephan Petzl Leave a comment Tech-Help

SharedPreferences in Android are a convenient way to store simple key-value pairs, often used for settings or preferences within an app. At times, developers need to examine or modify these preferences directly from the command line, especially during debugging. This guide will walk you through the process of accessing and editing SharedPreferences using ADB shell.

Understanding SharedPreferences Storage

SharedPreferences are stored as XML files in the app’s private data directory. The typical path for these files is:

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

The filename usually follows the convention:

your.app.package.name_preferences.xml

These files are essentially key-value maps, making them straightforward to read and modify.

Accessing SharedPreferences Using ADB

To access these files, follow these steps:

  1. Open your terminal and connect to your device using ADB:
  2. adb shell
  3. If your app is debuggable, use the run-as command to gain access to the app’s data directory:
  4. run-as your.app.package.name
  5. List the files in the shared_prefs directory to find the file you need:
  6. ls /data/data/your.app.package.name/shared_prefs/
  7. Use the cat command to view the contents of the XML file:
  8. cat /data/data/your.app.package.name/shared_prefs/your.app.package.name_preferences.xml

Modifying SharedPreferences

To modify the SharedPreferences file:

  1. Pull the file to your local machine:
  2. adb pull /data/data/your.app.package.name/shared_prefs/your.app.package.name_preferences.xml
  3. Edit the file using your preferred text editor, such as vim or nano.
  4. Push the modified file back to the device:
  5. adb push your.app.package.name_preferences.xml /data/data/your.app.package.name/shared_prefs/

Note: Ensure the app is restarted to apply the changes.

Handling Permission Issues

If you encounter permission errors, you may need to execute commands with adb exec-out or ensure your device is in root mode. For more complex scenarios, refer to our guide on granting app permissions using ADB without root.

Using Repeato for Simplified Testing

For developers looking to streamline their testing process, Repeato offers a no-code test automation solution. Repeato leverages ADB and allows you to execute ADB commands within automated test scripts. This functionality can be particularly useful when testing changes in SharedPreferences, as it ensures precise timing and execution of command sequences. Learn more about how Repeato can enhance your testing workflow on our Android Testing Tool page.

By following these steps and utilizing tools like Repeato, you can efficiently examine and modify SharedPreferences, aiding in a smoother debugging and development process.

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