Anatomy of an Android App

Sep 10 2024 · Kotlin 1.9.23, Android 14, Android Studio Iguana

Lesson 04: Leverage Logcat

Demo

Episode complete

Play next episode

Next
Transcript

In this demo, we’ll explore how to use Logcat in Android Studio to monitor and filter logs from multiple devices simultaneously. The setup includes using two physical devices mirrored through an app called Visor.

Setting Up the Environment

First, you’ll need to load your project in Android Studio. If you’re following along, ensure your two physical devices are connected and visible on your screen using the Visor app. We’ll build and run the app on both devices and monitor the logs using Logcat.

Clearing and Filtering Logs

  1. Clearing Logs:

    • Select the first device, Google Pixel 6, in Android Studio.
    • Navigate to Logcat, and clear the logs by clicking the clear logs icon.
    • Repeat the same process for the Pixel 3a.
  2. Filtering by Package:

    • With Pixel 6 selected, ensure there are no logs by filtering by the package name.
    • If you remove the filter, all logs from the device will be displayed, not just those related to your project.
    • Reapply the package filter to view only relevant logs.

Running the App and Viewing Logs

  1. Build and Run on Pixel 6:

    • Click the Debug icon to build and run the app on Pixel 6.
    • The app will run, and you can observe it in both the screen mirror and Android Studio.
  2. Switch to Pixel 3a:

    • Without stopping the app on Pixel 6, switch to Pixel 3a.
    • Hit Debug again to build and run the app on this device.
    • You can now switch between the logs of Pixel 6 and Pixel 3a in Logcat.

Viewing Logs Simultaneously

  1. Creating a New Logcat Tab:

    • You can create a new Logcat tab by clicking the appropriate icon.
    • Rename this tab to something like “Logcat 2” for easy identification.
  2. Splitting Logcat Pane:

    • To view logs from both devices simultaneously, split the Logcat pane by clicking the split panel icon.
    • Choose to split to the right to maximize screen width.
    • Now, you can see logs from Pixel 3a on one side and Pixel 6 on the other.
  3. Applying Filters:

    • Apply different filters to each pane. For example, you can filter by terms like “Bluetooth” to see all related logs, or by “info” to see only logs at that level or higher.

Demonstrating With the App

  1. Sending a Message:

    • On one of the devices, type a message like “Hi from Pixel 6”, and send it.
    • You should see this action reflected in the logs.
  2. Filtering by Tag:

    • Use the Main Activity tag to filter logs further.
    • Ensure the correct filter is applied to see the relevant logs.

Handling Errors and Permissions

  1. Forcing a Crash:

    • Attempt to force a crash by uninstalling the app and reinstalling it to trigger permission requests.
    • Observe how the logs reflect missing permissions and subsequent allowance.
  2. Monitoring Logs Post-Crash:

    • After resolving the permissions, filter by the Main Activity tag again to ensure logs are printed correctly.

Advanced Logcat Configuration

  1. Splitting and Adding Tabs:

    • You can further customize your Logcat view by splitting panes multiple times or adding additional tabs.
    • Each pane or tab can have different filters applied to monitor various aspects of your app’s behavior across multiple devices.
  2. Expanding the View:

    • Expand your Logcat view to see more information at once, which is particularly useful for large-scale debugging.

Conclusion

This demo should give you a solid understanding of how to use Logcat effectively in Android Studio, particularly when working with multiple devices. Feel free to experiment with splitting panes and applying various filters to suit your specific needs.

See forum comments
Cinema mode Download course materials from Github
Previous: A Tour of Logcat Next: Conclusion