Android Studio and Xcode app debugging with Breakpoints: How to from Zero
Tags: android, ios, xcode
Published: 2020-07-07 14:38:02

---
title: Android Studio and Xcode app debugging with Breakpoints: How to from Zero
published: true
tags: Android,iOS,Xcode
canonical_url: https://bugfender.com/blog/debug-breakpoints-android-studio-iphone-xcode/
---

_To kick off our series on debugging for software developers, we tell you how to build breakpoints step by step using Xcode and Android Studio to isolate key information about your app’s performance, and save crucial time during the process._

**Level:** beginner

**Platform:** iOS/Android

**Reading time:** 10 minutes

## **Ok first of all, what’s a breakpoint on iOS and Android ecosystem?**

To answer this question, let’s take a little trip down memory lane.

In my first-ever computer programming tutorial (called, very imaginatively, _Initiation to Programming_), we wrote an algorithm in **Pascal** to perform a series of simple operations, like printing the sum of two numbers to the console.

Once written, we pressed the execute key and waited for the result. It came back like this:

`var
  num1, num2: Integer;
begin
  // A simple program for adding two numbers.
  WriteLn('The result is: ', num1 + num2);
end.`

Soon, questions began rattling around my head (and not just about why the tutor made us use a programming language which was used to code the [Apple II](https://americanhistory.si.edu/collections/search/object/nmah_334638) 40 years ago).

As the algorithms became more complex, and started calculating things like the [greatest common divisor](https://brilliant.org/wiki/greatest-common-divisor/), I began getting unexpected outcomes. The output of the console started coming up incorrect.

Then it hit me. I was writing my first bugs.

At that moment I barely knew what the word ‘debug’ even meant, so I did what all programmers do. I added some logs to my code to understand what was happening (AKA _Writeln()_ everywhere).

The output of my console would now be something like:

`
$ Now I'm in the iteration 3 of the while loop
$ Var num1 has value: 7
$ Var num2 has value: 15
`

Writing that bunch of logs was helpful to understand the current state of the program execution and to find bugs. However, it wasn’t very practical. If, for example, you forget to print the result of a variable, you would need to re-write, re-compile and re-run the program.

Wouldn’t it be amazing if you could pause the program execution and print the value of all the variables? Well, it turned out someone had already thought of this.

That’s exactly what a breakpoint is. Thanks to the miracle of the breakpoint, app developers can pinpoint a specific point in their program and tell their IDE to “pause the program here and capture the value of every variable”.

Note: If you are using USB debugging with your device or remote debug with your phone, ipad, tablet, etc, this applies in the same way.

Here’s an example from [Xcode](https://developer.apple.com/xcode/).

![](https://lh5.googleusercontent.com/GQUl91LZDMlMQUn2-qu51zUf54hDLbcn5bZswK3meo9_BVWZb1ESyf4aWiHEBeHqipBxst61mEQFZ6m6m6glMfSIXzH3JJe2KrJpC97USIquG5Zjgy7unfkRY6Zx85-yOly5IRKT)

Here you can see that Xcode has paused the execution on line 31 and, in the lower pane, you can inspect the list of the stored values.

(At this point it’s worth mentioning that you should still have a good logging strategy, irrespective of breakpoints. Don’t worry, we’ll cover this in future chapters).

## Debugging app for Android and iPhone

### **How to set up a breakpoint** on Android Studio and XCode

Ok, so now let’s create your first breakpoint. Xcode and [Android Studio](https://developer.android.com/studio) both offer amazing options to get started; in fact, their debuggers are essentially the same.

The debugger allows mobile app developers to examine variables in real time and carry out much more sophisticated operations like evaluating expressions, modifying values during execution, and following the execution flow step by step.

To set up your breakpoint, you simply need to open a project in Xcode or Android Studio, and click close to the code line number.

![](https://lh5.googleusercontent.com/J4CKYyng4lVMwY3-kbeihQFS2j9YinqwFnXh6cQr4pntruy9XG7z3US7JyAj9f__y1qVl5sBhpsomKkyGUJN8QyXyvXvYIIFJ_Wfm0H9uj6n4fihNZCWbVQomXF8z9dT2JUOjbDM)

That’s how it looks in Xcode. You’ll see a blue arrow appear, indicating that the execution will pause when it reaches that point.

In Android Studio you can do exactly the same, but the debugger will not work by default. You need to press the button on the right, which shows a small bug, instead of the “Play button” on the left, to ensure that you are attaching the debugger.

![](https://lh3.googleusercontent.com/EStM7r4XbTWJKN7UEhjn19VY7wt_ghgV36uwBkr7R9qOiAxjP7eA1HSr0Zw-jfD3XIb2irTxVAQjDNNaawpmijLUy4c0ZRKmbMXjS54QuS9TJfRoAc1FQqbmF1dQHyEgg6kN4I2J)

The result will be pretty much the same as it is in Xcode. Once the point you’ve marked is reached, the program execution will stop and you will be able to check the values of all the variables.

![](https://lh4.googleusercontent.com/hb0wJpxbKM6TYb4DOab4dvtQojlCRALpMxZUtp21j0WO5XTYDYzNRIQb2xqvtxad_2CtGR9dEpzyijlMQ2rs0pkXEOid5unCcxNaZZgw882edy365NTmDiCm1ZS16ilxyv64mV_y)

### **Follow the program flow**

Nowadays, every debugger will pause to check the program state whenever a breakpoint has been reached: it’s a basic and universal tool. But there is another feature that programmers find very useful. It’s called **Step By**.

Once the execution of a program has been paused and all the variables inspected, you may want to know what happens when the execution continues. In case you are using software like React Native, Xamarin, Unity or Ionic the behaviour it’s similar.

Let’s take an example. Imagine you’re building your app and you need to debug this piece of code, which takes a random integer and returns a different number depending on whether the value is odd or even.

![](https://lh3.googleusercontent.com/6SnpEoMYsWMj6r5YacbNq1SgjJo7dRwxY1A_xY8bdDXXgx1VKg1JxMhwWn_STnJiG3-8eFirfbQM_INE7eTAzCxoErN96nHMX1y0UELogX7TG9PBgl_HXHRsjPIkPp9qPVk0VdGx)

This code is returning unexpected results, so you start your debug session by setting a breakpoint at line number 53 to check whether the random integer is correctly returned.

![](https://lh6.googleusercontent.com/3JIv6plxyjBDr5r8sqgunq1-LqfxyT94F3CIQluMBWGvXL4fx1ETfgWV9QYuL4mRSNCgseIaJeyE93QsTutZKDWRXiT5AK5C0xoNFcIL3NBnsQ2kVPBLPsf2KjpxpgMFvo8V5cwX)

Now you know that _randomNumber_ has a value of 5, you probably want to ensure that your program execution continues into the ‘if’ sentence of the following ‘if-else’.

To do this, you could set new breakpoints in the lines 54 and 56, resume the execution and inspect the values all over again.

But there’s a more efficient way. The debugger allows you to “navigate” step by step in the execution of the program.

Just press the **Step Over** button and check how your execution has advanced to the next line that will be executed.

This is how you do it in Xcode.

![](https://lh6.googleusercontent.com/N2hT2zc3zRb5AsIZ6F_UYE2_hV_bUWxRMjgJ-z7_iad4eqtdXuu3F7SL0Va-ncTYmNpCuE0foH6D5hfIm4XFOlRZgXaCn8v0GvgRBll3iLoJgOGUG9A0pnyf-9ZtPGZG0hr5sjgh)

Unexpectedly, the code has continued into the ‘else’ function, which indicates that the function “_isOdd()_” contains a bug.

This example can seem pretty trivial with one ‘if-else’ condition. But imagine a ‘switch’ sentence with 30 options or an ‘if-elseif-elseif-…-else’ chain. You don’t want to set 30 breakpoints or conditional breakpoints.

But there’s more.

The Xcode/Android Studio debugger doesn’t just allow you to Step Over into the next line. You can also go inside a function, **Step Into** , or advance to the next line outside the current method, **Step Out.** Or you can use the Xcode debugger commands directly or Android Studio LLDB.

If you’re like most developers, you’re probably working against the clock (with the [sheer number of apps being released every day](https://www.statista.com/statistics/276703/android-app-releases-worldwide/), tight deadlines are a fact of coding life). But it’s still worth taking these panes in Xcode and Android Studio and experimenting with them. They look the same and they are the same. You will be surprised at how much time you save once you’ve mastered these pretty basic functions of the debugger.

![](https://lh6.googleusercontent.com/G_JnWeBUvIVd8EkIsHE7i1f8hgA5XNcCgijJDVLTtxszntZy5b4jx4XXbum8gLhzAzPKXMk9RycslY2KaYzy__yYrLAqHOTbAoIa1uin7tzoivsBqEz7SqcMpNPEAk1ClUON5Cx8)

![](https://lh3.googleusercontent.com/5N6-Z0pOBlF-saAaWx96wev-M20KrccvffYeKL02z1_fJBGmHRWAoHNjMfEhf8qt_DCkRh2lcMlbinxx6KRE8Qks-3eNcVPzTKUctYi0Q4G-x5PKTYKBa0JP3I6mQLG2KbvRhSBd)

## **What’s next?**

Ok, that’s the end of our introductory breakpoint tutorial. But don’t worry, there’s plenty more to come. Inspecting a piece of code is just the most basic use of your debugger. There is so much more you can do.

In the next chapter we will continue talking about breakpoints, but we will also immerse ourselves in a totally new world. We’ll pause the execution of the code when a condition occurs, change the value of a variable during execution, trigger a breakpoint when a method from an internal class is called, attach debugger to an android process and iOS and Android remote device debug.

See you soon. Happy debugging!