Let’s face it we all have been there, trying to fiddle with managing our emulators and dev devices. And at max, we would do is try to manually manage it. Let me make this clear I am not talking about tests here as that’s all related to the code. My focus here is on managing the device/emulator that’s paired with your dev environment/system (..and to your IDE — Android Studio, if you still use Eclipse you are simply oblivious to the joys of coding in Android [here is a long blank stare from me to you] ).
Technically there is a tool called ADB (Android Debug Bridge) which maintains your connection with your device/emulator and the dev environment/system.
From the wikipedia, it is described as
adb is the command line tool provided in the Android SDK package. adb stands for Android Debug Bridge which allows you to communicate with an emulator instance or connected Android-powered device to:
1. Manage the state of an emulator or device.2. Run shell commands on a device.3. Manage port forwarding on an emulator or device.4. Copy files to/from an emulator or device.
Ok… that sounds something of use but not convincing enough for you to try and play with it yet. Let me try and put forward some of the problems as a dev you are facing every day (..and yet no one admits to them whenever it comes up in a conversation 🙄..) and how can we solve this.
Oh yeah ..you are used to hitting Run in the IDE (or the keyboard shortcut for the same) and BOOM the apk installs in the device. But what do you do to uninstall the apk? [Rolling eyes] oh yeah I know ..you will open up your device/emulator and manually uninstall the app by dragging it to the trash bin in the home screen and hit yes when prompted to uninstall it. Hard ain’t it? Yeah, it is.
ADB can make it as simple as executing just a command
adb uninstall <package_name>
if you wish to keep the data and cache directory intact but uninstall the app just add -k as an option to the command
adb uninstall [-k] <package_name>
[ ] means it is an optional parameter
Of course, you can install an app too via
adb install [-r] [-s] <package_name>
the options available to the command are
-r : reinstall the app, keeping its data-s : install on SD card instead of internal storage
That looks neat.
Ok this one is my favorite 🙃
So here is the thing. You have like 5–6 devices connected to your dev environment and now you want to do some work with a specific device at a time. Piece of 🍰
Easy you say, right? Like you can just fire up your Android Studio and execute the app in some specific device.
Not so early 😅. Let me complete. You want to execute commands in a certain order 🙄 ..yeah it isn’t easy as you thought before , eh 🤔
For our example here let’s assume we have like 3 devices and 2 emulators running, named as device_1, device_2, device_3, emu_1, emu_2.
Our end goal is to be able to execute some adb command on devices/emulator in order such as
Cool ? Now first of all let me give you the magic commands to handle this situation. Lets look at what's available to us
adb -d <command>
What this will do is it will directs command to the only connected USB device. Naice 🙂
You can also use adb -e <command> to directs command to the only connected Emulator
But we wanna go a step ahead and orchestrate commands in a pattern (..by writing a bash script, obviously). But to do that I need to specifically direct commands to the device I want to control.
adb -s <device_id> <command>
Get the device id by using adb devices
Now we are talking 😎. Now the only thing I gotta do is create a bash script something like this
#!/bin/bash
adb -s <emu_1_id> <some_command>adb -s <device_3_id> <some_command>adb -s <device_1_id> <some_command>adb -s <device_2_id> <some_command>adb -s <emu_2_id> <some_command>
where <some_command> doesnot have to be the same.
..and
That’s all there is. Every time you wanna run it like this just run the bash script 🤓 😎
That’s like creating a symphony with your devices, except for the fact it is not ..gonna be a symphony in the first place. And its just code executing🤔 But you get my point , right😅
Read my post “Life without wires: ADB over WiFi” to learn how to set it up.
There are times when the device would not show up in your IDE. Possible issue could be that adb is not responding.
To fix that simply kill the server and start it again.
# Kill the server if it is runningadb kill-server
# Start the serveradb start-server
I have seen that you also need to uncheck and then check ADB Integration inside Android Studio as an extra step.
# Prints about the current state of the device: offline | bootloader | deviceadb get-state
# Prints the serial number of the deviceadb get-serialno
# Prints the device pathadb get-devpath
You can advocate about trying to copy files from your device by opening up some software and then dragging and dropping things onto your desktop. But I am a fan of automating things and the above-mentioned method is completely fine if you are doing that for one file and only once or twice.
It changes when you wanted to do the same task again and again. When you want to automate things adb commands come in handy. You can even add them to a script and run it every time one of the tasks completes.
To push a file/dir to device
adb push <local> <remote>
where <local> is file in your local system i.e my_image.png and <remote> is file location in device/emulator i.e /sdcard/Downloads/my_image.png
adb push ~/Downloads/my_image.png /sdcard/Downloads/my_image.png
To pull a file/dir from device
adb pull <remote> [<local>]
where <local> is file in your local system i.e my_image.png and <remote> is file location in device/emulator i.e /sdcard/Downloads/my_image.png
adb pull /sdcard/Downloads/my_image.png my_image.png
Generate Public/Private Key
adb keygen <filename>
The private key is stored in <file>, and the public key is stored in <file>.pub Any existing files are overwritten.
Get a bug report
# Prints to the standard outputadb bugreport
# Writes to a file named bugreport.txtadb bugreport > bugreport.txt
Get list of all connected devices
adb devices
Of course, this is inbuilt into the IDE, but it is worth knowing that it is not just a window which you use in Android Studio, but so much more.
Let’s check out its power when used from terminal
# Dumps log entries from 3 log buffers: main, events and radioadb logcat
# Dumps log entries in long formatadb logcat -v long
# Dumps log entries into rotated log files on the deviceadb logcat -f /sdcard/Download/Android.log -r 1024 -n 8
# Dumps only error log entries generated from the “AndroidRuntime” process, including Java exceptionsadb logcat AndroidRuntime:E *:S
# Clears (flushes) the entire log and exitsadb logcat -c
# Dumps the log to the screen and exitsadb logcat -d
# Writes log message output to a file. The default is the “stdout — Standard Output”adb logcat -f <filename>
# Prints the size of the specified log buffer and exitsadb logcat -g
Logcat Format
To define the format of each log entry that is dumped , you need to set the output format for log messages
# The default is brief formatadb logcat -v <format>
# Display priority/tag and the PID of process issuing the message (the default format)adb logcat -v brief
# Display PID with the log infoadb logcat -v process
# Display the raw log message, with no other metadata fieldsadb logcat -v raw
# Display the date, invocation time, priority/tag, and PID of the process issuing the messageadb logcat -v time
# Display the priority, tag, and the PID and TID of the thread issuing the messageadb logcat -v thread
# Display the date, invocation time, priority, tag, and the PID and TID of the thread issuing the messageadb logcat -v threadtime
# Display all metadata fields and separate messages with blank linesadb logcat -v long
Filter your log data
A few people would say that we can always use | grep “filter_text” appended to the adb logcat. Well, you are right you can, but there are some neat tricks hidden in the actual implementation of logcat and you should look at them too.
adb logcat <options> <process-tag>:<priority-code>
When filters are provided, the logcat command will dump log entries that are generated from specified processes with priorities higher than the specified code, plus all log entries that generated from unspecified processes.A wildcard character, *, can be used to represent all processes
List of all priority codes that can be used in the “logcat” command:
This is pretty well known to people who are into Modding their device. But for people who aren’t going to mess with their device, here is what you can do with ADB
# Remounts the /system partition on the device read-writeadb remount
# Reboots the device, optionally into the bootloader or recovery programadb reboot [bootloader|recovery]
# Reboots the device into the bootloaderadb reboot-bootloader
# Restarts the adbd daemon with root permissionsadb root
# Restarts the adbd daemon listening on USBadb usb
# Restarts the adbd daemon listening on TCP on the specified portadb tcpip <port>
I am just going to list some more commands, just because well they exist! 😋
# Show help about all commands for adb, check this to get info about more commands in adbadb help
# Shows the version number for adbadb version
Comment/Suggestions always welcome.
If you would like to get more of such android tips and tricks, just hop onto my Android Tips & Tricks Github repository. I keep updating it constantly.
Keep on crushing code!🤓 😁
P.S.: This post was first published at my blog Crushing C.O.D.E
I would like to mention that @pbreault wrote a plugin for Android Studio and IntelliJ IDEA called ADB Idea [Github], which allows you to do some of the common and straightforward interactions with ADB inside IntelliJ platform i.e Android Studio. Do check it out too 🙂
Thanks for reading! Be sure to click ❤ below to recommend this article if you found it helpful.
You can connect with me on Github, Twitter, Linkedin, Facebook, Dribbble and Google+