Android: Modify SharedPreferences of another app

10,022

Solution 1

Take a look at this answer:

https://stackoverflow.com/a/13139280/361230

When you use su, you will have the permission to modify whatever you want on the filesystem.

However, I agree with Chris on the fact it's not really nice to do what you want to.

Solution 2

To actually answer your question, if you have root, you can do it with reflection. I have done it on android 4.1, on other platforms it may work differently. The good thing is that shared preferences is not accessible only by system services, so you can access "hidden" methods via reflection. Put this in a try-catch:

Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /data/data/package_name/shared_prefs/package_name_preferences.xml" });
proc.waitFor();
proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /data/data/package_name/shared_prefs" });
proc.waitFor();
proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "chmod 777 /data/data/package_name" });
proc.waitFor();

File preffile = new File("/data/data/package_name/shared_prefs/package_name_preferences.xml");

Class prefimplclass = Class.forName("android.app.SharedPreferencesImpl");

Constructor prefimplconstructor = prefimplclass.getDeclaredConstructor(File.class,int.class);
prefimplconstructor.setAccessible(true);

Object prefimpl = prefimplconstructor.newInstance(preffile,Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);


Editor editor = (Editor) prefimplclass.getMethod("edit").invoke(prefimpl);
//put your settings here
editor.commit();

Solution 3

I had the same problem, I tried everything, every suggestion I found on StackOverflow and other places on the web and nothing helped. I always ended up with the "No permission" error even though my device is rooted.

Finally, I came up with an elegant solution using Xposed.

In my solution below, I'm hooking the onCreate method of the Application class, then I'm getting a hold on the Context of the app I want to edit it's SharedPreferences and then I'm using the Context to edit the SharedPreferences. Here it is:

public class MainXposed implements IXposedHookLoadPackage{
    private static final String TAG = "MainXposed";

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        Log.d( TAG, "handleLoadPackage: loadPackageParam.packageName --> " + loadPackageParam.packageName );
        if ( loadPackageParam.packageName.contains( "com.someapp" ) ) {
    
            findAndHookMethod("android.app.Application", loadPackageParam.classLoader, "onCreate", new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                
                }
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    Log.d( TAG, "afterHookedMethod()" );

                    String pkg = "com.someapp";
                    Application application = ( Application ) param.thisObject;
                    Context someappContext = application.getApplicationContext();
    
                    someappContext.createPackageContext( pkg ,Context.CONTEXT_IGNORE_SECURITY );
                    SharedPreferences someapp_prefs = someappContext.getSharedPreferences(pkg + "_preferences", Context.MODE_PRIVATE );
                    SharedPreferences.Editor editor = someapp_prefs.edit();
                    editor.putString( "some_prefs_key", "someNewValue" );
                    editor.apply();
                }
            });
        }
    }
}
Share:
10,022
Wolf6969
Author by

Wolf6969

Updated on July 17, 2022

Comments

  • Wolf6969
    Wolf6969 almost 2 years

    I'm trying to write application that must read, modify and save some settings in Shared preferences of another application (data/data/package_name/shared_prefs/file.xml).

    This application isn't mine, and I have rooted device for testing.

    What android permissions i should add to manifest and how can I access this file and modify it? I know that SharedPreferences are unique to each App/APK, but I need to modify it in root mode.

    I have working code to modify xml file on sdcard, but when I change path to "data/data/package_name/shared_prefs/file.xml" it gives me an exception and message

    android open failed eacces (permission denied)
    

    Is there a way I can achieve that?

  • Wolf6969
    Wolf6969 over 11 years
    I can do this by any explorer with root mode, why I can't do this from my app?
  • Wolf6969
    Wolf6969 over 11 years
    Can you explain, I just need to run SU and grant permissions for my app or i should execute special command in SU shell to change file attributes?
  • Shade
    Shade over 11 years
    @Wolf6969, to be honest, I've only read up on such stuff out of curiosity. However, if you read the answer I linked to, it seems that once you execute su, the app starts running with superuser permissions. So, you only add the first line to your code, and afterwards you won't get permission problems. I'll try this out tomorrow, but why don't you try it out yourself?
  • Shade
    Shade over 11 years
    it's not like it's a secret or something. Besides, many people are using root for modding purposes and such.
  • Mgamerz
    Mgamerz over 11 years
    That doesn't change the fact that you are wanting to do it progmatically vs people doing it manually.
  • cnexus
    cnexus over 11 years
    That won't be the case. Having your app be granted superuser permissions gives you the ability to run commands as root. The app itself is still running in a sandbox and you wont be able to access those shared_prefs with anything but 'Runtime.exec() ` commands, or other system commands. What I would suggest is to use ansu command to copy the file to a location where you DO have read/write access (maybe your apps data folder), modify it, and then use anothersu command to copy it back and overwrite the existing file.
  • Shade
    Shade over 11 years
    What I meant was that there are apps that require root to operate, but still provide a benefit (rather than harm you). Many such apps you can encounter when using a custom ROM on your device -- f.e. apps for modifying your navigation bar (ICS+).
  • azyoot
    azyoot over 10 years
    The permission changes on the directories may not be needed, just on the actual preferences file. And of course, this has a lot of security issues, but I'm assuming you are aware of the dangers of root.
  • Muhammad Babar
    Muhammad Babar about 4 years
    Thank you very much, but why we have to perform chmod 777 3 times?