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:

Unknown said...

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

Thank you wherever you are.
Kevin

cirdes said...

It works for me,
thanks

Anonymous said...

Thanks. It's works :-)

Android Developer said...

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?!