How to debug your app |  Android | Developers  Android Developers

Posted on

Android Studio provides a debugger that allows you to perform the following actions and more:Select a device on which you will debug your appSet interrupts in your Java, Kotlin, and C/C++ codeExamine variables and evaluate expressions at runtime

This page includes instructions for basic debugger operations. For more documentation, you can also refer to the IntelliJ IDEA debugging documents.How to enable debugging

Before you start debugging, you should prepare yourself as follows:Enable debugging on your device:

If you are using the emulator, this option will be enabled by default. However, for a connected device, you’ll need to enable debugging in the device’s developer options.Run a debugable build variant:

You must use a build variant that includes debuggable true in the build configuration. You can usually simply select the default “debug” variant that is included in each Android Studio project (even if it’s not visible in the build.gradle file). However, if you define new build types that should be debugable, you should add “debuggable true” to the build type as follows:Groovyandroid buildTypes customDebugType debuggable true…Kotlinandroid buildTypes create(“customDebugType”) debuggable = true…

This property also applies to modules with C or C++ code (the jniDebuggable property is no longer used).

If your app depends on a library module that you also want to debug, that library should be packaged with debuggable true so that it retains its debug symbols. To ensure that debug variants of your app project receive the debugable variant of a library module, be sure to publish non-default versions of your library.How to start debugging

You can start a debugging session as follows:Set some interruptions in the app code.In the toolbar, select the device on which you will debug the app from the drop-down menu of the target device.

If you don’t have a device set up, you need to connect one via USB or create an AVD to use android emulator.In the toolbar, click Debug.

If the “switch from Run to Debug” dialog is displayed, it means that your app is already running on the device and will restart to begin debugging. If you prefer to keep the same instance of the running app, click Cancel Debug and instead attach the debugger to the running app.

Android Studio compiles an APK, signs it with a debug key, installs it on the selected device, and runs it. If you add C and C++ code to your project, Android Studio will also run the LLDB debugger in the Debug window to debug your native code.If the Debug window is not open, select View > Tool Windows > Debug (or click Debug  on the toolbar), and then click the Debuggertab, as shown in Figure 1.

Figure 1: The “Debugger” window, which displays the current thread and object tree for a variableAttach the debugger to a running app

If your app is already running on your device, you can start debugging without restarting it as follows:Click Attach debugger to Android process. In the Choose Processdialog, select the process to which you want to attach the debugger.

If you’re using an emulator or device with administrator rights, you can check the Show all processes option to see all processes.

From the Use Android Debugger Settingsdrop-down menu, you can select an existing run and debug settings. (In projects with C and C++ code, this allows you to reuse symbol directories and LLDB boot and post-connection commands from an existing configuration.) If you don’t have a run and debug setting, select Create New. This enables the Debug Typedrop-down menu, where you can select a different debug type. By default, Android Studio uses the Auto debugging type to select the best debugging option based on whether your projects include Java or C/C++ code.Click OK.

The Debug window appears.

Note: The debugger and garbage collector in Android Studio are loosely linked. The Android virtual machine ensures that any objects that the debugger identifies are not collected as an unused item until the debugger is disconnected, which can lead to an accumulation of objects over time while the debugger is connected. For example, if the debugger sees a running thread, the associated Thread object is not collected as an unused item until the debugger is disconnected, even though the thread has finished. How to change the debugger type

Because different debugging tools are required to debug Java or Kotlin code and C/C++ code, the Android Studio debugger allows you to select which type of debugger to use. By default, Android Studio decides which debugger to use based on the languages it detects in your project (with the Autodebugger type). However, you can manually select the debugger in the debug settings (click Run > Edit > Configurations)or in the dialog that appears when you click Run > Attach debugger to Android process.

Available debugging types include the following:AutoSelect this type of debugging if you want Android Studio to automatically choose the best option for the code you’re debugging. For example, if you have C or C++ code in your project, Android Studio automatically uses the Dualdebugging type. Otherwise, use the Javadebugging type. JavaSelect this type of debugging if you only want to debug code written in Java or Kotlin. The Java debugger ignores any interrupts or analyses you’ve set in your native code. Native (available only with C/C++ code)Select this type of debugging if you only want to use LLDB to debug your code. When you use this type of debugging, the Java debugger session view is not available. By default, LLDB inspects only your native code and skips interrupts in your Java code. If you also want to debug your Java code, you need to switch to the Auto or Dualdebugging type.

Native debugging only works on devices that meet the following requirements:

To check if your device supports run-as, run the following command in the ADB shell that is connected to your device:run-as your-package-name pwd

Replace your-package-name with the name of your app’s package. If the device supports run-as, the command should be displayed without errors.

The device has ptrace enabled.

To check if ptrace is enabled, run the following command in the ADB shell that is connected to your device:sysctl kernel.yama.ptrace_scope

If ptrace is enabled, the command will print the value 0 or an unknown key error. If ptrace is not enabled, it will print a value other than 0.Dual (available only with C/C++ code)Select this type of debugging if you want to make a switch between Java debugging and native code. Android Studio attaches the Java and LLDB debugger to your app process, one for the Java debugger, and one for LLDB, so you can inspect for interruptions in both your Java code and native code without restarting your app or changing debugging settings.

In Figure 2, notice the two tabs to the right of the title of the Debugwindow. As the app has Java and C++ codes, one tab is for debugging the native code and the other for the Java code, as indicated by -java.

Figure 2: The tab to debug native code and the tab to debug Java code

Leave a Reply

Your email address will not be published. Required fields are marked *