Posted on

Android ADB Basics


Android ABD Basics
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

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

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

Android ABD Basics

And finally, run this command

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

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

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

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

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

To reset settings to default run those commands

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

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!

 

 


  • Post title say about ABD ..I think it should be ADB.

    • Oh, it supposed to be ABCD. Kidding. Thanks, Mahendra!

  • Jonas Sevcik

    I’ve created a set of scripts for automating the process of emulating other devices. Check it out here: https://github.com/jonasevcik/adb-emulate-device

    • Ihor Klimov

      Great job! Just put it into environment path and you’re good! Thanks

  • Abdullah Al Hasan

    After “adb connect my_ip_address” I got this message “Unable to connect my_ip_address”.

    The solution is:
    1. Make sure that “Aggressive Wi-Fi to Cellular handover” under Networking section in the device’s developer options is tured off.
    2. Ping from PC to Mobile device to make sure it’s not in network idle mode. “ping my_ip_address” (for Linux), “ping -t my_ip_address” (Windows)
    3. Some command:
    adb usb
    adb tcpip 5555
    adb connect my_ip_address

    Collected from: http://stackoverflow.com/a/33654987/6200296