1
public void uninstallApp(String packageName){
       try {

                String[] command = new String[4];
                command[0] = "su";
                command[1] = "mount -o remount,rw /system";
                command[2] = "rm " + packageName;
                command[3] = "reboot";
                Runtime run = Runtime.getRuntime();
                run.exec(command);

                Log.d("DELETED", packageName);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.e("ERROR ON DELETE", e.getMessage());
    }
}

That's my code. This method receives an apk's path, what i need to do is execute those commands but without loosing permissions. I mean, if i execute "su" in one process and then "mount" in other process. "mount" won't have "su" access. So i need to execute all those commands in a single process. And that's not working. :\

12
  • Please indent your code properly. You seem to have a spurious } before the catch. Please make sure that your posted code is valid. Commented Mar 27, 2015 at 19:58
  • Can you define "not working" in this case? Commented Mar 27, 2015 at 20:07
  • The array you pass to exec() is not multiple commands, it's one command and it's command line parameters. Commented Mar 27, 2015 at 20:09
  • Oh i fixed the extra } . But it wasnt the problem. When i say "it doesnt work" i mean it doesnt execute anything. ci_ You said the array is not multiple commands. How should i execute multiple commands? Commented Mar 27, 2015 at 20:14
  • 1
    See also When Runtime.exec() won't for many good tips on creating and handling a process correctly. Then ignore it refers to exec and use a ProcessBuilder to create the process. Commented Mar 28, 2015 at 6:01

2 Answers 2

1

FOUND SOLUTION

StringBuilder cmdReturn = new StringBuilder();

try {
    ProcessBuilder processBuilder = new ProcessBuilder("su","-c mount -o remount,rw /system ; rm " + packageName + " ; reboot");
    Process process = processBuilder.start();

    InputStream inputStream = process.getInputStream();
    int c;
    while ((c = inputStream.read()) != -1) {
        cmdReturn.append((char) c);
    }

    Log.d("CMD RESPONSE", cmdReturn.toString());

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to use ProcessBuilder pattern. just add your commands to the String array in order:

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.