Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Modify SharedPreferences of another app

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?

like image 631
Wolf6969 Avatar asked Jan 25 '13 12:01

Wolf6969


Video Answer


1 Answers

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();
like image 75
azyoot Avatar answered Sep 17 '22 23:09

azyoot