Unable to set System property in Android using Reflection command or Linux in Android

I need to set and get a system property called "persist.sys.aabbcc". I was able to read / write the value using adb shell command as follows:

adb shell setprop persist.sys.aabbcc 123456 

and:

 adb shell getprop persist.sys.aabbcc 123456 

I can also read this property in java Android using Reflection:

  @SuppressWarnings("rawtypes") Class SystemProperties = Class.forName("android.os.SystemProperties"); //Parameters Types @SuppressWarnings("rawtypes") Class[] paramTypes = new Class[1]; paramTypes[0] = String.class; Method get = SystemProperties.getMethod("get", paramTypes); //Parameters Object[] params = new Object[1]; params[0] = new String("persist.sys.aabbcc"); ret = (String) get.invoke(SystemProperties, params); 

Or using the Linux exec command:

  try { String line; java.lang.Process p = Runtime.getRuntime().exec("getprop persist.sys.aabbcc"); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { System.out.println(line); Log.d("HelloDroid", line); } input.close(); } catch (Exception err) { err.printStackTrace(); } 

However, I cannot set (write) this property. My code (doesn't seem to work):

  try { @SuppressWarnings("rawtypes") Class SystemProperties = Class.forName("android.os.SystemProperties"); Method set1 = SystemProperties.getMethod("set", new Class[] {String.class, String.class}); set1.invoke(SystemProperties, new Object[] {"persist.sys.aabbcc", "999999"}); } catch( IllegalArgumentException iAE ){ throw iAE; } catch( Exception e ){ ret= ""; } 

Using exec also doesn't work:

  process = Runtime.getRuntime().exec("setprop persist.sys.aabbcc 555"); 

Please, could you tell me if you can set the system property in Android java? Thanks.

+6
source share
2 answers

There is a big difference from running the command prompt through the application. When you work in the shell, the process starts as root, and when the team finally goes to the property service and checks your UID, then it allows you to write as root, you can write any property.

When you mirror android.os.SystemProperties and make a call, you will make the request as the UID of the application and it will be rejected because the property service has ACLs of allowed UIDs for writing to specific key domains, see / system / core / init / property _service. c

See my answer here how to make it work: fooobar.com/questions/4126 / ...

+4
source

How to get / set properties

There are three main ways to get / set properties on Android.

  • native code When creating custom applications, the property_get and property_set APIs can be used to get / set properties. To use them, we must include cutils / properties.h and a link to libcutils.

  • Android Java code also provides the System.getProperty and System.setProperty functions in the java library, our Java application can use them to get / set properties. But it’s important to note that although these Java APIs are semantically equal to the original version, the Java version stores the data in a completely different place. In fact, the hash table is used by dalvik VM to store properties. Thus, java properties are separated, they cannot get or set their own properties, and not vice versa.

    Update: Andrey noted that the android.os.SystemProperties class can manage its own properties, although it is intended for internal use only. It calls through jni to its own property library to get / set properties.

  • The Android shell script provides getprop and setprop command-line tools for retrieving and updating properties. They can be used in a shell script. They are implemented on top of libcutils.

+1
source

Source: https://habr.com/ru/post/900487/


All Articles