CodeWithMMAK

How to Connect Android Devices via ADB over USB: A Complete Guide

Master the Android Debug Bridge (ADB) to connect real devices to your PC. Learn how to enable USB debugging, capture logs, and automate mobile testing efficiently.

CodeWithMMAK
March 9, 2019
8 min

Introduction

🎯 Quick Answer

To connect an Android device via ADB over USB, you must first enable Developer Options on your device by tapping the "Build Number" seven times in Settings. Then, enable USB Debugging within the Developer Options menu. Connect your device to your PC via a USB cable, download the Android Platform-Tools, and run the command adb devices in your terminal to verify the connection. This allows for app installation, log capturing, and automated testing.

Using a real Android mobile device for automation or manual testing is a challenge for testers. However, Android offers a great solution to connect a real device over USB using the ADB tool. It helps in getting ADB logs for debugging, automation, or to mirror the screen.

📖 Key Definitions

ADB (Android Debug Bridge)

A versatile command-line tool that lets you communicate with an Android device. It facilitates actions like installing apps, debugging, and running shell commands.

USB Debugging

A mode that allows an Android device to communicate with a computer running the Android SDK to use advanced operations.

Logcat

A command-line tool that dumps a log of system messages, including stack traces when the device throws an error.

Platform-Tools

A component of the Android SDK that includes tools that interface with the Android platform, primarily adb and fastboot.

Enabling Developer Tools on Android

Before you can use ADB, you must prepare your device:

  1. Go to your Android Device Settings.
  2. Go to About phone.
  3. Tap multiple times (usually 7) on Build number until you see a message saying "You are now a developer!".
  4. Go back to Settings and search for Developer options.
  5. Enable the USB debugging option.
  6. Connect your phone to the PC. On the phone, drag down the notification panel and select USB options.
  7. Select either Transfer files or MTP/PTP mode.

🚀 Step-by-Step Implementation

1

Download Platform-Tools

Visit the Android Developer site and download the SDK Platform-Tools for your OS (Windows, Mac, or Linux).

2

Extract and Navigate

Unzip the downloaded folder. Open a terminal or command prompt inside the platform-tools directory.

3

Verify Connection

Run the command:

Code Snippet
adb devices

If prompted on your phone, select "Always allow from this computer" and click OK.

4

Capture Logs

Start monitoring system logs in real-time:

Code Snippet
adb logcat
5

Install an App

You can push an APK to the device directly:

Code Snippet
adb install path/to/your/app.apk

Advanced ADB Commands

1. Wireless Connection (Optional)

If you want to disconnect the cable and stay connected over the same Wi-Fi:

  1. Connect via USB first.
  2. Set the port: adb tcpip 5555.
  3. Find your phone's IP in Settings > About phone > Status.
  4. Connect: adb connect 192.168.X.X.

2. Detailed Logging

  • Display time with logs:
    Code Snippet
    adb logcat -v time
    
  • Capture log to a file:
    Code Snippet
    adb logcat -d > crash_report.txt
    

3. Device Management

  • Reboot device: adb reboot
  • Enter Shell: adb shell
  • Disconnect all: adb disconnect

Common Errors & Best Practices

⚠️ Common Errors & Pitfalls

  • Device Unauthorized

    If adb devices shows "unauthorized", check your phone screen for a permission popup. Re-plugging the cable often triggers it.

  • Missing Drivers (Windows)

    Windows sometimes fails to recognize the device. You may need to install the specific OEM USB drivers for your phone (Samsung, Google, etc.).

  • Cable Issues

    Not all USB cables support data transfer; some are "charging only". Always use a high-quality data cable.

Best Practices

  • Always use adb logcat -c to clear old logs before starting a new test session.
  • Add the platform-tools folder to your System PATH environment variable for easier access from any terminal.
  • Use adb shell dumpsys to get detailed information about system services and battery status.
  • When automating, use the -s <serial_number> flag if multiple devices are connected.

Frequently Asked Questions

Is ADB safe for my phone?

Yes, ADB is an official tool from Google. However, enabling USB debugging does make your phone slightly more vulnerable if connected to an untrusted computer.

Can I use ADB on a locked phone?

No, you must unlock the phone and authorize the computer at least once while the phone is unlocked.

Why does my device keep disconnecting?

This is often due to a loose USB port, a faulty cable, or aggressive power-saving settings on the PC's USB hub.

Conclusion

Mastering ADB is a fundamental skill for any mobile developer or QA engineer. It provides deep visibility into the Android system and enables powerful automation capabilities that are impossible through the standard UI alone.

📝 Summary & Key Takeaways

Connecting an Android device via ADB over USB is a straightforward process involving enabling Developer Options and USB Debugging. Once connected, the adb command-line tool provides extensive control over the device, from capturing real-time logs with logcat to installing apps and running shell commands. By following best practices like clearing logs and using high-quality cables, you can ensure a stable and productive mobile testing environment.

Share it with your network and help others learn too!

Follow me on social media for more developer tips, tricks, and tutorials. Let's connect and build something great together!