October 17, 2010

how to correctly execute su from Android application

This way:
Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "setprop ctl.stop zygote"});
If you are getting failures like this:
W/su ( 1043): request rejected (0:0->0:0 /system/bin/setprop)
that means that
  • you didn't send "-c", or
  • you didn't give it a parameter, or
  • you gave it more than one parameter
You have to make sure that after "-c" you only send one parameter. Alternate syntax would be this:
Runtime.getRuntime().exec("/system/bin/su -c 'setprop ctl.stop zygote'");

4 comments:

  1. Fantastic. After weeks of searching this was exactly the post I was looking for.

    Thank you wherever you are.
    Kevin

    ReplyDelete
  2. It works for me,
    thanks

    ReplyDelete
  3. Anonymous6:25:00 PM

    Thanks. It's works :-)

    ReplyDelete
  4. Android Developer7:30:00 PM

    It gives me an error "null environment"!
    When trying to run it manually in the android terminal emulator, it also gave an error: "/system/bin/su not found"!

    My phone is rooted and I can execute root commands manually from the emulator, but programmatically I am not able to do that!

    Can you help?!

    ReplyDelete