7

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

adb shell setprop persist.sys.aabbcc 123456

and:

adb shell getprop persist.sys.aabbcc 
123456

I also able to 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 Linux command exec:

            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) that property. My code (that seems not working) was:

    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= "";       
  }

Not working also using exec:

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

Please could you tell me if you are able to set a system property in Android java? Thanks.

trung
  • 81
  • 1
  • 1
  • 5
  • Do you want to set the property for all processes on the phone or is it enough to set it for your app? – Robert Nov 01 '11 at 08:19
  • I need for all processes; so I can read this property in adb shell command. – trung Nov 01 '11 at 10:50
  • Thanks for your code to perform a 'getprop' in Java (with reflection) to read a native property. I was about to punch a wall because Android documentation doesn't say a peep about the native/Java access limitation. – Someone Somewhere Apr 13 '12 at 21:17
  • Actually, thanks for that exec method. The Reflection method isn't reliable. – Someone Somewhere Apr 15 '12 at 01:35
  • thanks for the reflection code, the exec method hangs every now and then on ICS (but not on android < 4). – SteelBytes Jul 05 '12 at 04:46

2 Answers2

4

There is a big difference from running on the command line to setting via an app. When you su in the shell then the process is running as root and when the command finally gets through to the properties service and it checks your UID then it will allow the write as root is allowed to write to any property.

When you reflect the android.os.SystemProperties and make the call then you will make the request as the UID of the application and it will be rejected as the properties service has an ACL of allowed UIDs to write to particular key domains, see /system/core/init/property_service.c

See my answer here on how to make it work: https://stackoverflow.com/a/11123609/1468536

Community
  • 1
  • 1
Cakey
  • 236
  • 2
  • 5
2

How to get/set properties

There are three main means to get/set properies on android.

  1. native code When writing native applications, property_get and property_set APIs can be used to get/set properties. To use them, we need to include cutils/properties.h and link against libcutils.

  2. java code Android also provides System.getProperty and System.setProperty functions in 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 native version, java version store data in a totally different place. Actually, a hashtable is employed by dalvik VM to store properties. So, java properties are separated, it can't get or set native properties, and neither vice versa.

    Update: Andrew mentioned that android.os.SystemProperties class can manipulate native properties, though it's intended for internal usage only. It calls through jni into native property library to get/set properties.

  3. shell script Android provides getprop and setprop command line tool to retrieve and update properties. They can be used in shell script. They are implemented on top of libcutils.

Gray
  • 115,027
  • 24
  • 293
  • 354
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
  • I read your post originally in rxwen.blogspot.com already, actually my code is using the 2nd method (SystemProperties) but it does not work. – trung Nov 01 '11 at 10:49