7

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?

tckmn
  • 57,719
  • 27
  • 114
  • 156
Wolf6969
  • 147
  • 1
  • 3
  • 10

5 Answers5

10

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();
azyoot
  • 1,162
  • 8
  • 18
  • 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. – azyoot Aug 08 '13 at 10:56
  • Thank you very much, but why we have to perform chmod 777 3 times? – Muhammad Babar Feb 25 '20 at 07:03
2

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.

Community
  • 1
  • 1
Shade
  • 9,936
  • 5
  • 60
  • 85
  • 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? – Wolf6969 Jan 25 '13 at 13:10
  • @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 Jan 25 '13 at 21:46
  • 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 an`su` 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 another`su` command to copy it back and overwrite the existing file. – cnexus Jan 25 '13 at 23:32
1

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();
                }
            });
        }
    }
}
Barakuda
  • 790
  • 8
  • 16
-2

You should not be changing the preferences of another app, rooted or not.

Chris Banes
  • 31,763
  • 16
  • 59
  • 50
-2

Unless you're the owner of the other application, you shouldn't be doing this. The only way without root would to have it be world readable, which is depreciated and almost never used.

I doubt anyone is going to give you an answer you want unless you state why you need access.

Mgamerz
  • 2,872
  • 2
  • 27
  • 48
  • it's not like it's a secret or something. Besides, many people are using root for modding purposes and such. – Shade Jan 25 '13 at 21:49
  • That doesn't change the fact that you are wanting to do it progmatically vs people doing it manually. – Mgamerz Jan 25 '13 at 22:27
  • 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+). – Shade Jan 26 '13 at 00:10