avatarSiva Ganesh Kantamani

Summary

The web content provides a guide on advanced debugging techniques in Android Studio, covering topics from basic logging to using breakpoints and customizing the Logcat.

Abstract

The article "Android Studio tips & tricks -Part 3(Debugging)" is a comprehensive tutorial aimed at developers looking to enhance their debugging skills within Android Studio. It builds upon previous articles focused on beginners and advanced users, diving into the intricacies of debugging Android applications. The guide emphasizes the use of LogCat for basic debugging without needing to run the app in debug mode, illustrates how to implement logs with varying verbosity levels, and demonstrates how to customize Logcat colors for clarity. It also advises on preventing log clearance upon app crashes, using breakpoints for step-by-step debugging, and evaluating variable values at runtime. The article underscores the importance of keeping Android Studio updated to leverage new features and encourages developers to follow best practices such as removing sensitive logs before production.

Opinions

  • The author, quoting Edsger W. Dijkstra, suggests that debugging is an inherent part of programming due to the inevitability of introducing bugs during development.
  • The author expresses the importance of clear and distinct logging, advocating for the use of tags and different log types to filter and prioritize messages in Logcat.
  • Customizing Logcat colors is presented as a personal preference of the author for improving log readability, indicating a subjective approach to debugging efficiency.
  • The article conveys a best practice of removing or restricting sensitive data logs before releasing an app to production, highlighting a concern for security and privacy.
  • An opinion is shared that developers should keep their Android Studio updated to take advantage of the latest features and improvements, reflecting a belief in the continuous evolution of development tools.
  • The author's enthusiasm for sharing knowledge is evident, as they invite readers to follow them on Medium for further insights and articles on Android development.

Android Studio tips & tricks -Part 3(Debugging)

This article is the continuation for the article Android Studio tips & tricks -Part 1(for beginners) and Android Studio tips & tricks -Part 2(Advanced).

In the words of the great Edsger W. Dijkstra: “If debugging is the process of removing software bugs, then programming must be the process of putting them in.”

Android Studio is a great tool not only for smart coding but also to debug the code in depth. In this article, I’m going to show you how to deal with simple to complex debugging situations. Let’s get into action.

You can start debugging by just clicking the Debug icon or press Shift + F9 after you run the app from the studio.

Debug icon

Debug using LogCat

Basic log implementation

You don’t need the app in debug mode for this. The log is a two-parameter utility which will show the output in the Logcat in Android Studio. The first parameter is a TAG which can be useful in filtering the content and the Second parameter is your message.

Different log types

There are 6 types of logs in Android studio, they are Log.v(), Log.d(), Log.i(), Log.w(), Log.e() or Log.wtf() and there levels of verbosity (VERBOSE, DEBUG, INFO, WARNING, ERROR). You can also filter which logs to appear in the logcat as shown.

Customize Logcat

To have a clear & distinct vision on every log that was printing in the logcat, you need to customize the logcat colors

File > Settings > Editor > Colors & Fonts > Android Logcat

Setting colors for different logs

Prevent clearing the log when the app crashes

By default when the app got crashed the logcat clear’s in the android studio. To prevent this, click the right end chooser(Filter Configuration chooser) in the logcat tab then select Edit Filter Configuration option and enter current app name and package name.

Remember that if you have any sensitive data remove the logs before going to production or print logs in debug mode. It is the best practice to remove the logs before going to production.

Logcat filters

BreakPoints

logcat is useful in the basic situations of debugging. If you want to debug step by step breakpoints is your option.

In order to use them, first set the breakpoints (Click beside line numbers in the android studio to add breakpoints ) and run the app in debug mode or run the app usually and attach the debugger (Shift + F9) then while you are using the app at the breakpoint the execution will halt and highlights the breakpoint as shown.

breakpoints demo

Through breakpoints, you can also see what are the values in the variables at that point of execution through Evaluate Expression option (Right the mouse to see options after selecting the variable). For example, intent-filter is null on the first breakpoint and has values when reached the second breakpoint.

Always use latest version of Android Studio

Developers at google is working hard to make studio useful in every possible way so don’t struck with any version be updated and have advantages of new features.

These are some basic debugging tools in android studio, explore Android Developers site for more info.

If you like my article, please don’t forget to click 👏👏👏 & to recommend it to others 👏👏👏.

Also, to be notified about my next articles and stories, follow me on Medium.

Android App Development
Debugging
Android
Android Studio
AndroidDev
Recommended from ReadMedium