Hey everyone, so I covered a basic introduction to Android Debugger Bridge (ADB), props to Stephen Janaway who created some of this with me for a course we run. ADB allows you to run various commands to a connected Android device via USB or WIFI.
Installation
I didnât cover how to get it installed, but the documentation from Google is good, so just head over to the Android Studio site and follow their instructions. Once installed youâll want to open the âSDK Managerâ
and download a version of the Android SDK, I downloaded 8.1 as my phone is running 8.1. Youâll also want to download the following SDK Tools.
Youâll also want to add ADB to your âpathâ, lots of Google guides on how to do this, but you should eventually get to the point where you can write âADBâ in your terminal and see lots of stuff like:
Android Debug Bridge version 1.0.40
Version 4797878
Installed as /Applications/android-sdk-macosx/platform-tools/adb
Configuring Your Device
Before you take advantage of ADB with your device, you need to enable USB Debugging. To do this you need to enable Developer options on your device. You have to go in to System > About Phone and tap the âBuild numberâ seven times. You should see a message saying âYou are now a developerâ. If you go back to System, you should now see an option called âDeveloper optionsâ, go into there, scroll until you see the Debugging section and enable âUSB debuggingâ. Youâll device will now be ready to receive ADB commands.
The first time you send a command, youâll have to check a box on your phone that asks you if you trust the device sending commands, you only have to do this one per machine
Install an App
The first command we covered was
adb install /path/to/your/file.apk
This instructs ADB to download the apk file to your device and install it. Before I discovered this we were putting apks files in Google Drive and downloading them that way, it use to take me 90 seconds or so to do this, takes 10-15 with ADB. We also moved to HockeyApp, but again it took around 90 seconds to open HockeyApp, updates builds and click install.
Uninstall an App
If we can install we want to be able to uninstall!
adb shell pm uninstall com.package.name
There is a bit more involved in this one. Firstly we introduce âshellâ which gives us the ability to run shell commands directly on the device. We then call âpmâ which is short for Package Manager, the Android applications that you use when you uninstall apps on your device. Then we send the âuninstallâ command. Finally, we have to provide the name of the package, you can get this from the âmanifest.xmlâ in the source code of your app, if you donât have access the developer should be able to give you this.
Update an App
Testing the update procedure of an app is really important, and ADB gives you a way to do this. We take the exact same command as install, but we add â-râ to it, this instructs ADB to replace the existing app with this one.
adb install -r /path/to/your/file.apk
Clear App Data
If you have an app that you have to log into, or saves the users activity for example you sometimes want to clear that and redo a test. Or perhaps log in as another user. Or you want to test as a ânewâ user, perhaps to see something like an onboarding screen. Before I discovered clear I was doing this by uninstalling and reinstalling the app, not anymore!
adb shell pm clear com.package.name
This command mimicks going into the App Infor and clicking Clear Data.
Taking Screenshots
Iâm not ashamed to admit that for the early stages of mobile testing career I was taking screenshots by using the buttons on the phone, then uploading the screenshot to Google Drive or emailing it myself, not anymore!
adb shell screencap /sdcard/myscreenshot.png
This ADB command will take a screenshot and save it to where you specify on your device. But we need it on our computer! So,we take advantage of another ADB command:
adb pull /sdcard/myscreenshot.png
Pull will download the file from my device and save it in the directory Iâm currently in on the Terminal.
With the help of Mark Collins, I turned this into a bash script, which I saved as a .sh (shell) file and added it to my /usr/local/bin folder on my Mac meaning I could call this script from anywhere in the Terminal. Iâve added the script to a Gist over on GitHub.
Recording Video
A nice feature I discovered from ADB is the ability to record your screen. This is great for enhancing your bug reports, especially with animation issues. And again, Iâd previously done this by recording one phone with another
adb shell screenrecord --bit-rate 6000000 /sdcard/myrecording.mp4
Again when you run this, you start it, do you action on the app, then stop it in the terminal and the mp4 will be saved to your phone, so then we have to download it to our computer like above.
Iâve also turned this into a script so itâs easier for me to utilise, you can also find this over on GitHub.
Bugreport
Android comes with a command called bugreport
adb bugreport
This is really helpful for your developers. It basically takes a dump of the phone including logs, memory, processes and much more. Majority of it made absolutely no sense to me, but my developers were very appreciative of recieving it.
Log Watching
adb logcat
This will print the logs from the device to the Terminal. Again very useful for enhancing your bug reports. You can also take advantage of BASH and tail this straight to a file, adb logcat > mylogs.txt.
Android Studio
All the above can be accessed without a Terminal using Android Studio if you prefer a GUI. With your device connected go to View > Tool Windows > Logcat.
From here youâll be able to see the logs, take screenshots and record the app.
Android Profiler
The final thing I demoed was Android Profiler. You can find this like the above at View > Tools Windows > Android Profiler. With this you can monitor CPU, Memory and Network usage. I would regularly have this open while doing Exploratory Testing, as it would help me identify potential memory/CPU spikes, as well as excessive network usage which could cost our users a lot in data!
Summary
If you are testing Android applications, I strongly encourage you to explore ADB/Android Studio, itâs like having Android superpowers! Utilising ABD saved me hours, especially around installing new versions. I blogged about a nice solution using Appium and ADB a while back now. As well as the simple things of taking screenshots went from 60-90 seconds to 10. That time really adds up over a release!