Skip to main content

Debugging Your Application

After you have built an application , you need to be able to debug it and see what is going on inside your code . One of the handiest ways to be able to see inside your code it through the use of break points. Breakpoints allow you to pause the execution of your code at specific location and see what is going on (or what is going wrong). Let's take a look at how to use breakpoints in Android Studio.

Setting Breakpoints

Breakpoints are mechanism by which you can tell Android Studio to temporarily pause execution of your code , which allows you to examine the condition of your application . This means that you can check on the values of variables in your application while you are debugging it. Also , you can check whether certain lines of code are being executed as expected - or at all.

To tell Android Studio that you want to examine a specific line of code during debugging , you must set breakpoint at the line. Click the margin of the editor tab next to line of code you want to break at , to set a breakpoint. A red circle is placed in the margin , and the corresponding line is highlighted in red , as show in Figure

You can also set breakpoint by placing your cursor in the line of code. where you want it to break and clicking Ru->Toggle Line Breakpoint. Notice that the term used it toggle , which means that any breakpoints you set can be turned off the same way you turn them on. Simply Click an existing breakpoint to remove it from your code.

NOTE Android studio only pauses execution at breakpoints when you debug your application - not when you run it it. This means you must use the green arrow with the bug behind it (or select Run -> Debug 'app' , or press Shift + F9).

Let's say that you do not know the exact line of code where you want the break to be . You might want to check on the condition of your code when a specific method is called . you can set a method breakpoint by selecting Run -> Toggle Method Breakpoint . A method breakpoint is represented by a red circle containing four dots placed at the method signature.

Notice in the lower left-hand area of Figure that Android Studio has issued a warning that method breakpoints can dramatically show down debugging . This is because method breakpoints do more than simple breakpoints in their default state. By default , method breakpoints are set apart from simple breakpoints. Android Studio pauses execution when the method is hit , and it also automatically sets a corresponding breakpoint and pauses at the end of the method.

NOTE Using a method breakpoint is different from setting a simple breakpoint at the signature of the a method . You easily click in the margin of the editor tab at the signature of a method to set a simple breakpoint there However , this one will get hit when execution enters the method. Also , a method breakpoint automatically pauses when the method is exited.

 Thus far, I've discussed simple and method breakpoints . However , there are two other types of breakpoints that you examine in the section: temporary breakpoints and conditional breakpoints.

Temporary Breakpoints :-

A temporary breakpoint is useful when you are trying to debug a large loop, or you just want to make sure a line of code being hit execution. To set a temporary breakpoint, place your cursor at the location in the code where you want it break and select Run -> Toggle Temporary Line Breakpoint. Notice that a red circle containing a 1 is now placed in the margin.
 
 

The 1 in the red circle represents the fact that Android Studio only stops at this breakpoint the first time youe code enters it . After that , the line is executed as though there is no breakpoint set . This can be very useful if you want to ensure line within a loop is being hit , but you don't want to stop at line every time it is executed.

However , let's say that you want to ensure that line within a loop is only being called when a specific variable is set to true ( or something similarly complex ). In such a case , you can use a conditional breakpoint.

Conditional Breakpoints :-

A condition breakpoint is a breakpoint at which Android Studio only pauses when specific conditions are met . To set a conditional breakpoint , first set a simple breakpoint at the line of the code you want to examine , the right-click the simple breakpoint to bring up the condition context menu.

From here you can set conditions that tell Android Studio when to pause at a breakpoint . For example , you can tell Android Studio to only pause at a line of the code when your variable named foo equals true. You would then set the condition in the breakpoint to

              foo == true

Conditional breakpoints are extremely useful in diagnosing intermittent issues in complex code blocks.

Comments

Popular posts from this blog

Applying Styles and Themes to an Activity

By default , an activity is themed to the default Android theme. However , there been a push in recent year to adopt a new theme known as Material . The Material theme has a much more modern and clean look to it. There are two versions of the Material theme available to Android developers : Material Light and Material Dark , Either of these themes can be applied from the AndroidManifest.xml To apply one of the Material themes to an activity , simply modify the <Application> element in the AndroidManifest.xml file by changing the default android:theme attribute. (Please be sure to change all instances of "com.android" to whatever package name your project is using.) <?xml version="1.0" encoding="utf-8"?> <manifest xmls:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        package="com.android.activity101">        <application ...

Using Code Completion

Code completion is an invaluable tool that shows you contextual options for completing the piece of code that you are trying to write. For example, in the editor tab for the MainActivity.js file, locate the line that reads.          setContentView(R.layout.activity_main); Place your cursor after this line and press the Enter Key . On the new line , type the letter R , and then type a period, as shown here:          R. Android Studio Code Completion should display a list of values that you could use to try to complete the code statement. Figure shows what this list might look like . this is important if you are not entirely sure of the spelling of a method call or how to identify the different method signatures. NOTE if the code completion window does not open , press Ctrl + Space to force it to open. This is the same key combination used in some other IDEs for their versions of the code complet...

Now in Android

Android Studio 4.2 discharged to steady channel   Android Studio 4.2 is now available in the stable release channel. Read the blog for detailed information on what’s new, including a new tool to help migrate your project to the latest Android Gradle Plugin version. We’ve also enhanced lots of stuff such as Database Inspector , System Trace , SafeArgs support, Apply Changes, and the new project wizard. As always, download here and file issues here .           Hilt is stable and ready for production Manuel Vivo wrote about the stable release of Hilt , Android Jetpack’s recommended dependency injection (DI) solution for Android apps. Hilt is a simpler, more opinionated way to leverage the power of the Dagger DI library, eliminating boilerplate and reducing errors. It provides direct injection support for popular Jetpack libraries such as ViewModel, WorkManager, Navigation, and Compose. ( DI Basics , Documentation )   Google Play updates ...