I need to get root access for rooted devices, and I was wondering how I can request root access. I need the access for the file system. Thanks a lot.
Asked
Active
Viewed 5.5k times
34
-
1Are you targeting the native platform, or trying to do this in Java? – Keith Layne Sep 03 '11 at 21:15
-
I'm trying to do it in Java :-) – Michell Bak Sep 03 '11 at 21:16
2 Answers
25
Just exec the command su
and within that Process
you have root priviliges:
Process p = Runtime.getRuntime().exec("su");
See this blog post for full example.

dacwe
- 43,066
- 12
- 116
- 140
-
So... I do this before getting directory information, like folders and such? – Michell Bak Sep 03 '11 at 21:17
-
-
4@keith layne: When you run `su` the `Superuser` app starts which can grant the request. – dacwe Sep 03 '11 at 21:31
-
48-1 **This answer is highly misleading**. The posted code will accomplish **absolutely nothing** as only a process started by `su` will actually run as root - **The android app itself does not switch to running as root**. The code given here only starts a root shell, which immediately quits due to lack of being given any work to do. – Chris Stratton Jun 01 '13 at 02:07
-
19@ChrisStratton: I clearly state that "within that process you will have root privileges". How is that misleading? – dacwe Jun 01 '13 at 08:46
-
7Within what process? The one that dies without doing anything? How is someone supposed to realize that from this answer? Instead they think it means the "calling" process. People are citing this answer in mistake elsewhere. – Chris Stratton Jun 01 '13 at 10:55
-
Chris Stratton is right, this answer is overrated and doesn't exactly answers the question. – xmen Jun 09 '13 at 01:16
-
I am getting "Caused by: java.io.IOException: Cannot run program "su": error=13, Permission denied". How can I configure my emulator to get this permission? – sadat Apr 03 '20 at 05:57
7
WARNING
THIS METHOD IS NO LONGER VALID From 5.0
Most root devices use Superuser or SuperSU, if you wanna root ability in your app, you should claim superuser permission in your app manifest.
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
More detailed information, you should check HOW-To SU by Chainfire (The author of SuperSU) and look into the example source libsuperuser(https://github.com/Chainfire/libsuperuser)

liuyix
- 594
- 5
- 7
-
9`"Due to changes in Android 5.0 Lollipop, this permission has been deprecated and is completely ignored from SuperSU v2.30 onwards"` - [How-To SU](http://su.chainfire.eu/) – Ashish Tanna Mar 11 '15 at 20:33