Perhaps this is due to the fact that you need to pass the su commands as such parameters:
su -c 'ls -l'
Or you may need to specify the full path to su , but I donβt understand why it will not work the way you use it: Runtime.exec("/system/bin/su -c ps")
Or maybe Runtime.exec("/system/bin/su -c \'ps\'")
Try also checking the output of this command: System.getenv("PATH")
Another option might be Runtime.exec("su -c \'ls -s \'")
Make sure you remember to avoid single quotes, as they are part of the actual String .
What I found works the most consistently, and it also works on devices that don't have Superuser or SuperSU installed, because these applications only listen to the broadcast that is sent when the application tries to run the command as root. @Boardy SuperSU and Superuser intercept the broadcast and therefore act as the average person between the privileges of the application and root, but this is not necessary for the root device. This is necessary if you want to have more control over applications in which commands are executed as root, but even then it still limits you only to which applications, and not which commands, get root privileges. Alternatively, you can take a look at RootTools and, more specifically, RootTools.isAccessGiven() , which asks for root privileges for your application.
Source: Run script as root via ADB
source share