Search code examples
androidmobilerooted-device

How can one create/ delete files in the /data/data dir using root in Android?


I'm working on a POC where I need to create and later on delete a file in the /data/data dir of a rooted device. I have tried to create a file in the standard way but it throws an PERMISSION_DENNIED exception as expected. I know this is possible because the Root Explorer app can do it.

How can I programatically create/ delete a file via root?

Thank in advance!


Solution

  • Based on @GabeSechan comment I was able to achieve this using this commands.

    Create new file:

    final String[] sCommand = {"su", "-c", "touch /data/..."};
            try {
                Runtime.getRuntime().exec(sCommand);
    
            } catch (Exception e) {
                Log.e(TAG, "Issue occurred while creating new file " + e.getMessage());
            }
        }
    

    And delete file:

    final String[] sCommand = {"su", "-c", "rm /data/..."};
    try {
        Runtime.getRuntime().exec(sCommand);
    
    } catch (Exception e) {
        Log.e(TAG, "Issue occurred while deleting " + e.getMessage());
    }