B04a: ADB and Accessing Logs

B04a: ADB and Accessing Logs

adb (the Android Debug Bridge) is the tool for interacting with an Android device. It provides numerous helpful commands. The most important ones are listed below:

# List available devices:
adb devices

# Access a local device' shell:
adb shell

# Connect to a remote device:
adb connect <HOST>[:PORT]
adb disconnect [HOST[:PORT]]

# Push & Pull -> Copy files:
adb push [--sync] [-z <ALGORITHM>] [-Z] <LOCAL...> <REMOTE>
adb pull [-a] [-z <ALGORITHM>] [-Z] <REMOTE...> <LOCAL>

# Sync a local build from $ANDROID_PRODUCT_OUT to the device (default all):
adb sync
# Sync only a single partition, e.g., /system
adb sync <PARTITION>
# Throw in some -l if you're curious what _would_ actually be transfered (dry-run)
adb sync -l [<PARTITION>]

# App installation
adb install app.apk

# Bug report
# https://developer.android.com/studio/debug/bug-report
adb bugreport report.zip

# More
adb help

Accessing Logs

# Access logs:
adb logcat

# Dump the current state of the log but do not continue to follow it:
adb logcat -d

adb logcat --help

# Inspect individual logcat buffers
# See also https://developer.android.google.cn/tools/logcat?hl=en
adb logcat -b radio
adb logcat -b main
adb logcat -b all

Logcat alternatives:

Getting kernel log messages:

# root-enabled device ('su' may help as well):
adb shell
$ dmesg

Development workflow, e.g., for Pixel devices, the boot loader needs to be unlocked

source build/envsetup.sh
lunch <option>
m -j$(nproc)
adb reboot bootloader
fastboot flashall

Hot Syncing

Syncing a source tree with a running (virtual) device can be done easily using sync commands but requires some preliminary steps, including disabling verified boot features and making the target partitions writable.

# Prepare
adb root
adb disable-verity
adb reboot
adb root
adb remount

# Afterwards
m sync

adb sync

adb shell "stop && start"

Debugging Services

Dumpsys

adb shell dumpsys -l

adb shell dumpsys

# For additional parameters for specific services see
adb shell dumpsys <servicename> help

# Car Services
adb shell dumpsys car_service

adb shell dumpsys car_service --list
adb shell dumpsys car_service --services [service-name]

# Your own services
adb shell dumpsys activity service com.example.yourapp

cmd

A command line interface for several system services. Not all services provide such an interface. Examples:

adb shell cmd -l                                    # lists all available services
adb shell cmd wifi connect-network VirtWifi open    # connects to SSID VirtWifi with no security
adb shell cmd car_service garage-mode on            # Puts car into garage mode
adb shell cmd window                                # lists all commands of the window manager

am, pm, ...

The Activity Manager am

adb shell am            # print all options and the help text
adb shell cmd activity  # same as adb shell am
adb shell am force-stop <PACKAGE>
adb shell am stop-service <INTENT>
adb shell am start-service <INTENT>

The Package Manager pm

adb shell pm                 # print help & all options
adb shell cmd package        # same as adb shell pm
adb shell pm list packages   # list all installed apps
adb shell pm uninstall       # uninstall an app
adb shell pm grant           # grant a runtime permission

fastboot

Apart from adb there is also the fastboot tool in the Android platform tools. We do not cover it in this training, but it's nevertheless important. It's the tool to flash the initial partition images onto the device or for recovering a bricked device. See Flash with Fastboot for more information.