Android Development

Google's android guide Home Contact

Android Debugging

To identify and resolve issue in the code during development is critical aspect of any development. Android provides few tools shipped with the SDK to fascilitate this. Effective debugging may require use of more than one tool, depending on nature of the issue to be resolved.

Logging (Logcat)

Logcat comes shipped with the Android operating system, and works by capturing and displaying log messages generated by various components of the Android system and apps. Logging is used to capture information, warnings and errors in the code using Log class. Logcat is used for tracking the flow of app, identifying runtime issues, and viewing custom log messages. Log messages are generated by different components of the Android system, including the kernel, system services, and apps. These messages can include information about system events, app lifecycle, variables, methods called, network activity, errors, and more. Developers and testers can use logcat to gain insights into app behavior and diagnose issues during development and testing.

Main Logcat Features

  • Logging statements are inserted in places of the code, to capture information about the state of variables, method calls, and other relevant data.
  • Various logging levels can be specified (e.g., debug, info, error) and tags are added to the log messages to categorize them. Tags are short strings that help to identify the source or context of the log message.
    Log.d("MainClass", "This is a debug message in MainClass.");
    
  • Each log message is typically formatted with essential information, inluding:
    - Log level (e.g., Debug, Info, Error)
    - Tag (identifying the source or context)
    - Timestamp
    - Process ID (PID)
    - Thread ID (TID)
    - Log message content
  • Log messages can be filtered based on various criteria, such as log level, tag, text content, and more. Filtering allows to focus on specific types of log messages or components of interest. The filtered log messages are then displayed in the Logcat panel.
  • When the Logcat panel is opened in Android Studio or by use the adb logcat command from the command line, it initiates a connection to the logcat daemon. This allows to read and display the log messages in real-time.
  • Logcat continuously monitors and captures new log messages as they are generated. It updates the Logcat panel with the latest log entries in real-time.

Debugger (Breakpoints)

The Android debugger, commonly used with Android Studio is a powerful tool for inspecting and debugging Android applications. It works by attaching to the running process of an Android application and allows to examine the application's state, step through code, and diagnose issues. Debugger allows to pause execution at specific points that allows to inspect and step through the execution of the code. It inspect variables, evaluate expressions and navigate through the call stack which provides a visual representation of the program's execution flow and allows to inspect the state of the program at various points during debugging sessions.

Some of the main features for using debugger include:

  • In debug mode, the compiler includes additional debug information in the compiled code. This information includes mapping between source code and compiled code, variable names, and line numbers.
  • Breakpoints are set in the code by clicking in the gutter next to the line number in Android Studio. When the app's execution reaches a breakpoint, it pauses, and control is transferred to the debugger.
  • When the app is paused at a breakpoint, the values of variables and objects in the current scope can be inspected. Android Studio displays this information in the debugger's Variables panel.
  • Code can be stepped through using controls provided by the debugger. Common actions include stepping into a function, stepping over a function call, and stepping out of a function.
  • The debugger maintains a call stack, showing the sequence of function calls that led to the current execution point. Call stack can be navigated through to understand the reaching to current state.
  • Android apps often run multiple threads concurrently. The debugger allows to switch between threads and inspect their state independently.
  • The debugger can be integrated with Logcat, allowing developers to view log messages generated by the app while debugging. This helps in capturing additional context and information during debugging sessions.
  • Debugger allows to set breakpoints with conditions, which cause the debugger to pause only when specific conditions are met. This is helpful for isolating issues that occur in specific scenarios.