Learning Android Development
When your Android Studio breakpoint doesn’t stop
“I put a breakpoint there, and it doesn’t stop!” Why? Even after 6 years of mobile development I still faced this, unbelievable!! Such an embarrassment?
Well check it out, see if you know all the possible reasons that can potentially cause the breakpoint not stopped?
Starting from the simplest one first
1. Is the line of code reachable? It’s crazy but true.
I know this sounds crazy. But trust me, at times the particular line of code is never reached as we are too engrossed in debugging the issue, without realizing the code is never reached. Check this StackOverflow.
There could be various reasons, e.g.
- We put the code in a different class that looks the same
- We are running on another variant of the app that is not attached to the debugging
- We are running on another device that is not attached to the debug
Hence the debug breakpoint never stops over there.
If you can’t get to it, just use
Log.d to make sure it can be reached first.
2. Is the Breakpoint Enabled
This probably is simple if you are familiar with Android Studio.
But in case you are not, below is the breakpoint icons indication.
For the first two, that means they are all good. Sometimes due to Android Studio glitched, the breakpoint is not shown attached but could be okay.
3. Are you running on Debug Mode?
Unlike iOS Xcode, in Android Studio, to debug your app, you’ll have to run it in Debug Mode.
As shown in the diagram above, make sure you are running the app by pressing the Debug icon. If you forget, you can still attach the debugger later.
4. Are you running on Debug Build?
To debug, you need to ensure that you run your app on Debug build Variant as shown below
Technically, even if you run it on release build, it will still stop and break on your breakpoint.
You might not able to watch the variable value or evaluate any expression properly. This is because by default the release variant is set to minify enabled, and proguarded (code if obfuscated). Check out this StackOverflow
5. Is the Breakpoint Enabled on the Thread?
This might be the most hit issue by many.
In Android, we can run our code on the main thread or background thread. If I’m not mistaken, the debug breakpoint will only break on the main thread.
Hence when you put a breakpoint on the code that is run on the background, it will not stop.
To ensure your breakpoint stop in all thread, go to your breakpoint setting (right-click on the breakpoint), set it to All instead of Thread as shown below.
Check out this StackOverflow.
6. Is it running on a different Process?
This will be the one that makes one pulling the hair till it comes off, still won’t work if you don’t know about it. There’s no indication in Android Studio this breakpoint set will not stop. This is regardless if you have set it to All thread or not.
In Android, our app normally runs on the main process, regardless of the main thread or background thread.
But on some occasion, we can have part of our app works on different process, so that it will not die when the application gets terminated. e.g. the below service is indicated as another process.
A used to popular framework for this process is, SyncAdator. If you check the document, you will be able to see that it is setting to
How do we have a breakpoint over it?
In order to debug it, you’ll need to add an extra code where you want to start your debugging (just before the breakpoint perhaps). Refer to this StackOverflow.
After running your app, and when the code has reached that line, it will stop waiting for you to attach the debugger.
When you just click the attach run to debugger icon as shown above, and select the process you want to attach.
That’s it. You’ll get the code to stop at the breakpoint of that different process.
If you like to get a design to experiment this out, check out the
SyncService of the design below.
Replicate Android Joda Time (2.10.9) issue where after App Startup is added, the Joda Time doesn't work on different…
Note: remember to remove that
Debug.waitForDebugger() before you release the app.
If you don’t like to add code, you can also use the below approach
7. Debug something that doesn’t launch through the normal flow.
This is another tricky one if you don’t know how to do it.
Imagine if you want to debug your code that launched from a deep link or a push notification. You cannot launch the app in debug mode first, as that is different from launching from a push notification or deep link.
The good news is, you can use the approach in the debugging different process above using
Debug.waitForDebugger(). But it is quite a hassle.
So another handy approach is to use the below command.
adb shell am set-debug-app -w --persistent <your.app.package>
This will permanently set your app to wait for a debugger to be attached before proceeding.
When you launch your app (through deep link or push notification), it will show the below dialog.
Now you can then attach your debugger onto it, and start debugging.
Also, remember to remove the wait for debugger setting in your device using the command below, else it will be there as long as the phone is on.
adb shell am clear-debug-app <your.app.package>
To get a little more insight into this approach, check out the below article.
Hope this helps you in your debugging.
Let me know if you find other issues that the breakpoint doesn’t stop that is not listed above.