The Android Debug Bridge (ADB) is a versatile command-line tool that acts as the crucial link between your development machine and an Android device. It empowers you to perform a wide range of actions, from installing and debugging apps to transferring files and accessing detailed device logs. A key component of ADB is the ADB Shell, which grants you direct access to the underlying Linux command-line environment on the device. This allows for powerful, low-level operations like managing file systems, controlling processes, and modifying system settings.
For any Android developer, mastering ADB and ADB Shell is not just a recommendation—it's a necessity. These tools are indispensable for efficient development, rigorous testing, and effective debugging. This comprehensive guide will walk you through everything from installation to advanced commands, helping you harness their full potential.
Getting Started: Installing ADB and Preparing Your Device
Before you can use ADB, you need to set up both your computer and your Android device. Follow these steps to get everything ready.
Step 1: Enable USB Debugging on Your Android Device
This is a critical first step. ADB requires "USB debugging" to be enabled in the Developer options on your device.
- On your device, go to Settings > About phone.
- Tap on the Build number seven times until you see a message that says, "You are now a developer!"
- Go back to the main Settings menu, and you should now see Developer options (it might be under System).
- Open Developer options and enable USB debugging.
Step 2: Install ADB on Your Computer
ADB is part of the Android SDK Platform-Tools package. You don't need the full Android Studio to get it.
- Navigate to the official Android SDK Platform-Tools download page.
- Download the package for your operating system (Windows, macOS, or Linux).
- Extract the contents of the downloaded ZIP file to a memorable location on your computer (e.g.,
C:\platform-tools
on Windows). - For convenient access from any terminal window, add the path to the extracted folder to your system's PATH environment variable.
Step 3: Verify the Installation and Connection
Once everything is set up, you can verify that ADB is working correctly.
- Connect your Android device to your computer via a USB cable. You may see a prompt on your device asking to "Allow USB debugging." Accept it.
- Open a command prompt (on Windows) or a terminal (on macOS/Linux).
- Type the command
adb devices
and press Enter. - If your device is connected and recognized, you will see its serial number listed with "device" next to it. If it shows "unauthorized," check your device for the authorization prompt.
Essential ADB Commands Every Developer Should Know
Once ADB is set up, you can start using its powerful commands. Here are some of the most fundamental ones you'll use daily.
List Connected Devices
The first command you should always run to ensure your device is connected properly.
adb devices
Install an Application
Installs an Android application (APK file) onto your device.
adb install <path_to_apk_file>
Uninstall an Application
Removes an application from your device using its package name (e.g., com.example.myapp
).
adb uninstall <package_name>
Transfer Files to Device
Pushes a file or directory from your computer to the device.
adb push <local_path> <remote_path>
Transfer Files from Device
Pulls a file or directory from the device to your computer.
adb pull <remote_path> <local_path>
Access the Device Shell
Opens an interactive Linux command-line shell on the device.
adb shell
View Device Logs
Displays the real-time log output from the device (Logcat), which is invaluable for debugging.
adb logcat
Reboot the Device
Reboots the connected Android device. You can also reboot into the bootloader (adb reboot bootloader
) or recovery (adb reboot recovery
).
adb reboot
Basic ADB Shell Commands
You can execute shell commands directly from your computer's terminal using the adb shell <command>
format, or you can enter an interactive shell with just adb shell
. Here are some essential shell commands.
List Files and Directories
Explore the device's file system, similar to the ls
command on Linux.
adb shell ls /sdcard/
List Running Processes
Shows all the processes currently running on the device.
adb shell ps
Dump System Service Information
Provides a massive amount of information about system services. You can specify a service to get targeted data (e.g., adb shell dumpsys activity
).
adb shell dumpsys
Start an App Activity
Launches a specific app component using the activity manager (am).
adb shell am start -n <package_name>/<activity_name>
Get System Properties
Displays the device's system properties, such as the Android version or screen density.
adb shell getprop
Set System Properties
Allows you to modify a system property. This often requires root access.
adb shell setprop <key> <value>
Simulate Text Input
Inputs a string of text into the currently focused text field on the device.
adb shell input text <text_string>
Debugging with ADB
ADB is your primary tool for identifying and fixing issues in your Android apps. Here are key commands for your debugging workflow.
Monitor Logs in Real-Time
The logcat
command is your best friend for debugging. You can filter by tag to see logs only from your app: adb logcat YourAppTag:D *:S
.
adb logcat
Generate a Full Bug Report
Creates a comprehensive report containing device logs, stack traces, and other diagnostic information, which is perfect for analyzing complex crashes.
adb bugreport
Check Java Debugger Connections
Lists the Java Debug Wire Protocol (JDWP) IDs of all processes that can be debugged. This is useful for attaching a debugger like the one in Android Studio.
adb jdwp
Force-Stop an Application
Immediately terminates an application's process. Useful for testing how your app behaves on a cold start.
adb shell am force-stop <package_name>
Performance Debugging with ADB Shell
ADB Shell provides powerful utilities for diagnosing performance bottlenecks like memory leaks and high CPU usage.
Check App Status and Performance
Using dumpsys
with a specific service like meminfo
can reveal detailed memory usage for your app.
adb shell dumpsys meminfo <package_name>
Monitor CPU Usage
The top
command shows a real-time list of processes sorted by their CPU usage, helping you identify CPU-intensive operations.
adb shell top
Trace System Calls
The strace
utility intercepts and records the system calls made by a process. This is an advanced technique for understanding low-level app behavior.
adb shell strace -p <process_id>
Advanced Usage of ADB and ADB Shell
Once you've mastered the basics, you can explore these advanced commands to further streamline your workflow.
Port Forwarding
Forwards a socket connection from a specific port on your computer to a port on the device. This is useful for debugging webviews or other network services on the device from your PC's browser.
adb forward <pc_port> <device_port>
Execute Commands as Your App
The run-as
command lets you execute shell commands with the same permissions as your debuggable application, allowing you to access its private data directory (/data/data/<package_name>
) without root.
adb shell run-as <package_name> <command>
Record the Device Screen
Records a video of the device's screen, which is perfect for creating demos, reproducing bugs, or analyzing animations.
adb shell screenrecord /sdcard/demo.mp4
Stress Test Your App
The Monkey is a tool that generates a stream of pseudo-random user events (clicks, touches, gestures). It's an excellent way to stress-test your app for stability and uncover rare crashes.
adb shell monkey -p <package_name> -v 1000
Backup and Restore Device Data
Creates a full backup of your device's data and apps to a file on your computer. You can later restore it with adb restore
.
adb backup -apk -all -f backup.ab
This article has covered the essential functions and commands of ADB and ADB Shell. By integrating these tools into your daily workflow, you can significantly enhance your efficiency in developing, testing, and debugging Android applications. We encourage you to experiment with these commands and explore their many options to become a more effective Android developer.
0 개의 댓글:
Post a Comment