4

My phone is rooted. I'm trying to do a very simple program. The program should delete file from app/app folder. How can I do this? I'm newbie, so example code is valued.

eagerToLearn
  • 41
  • 1
  • 3

3 Answers3

3

If your phone is rooted, you can issue commands as root through su—provided that the su binary is present and in your PATH—since Android is a variant of Linux. Simply execute the delete commands through Runtime.exec(), and Superuser should take care of the permission prompt.

Here's a simple example of its usage I took from this question:

process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
1

You can delete all files inside a folder recursively using the below method.

private void DeleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        for (File child : fileOrDirectory.listFiles())
        {
            child.delete();
            DeleteRecursive(child);
        }

    fileOrDirectory.delete();
}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1

On his github, Chainfire provides a sample implementation of a Shell class that you can use to execute the rm command as root. The rm command is the Linux variant of the command to delete files (and folders).

Code Snippet:

if(Shell.SU.available()){
   Shell.SU.run("rm /data/app/app.folder.here/fileToDelete.xml"); //Delete command
else{
   System.out.println("su not found");

Or if you are certain that the su binary is available, you can just run the delete command (commented line) and skip the check

Source: How-To SU

cnexus
  • 454
  • 1
  • 3
  • 14