My phone runs rooted 2.3.6 Gingerbread. I write these lines to take a screenshot:
adb shell screencap -p /sdcard/screen.png
It says permission denied! So I add su, like this:
adb shell "su -c 'screencap -p /sdcard/screen.png'"
Now when I run it, it says "screenshot: not found!"
You could accomplish this with MonkeyRunner instead of ADB by creating a screenshot.py file with the following content:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection()
result = device.takeSnapshot()
result.writeToFile('<some path>/screenshot.png','png')
and run it with the command (on Windows)
<android sdk path>\tools\monkeyrunner.bat <some path>\screenshot.py
Related
Below command is to pull a file:
adb -d shell "run-as com.myapp cat /data/data/com.myapp/databases/file.db" > file.db
But how to push it back like Android Studio does via Device File Explorer?
There is no simple command for uploading the file. What Android Studio does when uploading a file using Device File Explorer is this:
Upload the file via adb push to /data/local/tmp/<random file name>
Execute adb shell run-as com.myapp sh -c 'cp /data/local/tmp/<random file name> /data/data/com.myapp/<path>/<final file-name>'
Delete the temp file via adb shell rm /data/local/tmp/<random file name>
Get the updated view for Device File Explorer using adb shell run-as com.myapp sh -c 'ls -al /data/data/com.myapp/<path>/'
I discovered this by capturing the adb traffic on TCP port 5027 using Wireshark. An interesting detail is that each command executed using adb shell command uses the form <command-to-be executed in adb shell> || echo ERR-ERR-ERR-ERR
From Robert's answer now I can do like this:
function dbpull() {
adb shell run-as "com.$1.debug" cat "/data/data/com.$1.debug/databases/$2.db" > "/Users/username/Desktop/$2.db"
}
function dbpush() {
adb push "/Users/username/Desktop/$1.db" "/sdcard/db/tmp/"
}
function dbpush2() {
adb shell run-as "com.$1.debug" cp "/sdcard/db/tmp/$2.db" "/data/data/com.$1.debug/databases/$2.db"
}
function dbcheck() {
adb shell run-as "com.$1.debug" ls -al "/data/data/com.$1.debug/databases/"
}
Just write above code lines in your .bash_profile and then call it in terminal.
dbpull myapp mydata
At this moment I prefer to use Visual Studio Code than Android Studio to develop my Android apps. So, I need to know more about commands in terminal, e.g. adb, gradle, etc.
I hope this would be useful for everyone.
Use this command
adb push <file_path> <android_device_path>
adb pull <android_device_path>
I am trying to access adb shell of an android device and pass multiple commands such as
adb root
adb shell
cd /data/local/tmp
export PATH =/data/local/tmp:$PATH
export PATH =/data/local/tmp/testcases:$PATH
sh ./runltp -p -l 1213reportipc.log -d /data/local/tmp/LTP13/tmp -f ipc1q
exit
While I am able to do this manually using adb shell, I am unable to do this via a Python script since as soon script execution encounters 'adb shell' command it spawns a new internal shell and rest of the commands are not executed.
Can anyone please help me with this.
Use a text editor such as sublime or something, write your full adb script using subprocess to send commands and run it while your laptop is connected to your phone and USB debugging is switched on.
import subprocess
subprocess.call("adb shell <your command here>", shell = True)
subprocess.call("adb shell tap 172 3241", shell = True) -- example
etc, just put your commands after the 'adb shell' and run the python script. This should type all your commands into the adb shell. You should even be able to enter commands such as
subprocess.call("adb kill-server", shell=True)
or subprocess.call("adb root", shell=True)
I'm trying to execute command to adb shell from python
I'm using Windows OS
On CMD :
C:\Users\deounix>adb shell
shell#android:/ $ su root
su root
root#android:/ # reboot
What I'm doing is :
import os
os.popen("adb shell & su root & reboot")
But it isn't work
How can I do it?
import os
os.system("adb shell su -c reboot")
import os
os.system("c:/platform-tools/adb devices")
To run any command like adb devices on python execute the above command
This will work definitely. You just need to confirm the path where you have got the adb shell
$adb push ./asl-native /data/local/asl-native
$adb shell /system/bin/chmod 0777 /data/local/asl-native
above two lines can be executed in unrooted android device using Runtime.getRuntime().exec() command in program.
Is there any way to run the below command in unrooted device.
(Note:This command will run correctly in rooted device)
$adb shell "/data/local/asl-native /data/local/asl-native.log" &
if it is possible using jni or ndk,please explain the steps.
Reference: From ASL(Android Screenshot Library)
You can Try this:(01234ABC is your device)
1) adb -s 01234ABC shell
2) shell#android:/ $ run-as com.nitesh.tiwari
Hope this could help :)
One problem with ADB is that you need multiple commands to get things done.
For example:
adb shell
su
cp /data/local/x /data/local/y
exit
adb pull /data/local/y
Can this be done using python popen and os-system? Tried the example below without success..
print 'Starting emulator...'
subprocess.Popen(['emulator', '-avd', 'testavd'])
os.system('adb wait-for-device')
os.system('Perform whatever adb commands you need')
Any pointers?
You can simply do:
adb shell su -c cp /data/local/x /data/local/y
adb pull /data/local/y
or, if you want to run more than one command (only Linux & OSX):
adb shell <<EOF
ls
date
cat /proc/version
exit
EOF