Programmers will never be able to run away from debugging code. At some point, you won’t even realize that you’re debugging because it’s just a part of coding and testing.
Debugging is when you identify a bug and then through your code, try to identify and fix the problem.
If you’ve been working on an app that is buggy, or if you have a 3rd-party app that is throwing errors, you may be wondering how to debug the code.
If you’re staring at your code for hours wondering where the problem is, you may want to use a debugger to identify where the problem is coming from.
In this article, we will show you the basics of debugging an APK app through Android Studio.
Debug Mode on Android Device
To start debugging an app, you have to make sure the device you’re testing on has USB debugging enabled.
If your Android version is lower than 4.2.2, check out this article on how to enable debugging mode. If your Android device is 4.2.2 and later, follow these instructions:
To do this, go to “Settings” on your device. Then scroll down to “About.” Find the “Build Number.” Tap the “Build Number” repeatedly until you unlock “Developer Mode.”
Now in your settings a new section called “Developer options” should appear.
In this section, make sure “USB debugging” is enabled.
You will need to install a USB driver onto your device if you don’t already have one. Go to your device’s manufacturer to download the driver. Here’s a handy list.
Then, connect your device to your computer via USB.
Debug Mode in Android Studio
After your device is connected through USB, open up your APK app in Android Studio.
On the top menu bar under the “Run” dropdown, click on “Debug…,” or just click the Debug icon.
A “Choose Device” window will pop up. Select your device from this window.
Can’t find your device? Here is a helpful article to help you troubleshoot this.
Android Studio will launch the APK app in debugging mode. A Debug panel will appear at the bottom with two tabs, ”Debugger,” and “Console.”
Next, we will use breakpoints to diagnose where our problem code is.
Sometimes errors don’t jump out at you. Sometimes it’s the hidden semicolon you must find.
If you can’t find the mistake by looking at the code, try using breakpoints to get the debugger to locate the error for you.
By setting a breakpoint in your code, you’re telling the debugger to stop executing the code at that line. When you’re looking for a mistake, breakpoints are helpful in determining which chunk of code throws an error.
To set a breakpoint in Android Studio, go to the line where you want to break execution and click on the gray sidebar to the left. A red dot will appear to represent the breakpoint.
Next, run the app by clicking the Run icon or by going to the top menu bar and clicking “Run…” under the Run dropdown.
The debugger will execute the code line by line until it stops at the breakpoint. Then the debugger panel will show the status of your code.
Then you can get to debugging.
After the debugger reaches the breaking point, you can use the blue arrows in the Debug panel to get the status of your code line by line.
There are three blue arrows. One pointing down between two gray lines is called Step Over. When pressing this button, the debugger will execute the next line of code after the breakpoint.
The second arrow is called Step Into. It’s represented by an arrow pointing towards the bottom, right corner. Pressing this button will have you jump into any method call on the line.
The last arrow is called Step Out. This button is shown pointing towards the top right corner. When you press this button, the debugger will finish going through the current method and then go back to where that method was called.
Debugging Never Ends
Debugging is a big part of a developer’s job. You may have used other debuggers in the past for different projects. One example would be Chrome DevTools.
However, app development is getting more popular and you should know how to debug your APK app.
It all happens in Android Studio. Make sure your device has USB debugging enabled, has appropriate driver installed and is connected to your computer.
From there you are able to put Android Studio into debug mode, place breakpoints and start debugging.