Happy Black Friday!
Use
BLACKFRIDAY2024
with
30% OFF
Buy Now

How to use Android Debug Bridge (adb) on Android Emulators?

Cover Image for How to use Android Debug Bridge (adb) on Android Emulators?

This mini-blog will guide you on how to use the Android Debug Bridge (adb) command to perform complicated tasks under seconds.

1. What's Android Debug Bridge (adb)?

Android Debug Bridge is a powerful debugging tool that allows Android Developers to communicate with Android Emulators and executes many commands.

It facilitates how developers do many complicated and error-prone tasks, such as:

  • Override / Revert HTTP Proxy
  • Turn On/Off Wifi Network
  • Install and Trust
  • Close all running application
  • and more...

It enables developers to can perform many tasks by command line and save more time to focus on the UI/UX of the app.

You can read more at: https://developer.android.com/studio/command-line/adb

2. Installation

If you haven't installed the adb command yet, you can manually install by following the guideline:

1/ Install homebrew

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

2/ Install the adb command

$ brew cask install android-platform-tools

Read more at: https://stackoverflow.com/questions/31374085/installing-adb-on-macos

3. Common Tasks

All the following tasks requires the rooted Android Emulator.

Root Android Emulators

Please make sure that your Android Emulator is Google APIs version, not Play Store version.

How to use Android Debug Bridge (adb) on Android Emulators?

  1. Open the Android Emulator from Android Studio
  2. Run
$ adb root
  1. Wait couple minutes for restarting the devices.

Override HTTP Proxy

  • Override HTTP Proxy from Android Emulator to IP 127.0.0.1 at port 9090
adb shell settings put global http_proxy 127.0.0.1:9090

Revert HTTP Proxy

  • Since the HTTP Proxy config is overridden by the command line, it's crucial to revert it back when you don't need it anymore. Otherwise, your Android Emulator might not be able to access the Internet.
adb shell settings put global http_proxy :0

Install and Trust Self-signed certificates

  • Install and trust self-signed certificates on Android Emulator is painful, especially if you're using Android 11+, which requires extra steps to trust the certificate in the system.

You can automatically achieve it by using the following commmands:

# 1. Prepare your certificate
$ subjectHash=`openssl x509 -inform PEM -subject_hash_old -in my-certificate.pem | head -n 1`

$ openssl x509 -in my-certificate.pem -inform PEM -outform DER -out $subjectHash.0

# 2. Copy to Android Emulators
$ adb push ./$subjectHash.0 /data/misc/user/0/cacerts-added/$subjectHash.0
$ adb shell "su 0 chmod 644 /data/misc/user/0/cacerts-added/$subjectHash.0"

# 3. Restart your android emulator to take effect!

You can verify whether or not the certificate is installed and trusted properly by navigating to:

  • Setting app -> Security -> Encryption & Credentials -> Trusted Certificate -> User Tab

Enable Wifi Network

$ adb shell svc wifi enable

Close all running applications

  • Prepare the script: closeall.sh
APPS=$(adb shell dumpsys window a | grep "/" | cut -d "{" -f2 | cut -d "/" -f1 | cut -d " " -f2)

    for APP in $APPS ; do
       echo "Closing: $APP"
       adb shell am force-stop $APP
    done
  • Execute the script
./closeall.sh

4. ADB with Proxyman

From Proxyman 2.10.0+, Proxyman introduces Automatic Script for Android Emulator, which performs complicated tasks with one-click.

It includes:

  • Override HTTP Proxy to Proxyman
  • Revert HTTP Proxy if need
  • Auto Download, Install, and Trust Proxyman CA Certificate to your Android Emulator.

You can access via Certificate Menu -> Install Certificate on Android -> Emulator

How to use Android Debug Bridge (adb) on Android Emulators?

Read more at: https://docs.proxyman.io/debug-devices/android-device/automatic-script-for-android-emulator


Proxyman is a high-performance macOS app, which enables developers to capture HTTPs traffic on iOS device, iOS Simulator and Android devices.

Get it at https://proxyman.io

Noah Tran
Noah Tran