Using Python or adb command to read Android UI text? - android

I was just wondering if there was a way to read the text displayed on a Android device's UI using python or any adb commands?
I have found a module for python called "UIAutomator : https://github.com/xiaocong/uiautomator", which can be used to dump the screen's contents, but it seems to be really slow and often unreliable in terms of connection to the android device.
I also found google's own UIAutomator tool, but I needed something that can be used via a python script or an adb command to read the onscreen text.
Any help would be appreciated.
Thanks in advance :)

I have done a bit of research and found out two ways to read the UI's contents.
Using AndroidViewClient's dump tool.
This is their project page : https://github.com/dtmilano/AndroidViewClient
This tutorial helped me set up AndroidViewClient : https://www.darpandodiya.com/code/setup-androidviewclient-windows/
Using this adb command :
This command saves the UI Heirarchy XML in the device's sdcard.
adb exec-out uiautomator dump
This gives the result in the console itself. adb exec-out uiautomator dump /dev/tty

Related

Saving multiple screenshots with adb

I can save screenshots from an Android using adb exec-out screencap -p > ~/Desktop/screen.png. My problem with this is that it overwrites the last saved image when I take a new one. How can we save multiple images so that it saves the newest image with a number, or something, like screen-2.png?
I've looked around and am not seeing any answered questions on this particular issue, but if I missed it, please point me in that direction.
Thank you.
If you don't want to create a script as recommended in the comments, then
adb exec-out screencap -p > $(mktemp -t screen)
would be the closest using random names.

pull only new files (photos) from Android adb to linux os

I've been pulling photos from my android device to my linux OS like this:
$ adb pull <what-to-pull> <where-to-place>
In the future I would prefer to pull only the ones I don't alreay have.
What's the best way to do this?
Maybe I could put all the photos I've downloaded to the same folder and skip the ones with names that already exist in the folder I'm pulling from? How to do that?
Is that even the best way? Does an easier way to do this exist?
If so... how?
I'm on arch linux by the way, in case the distribution effects your suggested answer.
adb shell find "/sdcard/DCIM" -iname "*.jpg" | tr -d '\015' | while read line; do adb pull $line; done;
^that works well enough.
From here.
The adb-sync tool worked for me: https://github.com/google/adb-sync
Note that I had to make several changes to the source code to get it working for my use-case (invalid paths for Windows causing crash, Python version mismatch apparently, etc -- for details, see issues I commented in), but it ended up being the only way I was able to retrieve my files from a corrupted data partition.
(The adb pull of the whole directory would crash on various files, and I didn't want to manually have to delete each one then restart the whole transfer. With adb-sync [+my modifications] it would just fail that one file then continue.)
Regarding your question of having it only transfer new files, I believe adb-sync does that automatically if a file hasn't been changed. If you don't want it to re-transfer an existent file ever (ie. even if the file has been updated), I think that's what the flag mentioned here is for: https://github.com/google/adb-sync/issues/22

How to change ADB Device Names during build process

I am currently working with multiple android builds on different hardware. I am having an issue where they all have the same serial number, 0123456789ABCDEF. This makes it impossible to use adb when I connected to two or more devices at once, because the adb doesn't know which one to talk to.
I know that the name is being pulled from /sys/class/android_usb/android0/iSerial and if I wanted to I could change it there once the build is complete. Ideally though, I want that file to be set during the build depending on the build settings. I want to know where that file is being generated during the build. I believe it's being set either somewhere in barebox or in /system/core/adb, but have had no luck on the things I've tried editing.
If anyone has ran into this, any help would be greatly appreciated.
Edit:
Found the solution.
This can be found in /device/company_name/device_name/init.device_name.usb.rc
on boot
write /sys/class/android_usb/android0/iManufacturer ${ro.product.manufacturer}
write /sys/class/android_usb/android0/iProduct ${ro.product.model}
write /sys/class/android_usb/android0/iSerial ${ro.serialno}
echo "ro.serialno is ${ro.serialno}"
write /sys/class/android_usb/android0/idVendor 0451
write /sys/class/android_usb/android0/idProduct D101
..."
Change the ro.serialno to whatever you'd like.

Can I bundle a binary utility and call it from app?

Is it possible to create a app and call a command-line utility from it (bundled with the app), like it could be with a desktop application in linux?
Example:
My app wants to load some files but needs them to be converted, so first it calls an utility with command-line (like "jpgconv -r -t image.png") to create converted copies. Is it technically possible on Android?
What you are trying to do is basically call the Android shell from your application and pass it a binary.
Android's shell doesn't seem to accept Intents, in which case it's not possible, and it's anyway severely limited, which is why most users who need a shell (which is a very small subset of Android users) install a separate shell application, as well as tune their filesystem appropriately.
You could however try to embed your binary using the NDK, since, for exemple, BusyBox is straight C.

Pull a file from android emulator/device programmatically

I am familiar with adb pull command. Is it possible to pull a file from android device/emulator and saving it in specified location in PC programmatically?
In C, on linux use system() to execute adb (perhaps by its full path) with the appropriate arguments. OSX is probably something quite similar.
On windows... CreateProcess() but that returns before the child is finished, so you may have to spend some time wandering around MSDN to figure out the equivelent of system()

Categories

Resources