Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio threaded debugging

I've been having trouble debugging a multithreaded app with Android Studio 1.1. It seems as if when a breakpoint is hit all other threads also stop, not just the one with the breakpoint. I created a simple test app with the following method in the Activity's onCreate.

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      Thread a = new Thread("thread-a") {         @Override         public void run() {             Log.v("thread", "thread-a");         }     };      Thread b = new Thread("thread-b") {         @Override         public void run() {             Log.v("thread", "thread-b");         }     };      a.start();     b.start(); } 

I set breakpoints at the Log.v lines in thread-a and thread-b and then I run it in debug mode on my Lollipop Nexus 5.

When the app starts it hits the breakpoint in thread-a but the first problem I notice is that the app's UI is blank as if the main thread is paused. Next I went to see that the breakpoint in thread-b is also hit so I pull up the Threads view in Android Studio's debugger but when I go to expand the thread-b arrow there's nothing there. When I expand the main thread it shows it is paused somewhere in onStart().

Android Studio screenshot

Am I doing something wrong or is this debugger incapable of debugging multiple threads at once?

like image 708
miguel Avatar asked Mar 25 '15 00:03

miguel


People also ask

What is thread debugging?

A thread is a sequence of instructions to which the operating system grants processor time. Every process that is running in the operating system consists of at least one thread. Processes that have more than one thread are called multithreaded.

How are threads executed in Android?

When an application is launched in Android, it creates the primary thread of execution, referred to as the “main” thread. Most thread is liable for dispatching events to the acceptable interface widgets also as communicating with components from the Android UI toolkit.

What is UI thread in Android?

User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.

How do I debug my Android app in Android Studio?

Android Studio enables you to debug apps running on the emulator or on an Android device. With Android Studio, you can: Select a device to debug your app on. View the system log. Set breakpoints in your code. Examine variables and evaluate expressions at run time. Run the debugging tools from the Android SDK.

What is Android Debug Bridge and how to use it?

Android Debug Bridge (adb) is a versatile command-line tool that lets you communicate with a device. The adb command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device.

What are the different debug types available in Android Studio?

The debug types available include the following: Auto: Select if you want Android Studio to automatically choose the best option for the code you are debugging. Java: Select if you want to debug only code written in Java or Kotlin—the Java debugger ignores any breakpoints or watches you set in your native code.

How to pause all threads at once in Android Studio?

In IntelliJ IDEA (and Android Studio is based on IntelliJ), when you place a breakpoint, if you do right click over it a dialog is displayed and you can select whether to pause all threads (the default) or only that thread. You are pausing all the threads as it is the default setting.


2 Answers

In IntelliJ IDEA (and Android Studio is based on IntelliJ), when you place a breakpoint, if you do right click over it a dialog is displayed and you can select whether to pause all threads (the default) or only that thread.

You are pausing all the threads as it is the default setting.

like image 164
Mister Smith Avatar answered Sep 22 '22 16:09

Mister Smith


In Android Studio, it's possible to specify whether a given breakpoint will suspend execution of just the executing thread (that triggers the breakpoint) or all threads. This is on a per-breakpoint basis (i.e. some breakpoints can suspend all threads, while others only suspend the current thread).

Right-click on a breakpoint to bring up the Breakpoint Properties window:

Breakpoint properties

Note the 'Make Default' option which allows this to be defaulted for all newly created breakpoints.

If only the current thread is suspended (option "Thread" in the image above), then the stack-frame of other unsuspended threads will not be visible in the Frames window:

Frames not available for unsuspended thread

like image 32
CJBS Avatar answered Sep 21 '22 16:09

CJBS