Launching an Android Emulator from Python-Django - android

def start_test(request):
os.system('echo Starting emulator...')
os.system('./android-sdk-linux_x86/tools/emulator -avd testavd &')
return HttpResponse("OK")
Here is the barebones code of what I am trying to do.
When this code gets executed, the server stops responding while running the emulator. Any help appreciated.
I am using the django development server. Here is the server output:
Django version 1.1.1, using settings 'Cloust.settings'
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Starting emulator...
[21/Apr/2011 02:00:06] "GET /start_test/a.apk/ HTTP/1.1" 200 5
emulator: warning: opening audio output failed
emulator: emulator window was out of view and was recentred

Maybe you should try to run emulator in separate thread?
E.g.
import subprocess
thread = threading.Thread(target=subprocess.popen(['./android-sdk-linux_x86/tools/emulator', '-avd', 'testavd', '&'])
thread.start()

Considering you are using django you will probably need to manage the emulators somehow.
Threading is not really a good option in this case I think.
I'd suggest looking into task management in this case with something like http://code.google.com/p/django-tasks/

Don't know if it will help so one (hope it does).
I wanted the emulator to open before automation testing starts and for some reson appium can't do it.
in my case I needed to add the full path of the emulator.
check_output(["/Users/{USER_NAME}/Library/Android/sdk/tools/emulator", "-avd", "Pixel_API_26"])
Hope it will help someone until appium will fix this problem.

I still haven't gotten around to properly solving this problem, but using subprocess.Popen allows me to perform commands on the emulator afterwards:
print 'Starting emulator...'
subprocess.Popen(['emulator', '-avd', 'testavd'])
os.system('adb wait-for-device')
os.system('Perform whatever adb commands you need')
It's worth noting that this is using the django development server, which has been started using sudo, so obviously this is far from ideal.

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')

Related

"Wait for Debugger" Android Studio

I am using a guide to Decompile and Debug an APK but I can not pass the last step. Where when trying to Debug APK on my phone from Android Studio, an error appears: "Wait for Debugger". According to the guide I must execute the code:
adb forward tcp:5005 jdwp:$(timeout 0.5 adb jdwp | tail -n 1)
But since I do not have Linux (I have windows), I do not know what code I should execute. Thank you very much for your help!
Guide: https://malacupa.com/2018/11/11/debug-decompiled-smali-code-in-android-studio-3.2.html
First make sure you have adb in your path.
Then open a cmd.exe and run this command:
adb jdwp
and take note where application’s debug interface is listening. I'll call this value jdwp-port from now on.
Now execute the following command:
adb forward tcp:5005 jdwp:jdwp-port
and you should be fine. REMEMBER to change jdwp-port with the value you got from the first command.
Good luck.
But since I do not have Linus (I have windows)
It is actually called Linux anyway.
As a side note, you should check the official AOSP documentation on how to use GDB here: https://source.android.com/devices/tech/debug/gdb

"adb pull" stuck in half way

Suddenly my "adb pull" command will stuck in the middle of the process.
I'm not sure what cause it, after install some application or driver.
Happens for Pull one files or multiple files.
Does anyone encounter the same issue before? thanks.
Example:
U:\batch>adb pull /sdcard/xxxlog/mobilelog .\xxxlog\
[ 94%] /sdcard/xxxlog/mobilelog/APLog_xxx/main_log_1__xxx: 87%
Here a screenshot from my console
for my specific answer, and provide a possible answer to who encounter similar problem.
after I reinstall the whole os, testing different adb version,
the issue still remains.
during testing adb version,
I found the issue will not happens if i use Local Disk...
where issue happens when using Network Drive..
So a possible solution, is don't use Network drive
I encountered similar problem on adb server (v 1.0.40) started on Windows 7 machine. When tried to pull files from device on other machine running Linux in same local network (I used: "adb -H pull ...") the adb was freezing occasionally.
The solution was to not use Windows for ADB server.
This problem was NOT visible, when adb server was running on Linux VM (Ubuntu 16.4/ VirtualBox). Hope that this helps.
BR,
Ziggy
Every time I encounter this, it ends up being a max path length issue. Open a shell on the device using adb and review the file names within the directories that you are pulling. If any have exceedingly long names, they will silently fail and adb will hang
The thing that really throws you for a loop is that it usually fails after giving a percentage complete which makes you think it's a faulty connection or some other issue.
I haven't found a good way to recursively list out file names in shell and test their name length prior to doing the pull in order to know that the issue is going to happen, but when I have the same issue and rename long files, it ends up working on the next attempt.
I encountered the same problem when trying to transfer a large amount of data from an Android phone to a Raspberry Pi 3B+, and the logcat output seemed to reveal that adb was silently failing due to an issue with USB buffer reads (unfortunately, I don't have the exact message with me).
After the initial failure, it was possible to get another few files individually by unplugging the phone, running adb kill-server and adb start-server, and plugging it back in between each one, but the only longer-term fix I could find was restarting the Raspberry Pi. This solution, however, is not permanent, and must be repeated occasionally.
I was not able to replicate this issue on Windows or on a traditional Ubuntu system.
I encountered this problem while copying files from My Ubuntu machine to my Pixel 7.
A workaround based on this bug report, is to run
adb shell exit
in another terminal whenever it gets stuck until all files are copied. You can also run
watch -n 30 adb shell exit
to run the command every 30 seconds so you don't have to keep watching the process. I hope this helps someone.

See app background processes in Android Studio

Maybe I'm just missing it but is there a way to view WHAT is running in the background on-device in android studio?
I'm getting a battery usage alert on my phone (galaxy s8 - OS v8.0), indicating my app is doing something in the background and I'd like to see what it is.
Thank you
Yea, via the terminal function. This is one of options at the bottom of the Android studio.
You need to run ADB to connect to the device. Once connected you can use Unix command lines to see the processes running.
From google dev : https://developer.android.com/studio/command-line/adb
Also, the command I use to connect an emulator to run unix commands is:
adb -s <DEVICE> shell
Normally the ADB is stored :
USER\AppData\Local\Android\Sdk\platform-tools
and then once connected you can use the following to see the running processes :
https://www.howtogeek.com/107217/how-to-manage-processes-from-the-linux-terminal-10-commands-you-need-to-know/
Hope this helps.

How to run Android emulator remotely

I have a android application that connects/send/receive to/from a server as part of its operation.
my final goal is to run this application on a number of android emulators on a remote machine(to save some computing resources on my laptop).
I SSHed to the remote machine and created the emulators remotely using android create avd -n AVD_xxx -t 1 .
I tried to run the emulators using emulator-arm -avd AVD_1 but it gave error that looks obvious : SDL init failure, reason is: No available video device
(if I run the command one the michine directly, it will run just fine)
I will appreciate if you help me solve this issue.
Note:
I don't need video provisions. is it possible to disable that by configuring AVD? this is just an example. you might have better solutions.
There are two options:
export $DISPLAY and show the emulator on some X display (tunneled or remotely existing)
run emulator -no-window [-no-audio] to start it without the need of a X display

Where does Android store shutdown logs?

I know that the boot up log can be obtained by pulling out contents of kmsg or dmesg through ADB.
But I'm not aware of how to retrieve the shutdown logs in Android as there's no /var folder in Android (place where most desktop linux distros generally store their shutdown logs).
So how can I obtain the shutdown logs in Android?
Look in some locations such as these:
/proc/last_kmsg
/data/tombstones/
/data/dontpanic/
/data/system/dropbox/
(This list isn't strictly kernel logs, including framework and application logs too, which are also sometimes of interest)
One work around I found for collecting shutdown logs in Android is to run adb pull /proc/kmsg C:\Logs.txt on the host PC and then switch off the device. You will get the logs till the USB communication between the host and the device snaps! I know this is only one case out of the numerous shutdown scenarios but I haven't found satisfactory answers for other cases!
TL;DR:
Run command through adb that copies logcat and proc/kmsg to a file and keep it running even when adb disconnects with nohup, disown or setsid. Probably needs busybox, needs root and adb root, too.
setsid cat proc/kmsg > /sdcard/kmsg.txt &
and
logcat -v long -f /sdcard/logcat.txt (somehow only works without setsid)
Or add normal copy commands to some startup script.
/TL;DR
You can constantly copy proc/kmsg and logcat to a file on your android device or a microSD card to get the logs even after adb disconnects.
You need root access and adb root access for this to work. For the latter, use the setting in the developer options if you have a custom rom or the adbd insecure app.
After using adb shell to get your android shell, type su to get superuser access.
Then you not only need to put an ampersand (&) after the command but also make sure that the command keeps running after adb disconnects. That is done by nohup, disown or setsid (see here for usage).
If it doesn't work because you don't have these commands, you need to install busybox.
See my question here.
See here for how to get logcat and kernel logs and print it to some file or merge it.
See developer.android.com/tools/help/logcat.html for parameters for the logcat command.
In the end you could have a command like setsid cat proc/kmsg > /sdcard/kmsg.txt & for the kernel messages.
For logcat you could have one of the following commands: logcat -v long -f /sdcard/logcat.txt or logcat -v long > /sdcard/logcat.txt
I don't know why, but sometimes it didn't work with setsid and just didn't copy continuously but stopped shortly after executing the command. In these situations, it also showed up when entering jobs, which it didn't otherwise. Then it just worked without setsid, it stayed alive after disconnecting and reconnecting. I guess you must just try when the file does keep getting larger. If someone figured out why it is behaving like it is... let me know and I'll edit the answer.
Probably adding the commands to a startup script could be a solution for some, too.
Hope this helps.
fightcookie
Newer phones do NOT use any of these locations so if you're reading this article then as of now
The kernel crash logs are now in /sys/fs/pstore instead of /proc/last_kmsg
I was looking for the same thing, and finally, I found the answer!
In android 8 all logs are located in \data\log\android_logs\... including apps and kernel logs. Kernel logs are called kmsgcat-log_timestamp_.gz
edit: Although this is a very old thread, I think the answer might be helpful.

Categories

Resources