40

Can anyone say, whether adb commands can be executed through my android application. If it is possible to execute, how it can be implemented?

Alex P.
  • 30,437
  • 17
  • 118
  • 169
RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84
  • did you find a way to run adb on the device? I only get the message: error: device not found when running adb direct on the device – wutzebaer Jan 04 '16 at 15:26

7 Answers7

32

You can do it with this:

Process process = Runtime.getRuntime().exec("your command");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));

Don't forget to surround it with a try and catch statement.

Edit:

@Phix is right, ProcessBuilder would be better to use.

Terril Thomas
  • 1,486
  • 13
  • 32
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 4
    `Runtime.getRuntime()` has been set aside. http://stackoverflow.com/questions/7069425/is-there-an-alternative-to-runtime-getruntime-exec – Phix Nov 14 '12 at 15:43
  • 1
    Will this be able to execute the adb commands even if the device is not connected via USB? – Green goblin May 08 '14 at 13:23
  • 3
    I'm trying to run "adb shell input keyevent KEYCODE_BACK" but it doesn't work. Please can you help me? Thank you – user2430771 Sep 16 '14 at 20:09
  • you can directly pass the keycode to command like : adb shell input keyevent 26 (power key) – Deepak Apr 15 '15 at 09:41
  • this is working on emulator (geny motion) , but not on real device. what to do ? – Amarjit Jul 31 '15 at 07:52
  • ProcessBuilder proc = new ProcessBuilder("adb pull /storage/sdcard/s.txt C:/Users/401896/Desktop/adbPush.txt"); Process p = proc.start(); I have tried this and its throwing error. Can any one help – Sidharth Dash Aug 12 '15 at 14:16
  • This is not working for me, getting : Error running exec(). Command: [adb, shell, pm, clear, com.package.project Any help? – Heitor Colangelo Sep 08 '16 at 21:53
  • 2
    @HeitorColangelo sorry man - been a while since I did that, don't really remember anything about it now :/ – Ahmad Sep 09 '16 at 04:34
  • 1
    I am getting Cannot run program "": error=13, Permission denied :( – Ashish Jain Apr 22 '19 at 13:30
  • This is working on emulator but not working on real device. may be needed root devices. – Azahar Feb 22 '20 at 09:50
15

Normal Android apps have different privileges to processes started via adb, e.g., processes started via adb are allowed to the capture the screen whereas normal apps aren't. So, you can execute commands from your app via Runtime.getRuntime().exec(), but they won't have the same privileges as if you had executed from an adb shell.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • 3
    could you tell what different privileges you have? any source? – Anthea Jan 21 '16 at 10:29
  • I have this problem @Anthea, so anecdotal? – Allison Jun 07 '16 at 01:06
  • Very simple, if Android allowed access to ADB as the `shell` user from any, random Android app it would be a big security flaw. The `shell` user has full access to the ADB features, it's meant for device debugging and can be acessed only from USB debugging. – hldev Jan 22 '23 at 03:37
5

i came across this post looking for a different query, but i have worked specifically with input on android before, so I'd just like to put some clarity on the matter.

The reason why

Runtime.getRuntime().exec("adb shell input keyevent 120");    

Is not working, is because you are not removing

adb shell   

The ADB part is only for use on your computer, if you have incorrectly installed ADB, the command would actually be a path to the adb.exe file on your computer, like this

C:\XXXX\ADB Files\adb.exe shell    

or
C:\XXXX\ADB Files\adb shell

The shell part tells the ADB program on your computer to access the devices shell, so your device will not know what shell is either...

Using sh /path/to/commandList.sh will execute the commads listed in commandList.sh as it is a shell script (a .batch file on windows is similar )

The command you want to use is

Runtime.getRuntime().exec("input keyevent 120");     

However this will cause Environment null and working directory null, you can bypass this by writing the commands to a shell script ( .sh file ) and then running the script with

Runtime.getRuntime().exec("sh path/to/shellScript.sh");   

Sometimes the sh is not needed, but i use it just incase.

I hope this clears at least something up :)

Empire of E
  • 588
  • 6
  • 12
  • 1
    Hi, It runs without adb shell, but not all commands work. Simple commands like swipe, keyevent do not show any effect however they work with adb shell from a system. Any idea ? – viv Mar 20 '19 at 08:26
1

adb shell invoked in Runtime.getRuntime().exec is not running under shell user. It provide shell but with same process owner user (like u0_a44). That's the reason all command did not work.

1

This is what I do in Kotlin, I also get command responses too

fun runShellCommand(command: String) {
    // Run the command
    val process = Runtime.getRuntime().exec(command)
    val bufferedReader = BufferedReader(
        InputStreamReader(process.inputStream)
    )

    // Grab the results
    val log = StringBuilder()
    var line: String?
    line = bufferedReader.readLine()
    while (line != null) {
        log.append(line + "\n")
        line = bufferedReader.readLine()
    }
    val Reader = BufferedReader(
        InputStreamReader(process.errorStream)
    )

    // if we had an error during ex we get here
    val error_log = StringBuilder()
    var error_line: String?
    error_line = Reader.readLine()
    while (error_line != null) {
        error_log.append(error_line + "\n")
        error_line = Reader.readLine()
    }
    if (error_log.toString() != "")
        Log.info("ADB_COMMAND", "command : $command $log error $error_log")
    else
        Log.info("ADB_COMMAND", "command : $command $log")
}

nima moradi
  • 2,300
  • 21
  • 34
0

Executing

Runtime.getRuntime().exec("adb shell input keyevent 120");

I got the following error: java.io.IOException: Cannot run program "adb": error=13, Permission denied.

Executing

Runtime.getRuntime().exec("adb shell input keyevent 120");

There is no error but at the same time, my request is not processed to take the screenshot.

I found out this was working in earlier versions of android but later it was removed. Though I'm not able to provide the source here why it is not working.

Hope this helps someone like me who is trying to use this approach to take the screenshot when the app is not in the foreground.

Siva
  • 1,078
  • 4
  • 18
  • 36
  • 1
    If you want to use this tool do not forget to add permission into your android manifest automatically `android.permission.DUMP` – Hardik Vasani May 05 '18 at 10:02
0
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
string cmd = "/system/bin/input keyevent 23\n";
os.writeBytes(cmd);

the phone must be rooted. here I have executed adb command "input keyevent 23". remember when you execute adb command through su you does not need to add "adb shell input keyevent 23"