Learning Android Development

When your Android Studio breakpoint doesn’t stop

List all possible causes that your Android Studio breakpoint is not stopping

Elye
Elye
Jan 10 · 6 min read
Image for post
Image for post
Picture in Pixabay by GoranH

“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.

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

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

buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

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.

Image for post
Image for post

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.

<service android:name="com.example.myapplication_sync.MainService"
android:process=":anotherProcess" />

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 process=":sync"

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.

Debug.waitForDebugger()

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.

Image for post
Image for post

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.

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.

Image for post
Image for post

Now you can then attach your debugger onto it, and start debugging.

Image for post
Image for post

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.

Mobile App Development Publication

Sharing Mobile App Development and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store