Android: changing SharedPreferences of another application

I am trying to write an application that should read, modify and save some parameters in the general preferences of another application ( data/data/package_name/shared_prefs/file.xml ).

This application is not mine, and I have a root device for testing.

What android permissions should I add to the manifest and how can I access this file and change it? I know that SharedPreferences unique for each application / APK, but I need to change it in root mode.

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

 android open failed eacces (permission denied) 

Is there any way I can achieve this?

+6
source share
4 answers

Take a look at this answer:

fooobar.com/questions/375202 / ...

When you use su, you will have permission to change what you want on the file system.

However, I agree with Chris that it is not very pleasant to do what you want.

+1
source

To answer your question, if you have a root, you can do it with reflection. I did it on android 4.1, on other platforms it may work differently. It’s good that the general settings are not available only to system services, so you can access the β€œhidden” methods through 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(); 
+8
source

If you do not own another application, you should not do this. The only way without root would be to be readable in a world that depreciates and is almost never used.

I doubt that someone will give you the answer you need if you do not indicate why you need access.

0
source

You should not change the preferences of another application, root or not.

-1
source

All Articles