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?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Rob
  • 2,766
  • 5
  • 32
  • 39
  • 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. – Chris Stratton May 28 '16 at 19:23

3 Answers3

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.

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
  • 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. – Chris Stratton Dec 28 '12 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 03 '13 at 17:36
  • 2
    @Mohit: The [link](http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/) 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 03 '13 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)

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 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. – Chris Stratton May 20 '14 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 03 '14 at 08: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 '16 at 10:35
12

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

Romain Guy
  • 97,993
  • 18
  • 219
  • 200