Search code examples
androidshelladbrootpreferences

Android Shell edited file isn't recognized by app anymore


First-time poster here, I apologize for any mistakes. :) I searched for quite the while on this problem but could't find anything.

I am trying to edit a shared_preferences-file of another android application. (I have the developer's permission to do so.) The file is an xml structured like this:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="password">XYZ</string>
    <string name="username">XYZ</string>
    <boolean name="2.6.0.5" value="false" />
    <boolean name="disclaimer.isAccepted" value="true" />
    <string  name="safeForMode2">7</string>
</map>

I load the file with a root-terminal, edit the content and write it back to the file.

Loading code:

String[] commands = {"cd <directory>", "cat pref-file.xml"};  
String output = RunAsRoot(commands);
settings = output;

Writing code:

String[] commands = {"cd <directory>", "echo '" + settings + "' > pref-file.xml"};
RunAsRoot(commands);

RunAsRoot gets SU-Privilieges and executes the commands in the String-Array. This part works fine.

If I edit the file using e.g. ES File Explorer, everything works fine. The settings are applied and it works. If I try to edit it with above code, the modified file isn't recognized by the app and will be overwritten with it's standard-xml, resulting in a complete reset of the app's settings.

I already checked the file-permissions and set them identical to the source-file, but the app won't accept it. In the text-editor both versions of the file look exactly the same.

Do you experienced this issue somewhere before? As it seems, both files are identical to each other. I can't find the reason why the app won't accept it. If you need more codesnippets, I'll be glad to post them.

Again, apologies if this question has been answered before. In this case I would be very happy to get a link. ;)

Thanks in advance!

EDIT: Fixed a mistake in the xml-example. Header weren't included.


Solution

  • Use awk to alter the file content and writing it back using dd:

    awk '/map/{print " <string name=\"foo\">"}1 | dd of=/path/to/xml
    

    This keeps the original file but exchanges the whole content.