0

I need to execute a C program in my App by simply adding the executable to the android project and building the .apk. Then I try to execute the program in my application like this:

    Process result = Runtime.getRuntime().exec("su");
    String cmd = "PROGRAM_NAME";

    DataOutputStream dos = new DataOutputStream(result.getOutputStream());
    DataInputStreamdis = new DataInputStream(result.getInputStream());

    dos.writeBytes(cmd + "\n");
    dos.writeBytes("exit\n");
    dos.flush();

I know I need root access to do this so I installed Superuser.apk but that didn't work. Is there another possible way to do this? Btw the code is not fully extended it should just give a look at the way the program should be executed I'm running the emulator with Android 4.2.1

Edit: Checking root permission first with

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

         DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
         DataInputStream osRes = new DataInputStream(suProcess.getInputStream());

         if (null != os && null != osRes) {
             os.writeBytes("id\n");
             os.flush();
             String currUid = osRes.readLine();
             boolean exitSu = false;
             if (null == currUid) {
                 Log.d("ROOT", "Can't get root access or denied by user");
            }
wasp256
  • 5,943
  • 12
  • 72
  • 119
  • See the answers here: http://stackoverflow.com/questions/5095234/how-to-get-root-access-on-android-emulator?rq=1 – Erik Nedwidek May 18 '13 at 12:38
  • The use android version 2.2 thats why its working for them, I'm using 4.2.1 for which the Root.apk/Superuser.apk, think its the same anyway, is not working!? – wasp256 May 18 '13 at 12:40
  • Superuser.apk does not root the device. Try the instructions for the second answer and try remounting the /system filesystem as read/write and pushing the su command. – Erik Nedwidek May 18 '13 at 12:43
  • BTW I've just started my emulator. An adb shell puts you right in as root so you can do anything to the image. /system/xbin/su is the correct path. I don't think Process searches your PATH env variable. – Erik Nedwidek May 18 '13 at 12:51
  • I have tried the instructions but it didn't work, I could execute them without any errors but I still don't have root permissions (see edit of the question) – wasp256 May 18 '13 at 13:33

1 Answers1

0

I have got the same problem as you. you can find many answers like here , but they are too old and they are not working anymore (in the new sdk).

I found the best answer here which says that the Security Tips of Android are not allowing to any developer to have root access :

 A central design point of the Android security architecture is that no application, by default,
 has permission to perform any operations that would adversely impact other applications, the 
operating system, or the user. This includes reading or writing the user's private data (such as 
contacts or e-mails), reading or writing another application's files, performing network access, 
keeping the device awake, etc.

So the only access that you have under the Application Layer is by permissions.

Community
  • 1
  • 1
ahmed_khan_89
  • 2,755
  • 26
  • 49