9

Is it possible to run my program as root? I know how to run command-line native utils, but how to run Java program as root?

artem
  • 16,382
  • 34
  • 113
  • 189
  • Have a look at this: http://stackoverflow.com/questions/5293615/how-can-i-get-root-permissions-through-the-android-sdk – Romain Piel Feb 08 '12 at 14:00
  • i found a library for you here take a look: https://code.google.com/p/roottools/wiki/Usage it's free to use @RomainPiel – gumuruh Jul 07 '14 at 06:29

1 Answers1

13

This will work for you:

try {
    Process process = Runtime.getRuntime().exec("su");
    process.waitFor();
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

You could use this for a command:

exec(new String[] { "su", "-c", COMMAND });

Best wishes, Tim

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
Tim
  • 6,692
  • 2
  • 25
  • 30
  • 2
    Yes, I know it. But how to run the non-commandline android java app with root permissions? – artem Feb 08 '12 at 14:04
  • 10
    @RankoR You can't. To run as root you have to use the command line, because that's the only way you interact directly with the linux kernel. You can however use the command line to copy your app to the /system/app/ directory, and then you have the same permissions as system apps. Also, check out[`RootTools`](http://code.google.com/p/roottools/). It's very helpful for running root. – Reed Feb 08 '12 at 14:23
  • and also, you can run "su" only on a rooted device – Ovidiu Latcu Feb 08 '12 at 14:33
  • 1
    Like Jakar said, install your app to the /system/app/ directory. Technically, any app installed in this directory can run with root privileges... – ChuongPham Feb 03 '13 at 17:52
  • 1
    @Jakar Does it mean that if my app needs to get the list of files in the internal storage folder of another app, it needs to use "ls" (or something similar) instead of "new File(path).list()" ? – android developer Apr 01 '13 at 22:06
  • @Chuong, I believe that being in /system/app/ allows the app to use API that only system apps are allowed, thus making root not needed. I don't believe it has an effect on command line actions as utilizing root (`su`) does. – Reed Apr 06 '13 at 16:48
  • @androiddeveloper You would need to use `ls`, unless as Chuong mentioned, you move your application to the /system/app/ directory. Then `new File(path).list()` should be able to list internal storage folder. – Reed Apr 06 '13 at 16:49
  • @Jakar Thank you. I guess you've tested it, and therefore you've written it right after saying you believe in it, right? Anyway, can't root access give you the permission to use the normal API for this task? – android developer Apr 06 '13 at 16:54
  • @androiddeveloper I have not recently tested it, so I may be incorrect, but when you use `su` command and are granted root access it changes the linux UID of the process to `0`. This does not affect the application itself, just the process in which the commands are running. Thus, it does not affect API access and the `File` command will not have any further abilities. However, getting root access could allow you to install your app to `/system/app/` and then it will have the same API access as system apps and should allow `File` to have greater access – Reed Apr 06 '13 at 17:45
  • 1
    @Jakar thank you. However, it could be nicer to have "super powers" API when you get root permission, instead of using console-commands in this case. – android developer Apr 06 '13 at 18:17
  • 1
    Just tested out the `/system/app` theory, it did not work for me. Can anyone confirm that it works? – CatShoes Jun 11 '13 at 18:54
  • The /system/app method does work. I've installed apps that i've written and signed with my own key in that partition using the post-build method (mounting the image and then adding in the APKs manually) and was able to run them without a problem. BUT I have run into issues pre-installing apps using this method with certain APKs like Hulu Plus. – Android Noob Jun 11 '13 at 19:34
  • @Time How is (new String[] {"su", "-c", command}) different from ("su -c + command) (besides that one works and the other doesn't :))? – David Doria Sep 17 '13 at 18:39
  • In my tests it never worked with just passing the whole string. Seems like it needs the arguments seperately. – Tim Sep 20 '13 at 08:37
  • @Jakar: I copied my apk (a filemanager) to /system/app, gave 644 permissions, and rebooted. Yet, my app is still run without root privileges (deny access to some folders). Any clue? Thanks! – Luis A. Florit Sep 11 '14 at 12:51
  • @LuisA.Florit, I would suggest posting a new question for your specific problem then I'll try to help. – Reed Sep 11 '14 at 17:57
  • 1
    @Jakar: No problem: http://stackoverflow.com/questions/25799038/failed-running-an-app-as-root – Luis A. Florit Sep 12 '14 at 00:42
  • 1
    Single string doesn't work because your Runtime object isn't a shell and isn't parsing the line. It will see the entire string as the command, without any arguments. Running 'su' doesn't change the API because the su command is a child process. The kernel gives 'su' root because it has the setuid bit set and is owned by root. Thus, su is root and its children (the line you pass) will be owned and run as that user. The parent (your Android app) is not changed by running another app. That would be a horrible security problem. There is no easy "get root" API to make carriers happy. – Evan Langlois Dec 14 '15 at 19:05
  • I copied that java code in Oncreate of my application class. It caused my app doesn't start at all. The app just shows a while page. Does anybody know the reason ? – Fahime Ghasemi Jul 26 '22 at 08:21