

Designed by Freepik

Hey there! We all use Android Debug Bridge daily without concerning that we use it. Primarily because Android Studio does all the job for us, we don’t need to run any console commands. Installing apps, debugging, perfomance profilling – all is done through Android Studio interface, but at some point earlier or later you need to learn some adb commands, they will make your development process way easier and faster. Let’s make our development better with those Android ADB basics

What it is

Let’s start with basics first. What ADB does? All the job with actual devices or emulators does ADB. Installing apps, debugging etc. The moment you first open Android Studio run window you see ‘Initializing ADB…’ message, that’s us starting a server. This server runs on our computer. You can start it by running any command or by

adb start adb kill-server 1 2 adb start adb kill - server

And there’s a client program which is what we use on our computer and a deamon which is run on devices.

Game changer

Now if you’ve been devoloping using USB cable, you know that it’s lots of pain. And there’s many reasons for that.Wireless development is way much better! My out-of-the box Nexus 6p cable is about 10 inches long, which makes it closely attached to the USB port, even if you have a laptop, in front of you, you’ll still need to reach out to it or keep your laptop right in front of your face.

Second reason is glitchy USB cable, maybe it’s because I dropped my laptop many times, but in a slight movement of USB cable(when it’s being connected) it looses connection, which sucks, so sustainable wireless connection is way better.

Third reason is that you can charge your phone while developing, we all know that USB charging from computer doesn’t charge anything, in fact you might still even loose juice when connected.

And the last reason is just because of cable itself, the less cables – the better.

Setup using USB Cable

So how do we set it up for Wi-Fi development? Pretty easy. But you’ll still need a USB cable for a first step.

First connect your device to the computer via USB cable, then run

adb tcpip 5555 1 adb tcpip 5555

This command will set your device to listen on this port for adb connection. Next disconnect USB cable. Now make sure your device and computer connected to the same Wi-Fi network, because they’re going to communicate via local network.

Now you need to learn your device local IP address. You can do it ether via Settings -> About phone -> Status -> IP address or by having a home screen widget from some app. I prefer widget, it’s easier and faster, I use this app and that what it looks like on home screen

And finally, run this command

adb connect ip_address_of_device 1 adb connect ip_address_of_device

And you’re done! Just two commands. And the best part is that you don’t need to run the first command after that. You can shut down adb server or computer and then just lookup for device ip and run connect command. After restarting your phone you’ll still need to connect with cable and run first command.

Setup entirely wireless

So what if you don’t have a USB cable nearby or you don’t want to use it? You can setup device deamon and connection listener without connecting via USB at all. You’ll need this app, it requires root access though. And rooted device won’t install OTA updates, so if you want to root your device only for that reason, it might not be worth it, since you’ll spend more time installing updates manually than you save by not connecting it via USB cable. But after that you’ll never need to use USB cable again.

Debug

Now let’s look at some useful adb commands. First and most used by me is

adb install path_to_apk 1 adb install path_to_apk

Which installs APK to connected device.

Next one is sending a broadcast. Let’s say you have a broadcast receiver and you want to test it. No need to write any code – just run

adb shell am broadcast -a 'my_action' 1 adb shell am broadcast - a 'my_action'

And there’s many other useful commands for debugging app flow itself from Activity Manager, check them here

Sometimes you need to reset permissions to test app’s flow again on >= M, adb command for it is

adb shell pm reset-permissions -p your.app.package 1 adb shell pm reset - permissions - p your . app . package

Not including any package will remove all permissions from all apps

Emulate other devices

Let’s say you build an app and you have bunch of different layouts for different display sizes, API versions. You can have real devices or emulators running, which is the best solution, but sometimes OS is so overloaded and everything gets so slow that just installing it to new emulator will take up to 5 minutes.

There’s a super quick solution – emulate display on your device. For example if we want to emulate Nexus 9 display on Nexus 6p, we can do it with those two commands

adb shell wm size 2048x1536 adb shell wm density 288 1 2 adb shell wm size 2048x1536 adb shell wm density 288

First command sets pixel size of Nexus 9 display and second number of pixels per inch. Make sure to run both. But what you’ll see is your portrait mode will be a landscape and vice versa. It’s because we changed screen size, but Android doesn’t know that we emulate a tablet, it thinks we emulate a phone with this size. That’s why for emulating tablets just reverse resolution and you’re good

adb shell wm size 1536x2048 1 adb shell wm size 1536x2048

To reset settings to default run those commands

adb shell wm size reset adb shell wm density reset 1 2 adb shell wm size reset adb shell wm density reset

This might mess up your launcher app, so you’ll have to restart your phone.

And you can get any device’s metrics here, clicking on each item will show pixel density of each screen.

Brute Testing

And the last one for today is adb monkey command, it’s fast way to find crashes in your app. It runs random events for your app, starting activites, clicking buttons etc. And since it all happens pretty fast, it’s a good stress test as well. Just run this command

adb shell monkey -p com.myhexaville.restwithdatabinding -v 500 1 adb shell monkey - p com . myhexaville . restwithdatabinding - v 500

And replace with your package name. 500 – is the number of events, it will start your application if it’s not running. One advice – don’t play music during it. It will play around with volume! You can get a full reference to this command here

Alright, hopefully your learned something new that will make your development faster and easier today. Don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends if you think they will benefit from it!