Skip to content

Android: Requesting root access in your app

by mat on January 17th, 2010

This snippet shows how root access can be requested inside an application in order to write a file into a place we do not have permission to access usually. Requesting root access will only work if your phone allows it, or it has been ‘rooted’ (hacked to allow superuser permissions).

Process p; 
try { 
   // Preform su to get root privledges
   p = Runtime.getRuntime().exec("su"); 
   // Attempt to write a file to a root-only 
   DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
   os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
   // Close the terminal
   try { 
           if (p.exitValue() != 255) { 
        	  // TODO Code to run on success
           else { 
        	   // TODO Code to run on unsuccessful
        	   toastMessage("not root");    
   } catch (InterruptedException e) { 
      // TODO Code to run in interrupted exception
	   toastMessage("not root"); 
} catch (IOException e) { 
   // TODO Code to run in input/output exception
	toastMessage("not root"); 

Where my “toastMessage” is just a function which creates a toast to display on the screen. On phones with superuser permissions installed (root access) this will display a dialog asking the user to allow or deny the application permission to have root access:

android root access dialog

android request root access dialog


  1. elviento0891 permalink

    This was a great help, I appreciate this post!

  2. hi~!
    ‘root access dialog’ is made by you?
    or, made by android system?
    if app requires root permissions
    then automatically show up ‘root access dialog’?
    IOException raised when write a file into a place we do not have permission(ex : /system/sd/)
    and app finished..
    not show ‘root access dialog’
    i want make app writing a file into /system/
    help me..

  3. The root access dialog is part of the android system, but its is only avaliable if you have “rooted” the phone it is running on otherwise permission is simply denied.

  4. Thanks,
    ‘rooted’ gets using shell command ‘mount’ ?

  5. No, I think your getting confused. Mount has nothing to do with this.

    A routed phone is one that has been hacked to allow root access. Your application will only work on phones that have been rooted by the user. This is obvious for security reasons.

  6. Rob permalink

    How can I use this to access another applications database?

  7. WeldFire permalink

    Hello Mat,
    You say you can use this method to “write a file into a place we do not have permission to access usually” do you have to try and copy files using os.writebytes()? or just a FileOutputStream?

  8. @WeldFire Sorry I missed your comment, I’ve been quite busy lately. I don’t know an incredible amount about this as I never really ended up using it, but I imagine there is a way to use FileOutputStream, maybe even if you just need to turn it into a byte array (which you could then use with os.writebytes())

    Deleted your comment on other post as I’ve answered here

  9. Rocker permalink

    Thank you Mat. I tried to modify the hosts file but not success.

    String cmd = String.format(“echo %s >> /etc/hosts\n”, entry);

    Do you have any idea?

  10. juwon permalink

    I have an huawei ascending 2 and it says I don’t have root access what app can I install dat. allows this. permission my email is contact me if u have any information

  11. Jonathan permalink

    Is there also a way to make the app detect when superuser permissions have been denied? Currently this code doesn’t seem to test that possibility.

  12. Nice info but how to make your jni (C Function) run as root?

    Have something like this in my code.

    Process p;
    try {
    // Preform su to get root privledges
    p = Runtime.getRuntime().exec(“su”);

    // Attempt to write a file to a root-only
    DataOutputStream os = new DataOutputStream( p.getOutputStream() );
    os.writeBytes( “echo \”Do I have root?\” > /data/temporary.txt\n” );

    // Close the terminal
    try {
    if (p.exitValue() != 255) {
    // Code to run on success
    tv.setText( stringFromJNI() ); // This function returns with permission denied error.
    else {
    // Code to run on unsuccessful
    tv.setText(“not root”);
    } catch (InterruptedException e) {
    // Code to run in interrupted exception
    tv.setText(“not root”);
    } catch (IOException e) {
    // Code to run in input/output exception
    tv.setText(“not root”);
    } // end SU block

    I have the file created in /data folder but I guess that echo runs in a child shell whereas my jni lib gets loaded an android app env. How to make sure that the jni code runs in super user mode as well?

    Thanks in advance.

  13. Nitish permalink

    how to use it frnds!!

  14. Eduardo permalink

    Hi BTR Naidu,

    Did you find out an answer to your question?



  15. Krishnamurthy permalink

    I am trying to run ubuntu-linux in andriiod but at command propmpt when i give “su” command the error message “permission denied” please help

  16. amir permalink

    can we acces “/data/data/com.whatsapp/databases/msgstore.db” file

  17. Frank permalink

    Great Post!

    I use it to change the rights to a file and then read/write in normal way:

    os.writeBytes(“chmod 777 ” + PROPERTY_FILE + “\n”);

  18. perfect, that’s what i need

  19. haftay permalink

    Great full code it successfully access the file .thank you

  20. Here is tutorial on creating root app using library:

  21. 09260803605

  22. Aungpingsoe123456789

  23. 2weettyggfuugfdstvyccyxf္​ပါငရာ ့လခအထခအ ာာလပပလပခf9ccoocococococtctictct

  24. David permalink

    I know this is an old post. But I am following the sample in my own app. Can you explain the result code 255? Is that some special error code if the su binary fails? In my simple tests, it seems that the result code I am getting is the result of the command run by su. These are generally 0 for success and non-zero (I am getting 1) for non-success. So I am confused by your treatment of any result != 255 as a success. Thanks for the post and your elaboration.

  25. This excellent website really has all the info I wanted about this subject
    and didn’t know who to ask.

Trackbacks & Pingbacks

  1. Diario de un androide » Como hacer que una aplicación solicite permisos de superusuario
  2. Android: howto request root access? « 神啊,我是个罪人
  3. get root permissions through Android SDK | Coding and Programing

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS