Screenshots with adb from script which is run form macOS application - android

I'm trying to take a screenshot on an Android-device/emulator, which should be triggered from within an macOS application.
I've created a bash-script - and it works well if I run it from the terminal.
However, when I run it from my macOS application, I get an error when I try to pull the image from the device.
ADB="$1"
DEVICE="$2"
PATH="$3"
FILENAME="$4"
$ADB -s "$DEVICE" shell screencap -p "/sdcard/${FILENAME}"
$ADB -s "$DEVICE" pull "/sdcard/${FILENAME}"
I get the following error:
adb: error: cannot create './EBC34F20-4624-4435-806B-15D844F4540C.png': Read-only file system
Why is it different from when I run it in the terminal? I've tried looking at which user is executing though adb and that all comes back with the same info.
I've removed sandbox and hardened runtime from my macOS app to to avoid it interfering.
It doesn't seem to matter if I change location (not /sdcard) on the emulator either.

I don't know what the actual problem is with the file-system on your device but I would simply avoid using the file-system at all and directly pipe the screenshot to stdout and via adb to your Mac.
Remove the following two lines:
$ADB -s "$DEVICE" shell screencap -p "/sdcard/${FILENAME}"
$ADB -s "$DEVICE" pull "/sdcard/${FILENAME}"
and replace them with this line:
$ADB -s "$DEVICE" exec-out screencap -p > ${FILENAME}
No adb pull and hence no problems with the file-system at all.
The command has been taken from the following answer: https://stackoverflow.com/a/31401447/150978

Related

nohangup using ADB shell

I am trying to do a logcat to a file using adb shell by following command -
adb shell "nohup logcat -f /storage/sdcard0/myLog.txt -v time &"
If I do a ps | grep logcat, I don't see the logcat command. Even I tried to see nohup command, but it is not there. So somehow above command does not work.
However if I perform the command in 2 steps it works fine -
adb shell
nohup logcat -f /storage/sdcard0/myLog.txt -v time &
I can see the process using ps and logcat continues to record to the file even if I disconnect adb shell. Now I would like the first command to work, since I am using python scripts to issue commands via ADB. It is possible to change the python scripts, however I would like to know if I am doing anything wrong in issuing the first command and if it is possible to make it work.
try
adb logcat
not
adb shell logcat

adb "error device not found" when trying to use 'adb shell' but "adb devices" detects the device

Guys basically I've plugged in a bunch of Android devices to a computer and each of them has adb enabled. I'm running a shell script to push certain files to the devices using the Device Serial Numbers stored in a text file. When I trigger the script the first time it works. if i trigger it again adb throws an error , "error device not found" though the devices are still detected using adb. If i close the terminal and reopen it and run the script it works fine. I plan to eventually automate the script running. How can i stop this from happening.
EDIT: This is the piece of code where the adb shell commands are triggered
cat device_serial | while read line
do
adb -s $line shell
cd /data/
ls
exit
adb -s $line root
adb -s $line push 'stestfile.txt ' /data/
echo "done"
done
I need to look at the script to fix the issue.
But looks like the problem is that the next time you run the script, you are actually in the device's shell instead of your computer's.

Copying files in ADB shell with run-as

Is there a way to write a script that will copy files from an ADB shell using run-as?
The only way I know of to copy in the adb shell is using cat source > dest (edit: modern android versions have the cp command, which makes this question unnecessary), but I am only able to quote the greater-than sign one level deep - so my script can pass it to adb shell, but not to adb shell run-as.
For example, this works:
adb shell "cat source > dest"
But this does not:
adb shell run-as "cat source > dest"
Nor this:
adb shell "run-as cat source \> dest"
I even tried created a small script and uploading it to the device, but I can't seem to run the script from the adb shell - it tells me "permission denied". I can't chmod the script, either.
The reason I want to do this is to copy a file into an app's private storage area - specifically, I am using a script to modify shared preferences and put the modified preferences back. Only the app itself or root can write to the file I want, however.
The use case in this scenario is coping a file to a protected location on the device, not retrieving it; for retrieving, there are already good answers in this question.
The OP tried to combine the following 3 commands (that he had no problem executing one after another in the interactive shell session) into a single non-interactive command:
adb shell
run-as com.example.app
cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml
For simplicity let's start from within an interactive adb shell session. If we just try to combine the last two commands into a single line:
run-as com.example.app cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml
This would not work because of how shell redirection works - only the cat /sdcard/temp_prefs.xml part of the command would be run with com.example.app UID
Many people "know" to put the part of the command around redirection into quotes:
run-as com.example.app "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"
This does not work because the run-as command is not smart enough to parse the whole command. It expects an executable as the next parameter. The proper way to do it would be to use sh instead:
run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"
So can we just prepend adb shell to the command and be done with it? Not necessarily. By running the command from your PC you also add another local shell and its parser. Specific escape requirements would depend on your OS. In Linux or OSX (if your command does not already contain any ') it is easy to single-quote the whole command like so:
adb shell 'run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"'
But sometimes it is just easier to use an alternative solutions with (-out or less) quotes:
adb shell run-as com.example.app cp /sdcard/temp_prefs.xml shared_prefs/com.example.app_preferences.xml
Or if your device does not have the cp command:
adb shell run-as com.example.app dd if=/sdcard/temp_prefs.xml of=shared_prefs/com.example.app_preferences.xml
Also notice how I used shared_prefs/com.example.app_preferences.xml instead of full /data/data/com.example.app/shared_prefs/com.example.app_preferences.xml - normally inside of run-as command your current directory is the HOME dir of your package.
Following Chris Stratton's advice, the way I eventually got this to work was as follows (for copying shared preferences back to the device):
adb push shared_prefs.xml /sdcard/temp_prefs.xml
cat <<EOF | adb shell
run-as com.example.app
cat /sdcard/temp_prefs.xml > /data/data/com.example.app/shared_prefs/com.example.app_preferences.xml
exit
exit
EOF
Piping directly to adb shell run-as did not work, and I do not know why, but piping to adb shell does. The trick is to then call run-as from the interactive shell, and it continues to accept input from the pipe.
The HERE doc lets me easily embed the newlines to separate commands and in general just makes it readable; I did not have much luck with semicolons, but that might have been because of the way I was doing things. I believe it might work with other methods of piping multiple commands/newlines; I stopped the experiment once I finally got it to work.
The two exits are necessary to prevent a hanging shell (killable with CTRL-C); one for run-as, and the other for adb shell itself. Adb's shell doesn't respond to end-of-file very nicely, it seems.
you could just change the permission of the directory and then pull all the files out. but for me i was looking for just one shared preference file and i was able to get the data like this:
PACKAGE='com.mypackage.cool'
SHAREDPREF_FILE="${PACKAGE}_preferences.xml"
adb shell "run-as $PACKAGE cat /data/data/$PACKAGE/shared_prefs/$SHAREDPREF_FILE">$SHAREDPREF_FILE
now we have the data of the sharedpreference file stored in a file of the same name.
Using the latest adb (ADB v1.0.41 / Version 33.0.3) and a Play Store emulator image I experienced adb root not being granted. I also could not copy from /data/local/ or /storage/emulated/0/ due to not having permissions when run-as com.myapp.app
new_prefs_path="my_machine.xml"
config="$(cat $new_prefs_path)"
my_app_uri="com.myapp.app"
adb shell "run-as $my_app_uri sh -c 'echo \"$config\" > shared_prefs/on_android.xml'"
This fixes it for me as a bash script. It's made slightly more complicated by needing to be configurable for different apps and complex payloads.
We take a file (could be generated earlier in this script) and read it to a variable.
We then start shell, do run-as my app and run echo expanding the read file to a file in shared_prefs.

Start a process in background from adb shell without attaching the process to the terminal in Android

I have a simple problem.
I want to start/run a program file on an android phone using adb shell.
My Program is in /system/bin folder and has root privileges.
I want to run this program from my command prompt as adb shell runme 3000000 > logs.txt but it should not block the terminal, It should run in background.
I cannot use screen/disown/nohup for my problem as android doesn't have all this.
I tried
adb shell "runme >logs.txt &" but of no use.
When i issue command as
adb shell
# runme 3000000 > logs.txt &
It runs fine, when i exit the terminal/disconnect the device and then connect again to system.
Do adb shell ps | grep runme shows the process is still runnning in background.
Thanks
Busybox has nohup applet which works just fine in Android

Run sqlite3 command from batch file?

I want to make it easier to access the sqlite databases on the emulator, so I've created a batch file that successfully runs the first line, but is there a way to run the second line from the batch file? (At this point adb is already running, so it would be running a command in the shell.)
adb -s emulator-5554 shell
# sqlite3 /data/data/com.myProject/databases/myDatabase
UPDATE:
For some reason
adb -s emulator-5554 shell sqlite3 /data/data/com.myProject/databases/myDatabase
prevents you from using the command line. Typing and pasting both seemed to be blocked. I ran this line in a command line outside of the batch and it does the same thing.
Separate lines work fine:
adb -s emulator-5554 shell
sqlite3 /data/data/com.myProject/databases/myDatabase
but this doesn't work in the batch file (the sqlite3 command gets executed too early).
You can run specific commands other than just an interactive shell on the target device. eg:
adb -s emulator-5554 shell ls /sdcard
or
adb -s emulator-5554 shell sqlite3 /data/data/com..../database.db
Once it gets complicated, it might be better to use a script that you copy over onto the device and run on demand.
You can use input redirection to send a stream of commands to ADB:
adb -s emulator-5554 shell <a.txt
Where a.txt contains
sqlite3 /data/data/com.myProject/databases/myDatabase
logout
This scales better. a.txt can grow as big as you want.

Categories

Resources