Android Studio for Platform (ASfP) provides a powerful debugger that lets you:
- Select a device to debug on.
- Set breakpoints in your Java, Kotlin, C/C++, and Rust code.
- Examine variables and evaluate expressions at runtime.
Before you use the debugger, you must flash your build to a device or emulator.
App process (Java/Kotlin) debugging
To debug a Java or Kotlin application process:
Set breakpoints in your Java or Kotlin code within ASfP.
Select Run > Attach Debugger to Android Process from the menu.
In the Choose Process dialog, make sure the Debug type is set to Java Only.
Select your device from the list.
Choose the specific application process you want to debug.
Click OK.
Interact with the application on your device to hit the breakpoints.
System process (C/C++) debugging
To debug a system process written in C or C++:
Verify that you have only one device or emulator running.
Open a terminal and run
adb rootfrom your AOSP checkout root:bash adb root
Set breakpoints in your C/C++ code within ASfP.
Select Run > Attach Debugger to Android Process from the menu.
In the Choose Process dialog, change the Debug type to Native Only or Dual (Java + Native).
Check the Show all processes box to see system processes.
Select your device from the list.
Choose the specific system process you want to debug (such as
surfaceflingerorsystem_server).Click OK.
Interact with the device to hit your breakpoints.
Rust debugging
ASfP supports Rust debugging using the Debug Adapter Protocol (DAP) with LLDB. This section outlines how to set up CodeLLDB as a Debug Adapter Server and debug Rust code on the host and on an Android device.
Set up CodeLLDB as a Debug Adapter Server
Create a new Debug Adapter Protocol Run/Debug configuration:
- Select Run > Edit Configurations from the menu.
- Click the + button.
- Select Debug Adapter Protocol.
In the server tab, click create a new server.
In the newly opened dialog, click Choose template and select CodeLLDB from the list.
After selecting the CodeLLDB template, the new server is added with a predefined configuration.
- Enable verbose tracing by selecting Verbose in the Trace dropdown.
- Add an environment variable that specifies the path to the
lldb-serverin your Android source prebuilts:
LLDB_DEBUGSERVER_PATH=REPO_ROOT/prebuilts/clang/host/linux-x86/CLANG_VERSION/runtimes_ndk_cxx/x86_64/lldb-server ``` Replace `REPO_ROOT` with the absolute path to your Android source checkout. To find `CLANG_VERSION`, run the `get_clang_version.py` script from the root of your Android source tree: ```bash ./build/soong/scripts/get_clang_version.py- Do not alter the
<<insert base directory>>section.
Debug Rust binaries on the host
- Open the Configuration tab in your Debug Adapter Protocol Run/Debug configuration.
- Select Launch as the Debug Mode.
- Update the Working directory and select the Binary file you want to debug.
- Click OK to save the configuration.
- Start the debugging session by clicking the Debug icon next to the configuration.
The first time you run this, CodeLLDB downloads. You should see DAP traces in the console. Breakpoints set in your Rust code should be hit as expected.
Debug Rust binaries on an Android device (Attach mode)
Find the PID: Identify the Process ID (PID) of the application you want to debug on the Android device.
Start lldb-server on the device: From the root of your Android source tree checkout, run the
lldbclient.pyscript, replacing<PID>with the process ID:lldbclient.py --setup-forwarding vscode-lldb -p <PID>This script pushes the correct
lldb-serverto the device, starts it, sets up port forwarding (host port 5039 to device), and outputs the DAP JSON configuration needed for the next steps. Keep this terminal open.Start the CodeLLDB Debug Adapter on the host:
- Navigate to the CodeLLDB extension directory (default:
~/.lsp4ij/dap/codelldb/extension/adapter). - Set the required environment variables and start the adapter server, replacing
REPO_ROOTandCLANG_VERSIONas determined in the server setup:
# Sets PYTHONHOME env variable export PYTHONHOME=REPO_ROOT/prebuilts/clang/host/linux-x86/CLANG_VERSION/python3 # Tell the dynamic linker where to find python libs export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:REPO_ROOT/prebuilts/clang/host/linux-x86/CLANG_VERSION/python3/lib # Starts the CodeLLDB Debugger Adapter server on port 1234 ./codelldb --liblldb REPO_ROOT/prebuilts/clang/host/linux-x86/CLANG_VERSION/lib/liblldb.so --port 1234- Navigate to the CodeLLDB extension directory (default:
Configure the ASfP DAP Client:
- Go back to your Debug Adapter Protocol Run/Debug configuration in ASfP.
- Select the Configuration tab.
- Set Debug Mode to Attach.
- Set Address to
localhost. - Set Port to
1234. - Paste the JSON output from the
lldbclient.pycommand (Step 2) into the DAP parameters (JSON) field.
Click Debug to start the debugging session.
Troubleshooting
- If you see the error
error: Connection shut down by remote side while waiting for reply to initial handshake packet, terminate the current debug session, and restart thelldb-serveron the device and the CodeLLDB adapter on the host.