25

I'm learning Android programming, and I want to make an application which has to run as root. The logical thing would be to add a root permission in the Android Manifest.

I saw this link in the documentation, and especially noted the FACTORY_TEST permission:

public static final String FACTORY_TEST

Since: API Level 1

Run as a manufacturer test application, running as the root user. Only available when the device is running in manufacturer test mode. Constant Value: "android.permission.FACTORY_TEST"

Is that the best way?

If it's not possible using the SDK, how can I make a "root" application work?

1
  • 1
    Please note that while this upvoted questions has an answer with a lot of upvotes, that answer is wrong and useless, and the mechanisms it inaccurately alludes to are themselves long removed. It accomplishes nothing, and arises from a fundamental misunderstanding of the Linux kernel and Unix security and process model on which Android relies. May 28, 2016 at 19:23

3 Answers 3

29

What you need to do is something like:

Process root = Runtime.getRuntime().exec("su");

That causes SuperUser to show, which lets you either Allow or Block it from root access. This approach might not work if the user is not rooted. Here is a way you can test it.

3
  • 27
    This does not make the application itself run as root. What this does, on systems which support it, is allow the application to launch a helper program in a new process which runs as root. Further, the code shown above accomplishes almost nothing, since no worker process is launched. All that this is likely to do is show the user confirmation dialog of one of the su hacks, and possible cache the user's choice so that they are not asked again if another invocation of su is made. But nothing is yet run as root. Dec 28, 2012 at 15:51
  • Agree with Chris' comment above - you'll need a worker process to run and get the result from this process to determine if you have root permission or not.
    – ChuongPham
    Feb 3, 2013 at 17:36
  • 2
    @Mohit: The link you posted will not work in all situation. Checking for 255 is not foolproof. It's better to do something like isRooted = (p.exitValue() != 0) ? false : true;.
    – ChuongPham
    Feb 3, 2013 at 17:45
17

First lets us get the basics right. Android run Linux kernel underneath. Now if you have to run your process on it with super user privileges(run it as root) the only way is to execute your process is via command line because it is the only way you can directly interact with the kernel. Also you need to use su before running any command. Also as Chris has mentioned in his comment on the 1st answer

Process process = Runtime.getRuntime().exec("su");

will accomplish nearly nothing. It will just ask for super use privilege using dialog. What you can do is instead of just executing su you can execute your process with su as following

Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", yourCommand});

The -c Option

Among the most commonly used of su's few options is -c, which tells su to execute the command that directly follows it on the same line. Such command is executed as the new user, and then the terminal window or console from which su was run immediately returns to the account of the former user after the command has completed execution or after any program that it has launched has been closed.(More details)

Alternate Option

Alternative to above method one another way that might work is to use command line to copy you app to /system/app/ directory. Then your application will run automatically with root privileges(same as System apps)

3
  • 8
    No, apps in /system/app do not run as root either. They may be able to obtain more Android-level permissions, but are still not root. May 20, 2014 at 15:13
  • In order to by root, you must sign with the system certificate. Like Chris said, workarounds like putting the app in /system/app or running "su" will NOT make your app run as root.
    – flicflac
    Oct 3, 2014 at 8:00
  • 2
    @flicflac I think signing with the system certificate still doesn't make your app run as root. It only gives you more permissions (Android level). Or am I wrong? Thank you!
    – sjkm
    Nov 17, 2016 at 10:35
12

The SDK does not offer a way to run an app as root.

2
  • 21
    OK, Mr. Google :) I should reformulate my question "How can I make an application running as root ?"
    – Rob
    Mar 14, 2011 at 1:12
  • 1
    @Rob - you cannot. At most helper processes can be run as root, not applications themselves. May 28, 2016 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.