Is it possible to watch variables at runtime?
Asked Answered
T

3

17

I know the basics of debugging, and I know I can add watches to a variable that stop the program's execution on a given condition. But I didn't want to stop the program every time I need to see the value of a variable. Neither I want to log the value of every relevant variable into logcat... I only wanted to see their values like I do at breakpoints, only in runtime.

I'm programming Android, in Android Studio.

Thanks for the help!

Topping answered 16/10, 2015 at 14:29 Comment(2)
no log no break point i think it not possible or you have to put somewhere textview and update that value run time that will show you variable value..but why this type of requirement?Leeway
Thanks. It's kind of a personal approach on debugging, I just wanted to know if it was possible. As I said, assigning log to every variables that I need to know can be stressful in a tight schedule. Plus I just got into my current job, and I got to support a software I didn't built... IMO the approach I suggested might be productive. I'm not a senior professional, so I might be wrong, after all.Topping
V
14

When your program has stopped on a breakpoint click the icon at the far right of the debugger menu (see image below). You can type in methods or variable names into this window and see what they would be.

enter image description here

You can type any expression you like (as long as it is within the scope of where you broke your code) and input any hard-coded values or objects all without re-running your project.

enter image description here

To add a variable to your watch list

Start by putting a break point in the class where you'd want to watch a specific variable. Run the code and once it hits your breakpoint from the Variables window frame you should see all of the variables that are accessible. Simply choose the one you'd want to watch and then right click and choose "Add to watches" from the drop-down.

enter image description here

Keep debugging and you should see the variable from the Watches window frame update when appropriate based on your code.

enter image description here

Vtehsta answered 16/10, 2015 at 14:36 Comment(7)
Thanks for your answer. I know about the expression evaluator, I just wanted to, like, use it when the program is running, not stopped at a breakpoint. That's the kind of approach I wanted to go to. Sorry if I wasn't clear enough.Topping
I've updated my answer to include watching variables. I'm afraid that's as close as you're going to be able to get to what you are after.Vtehsta
My debugger shows me only the this variable. No method variables are presented. any suggestion why?Shay
When searched in the expression evaluator on mac it is showing "Cannot find local variable 'xxxxx'"Zigrang
@developer1011 make sure testCoverageEnabled in your application build.gradle file is set to false.Vtehsta
can you suggest where i might place a breakpoint such that it's in the UI thread? there is no specific place in the code I want to watch some values, I just want to know the value whenever i say, regardless of what code is running at the time. In fact, I suspect most of the time I want to look at the watch the app will be idle - how do you set a breakpoint for that???Deadfall
@Topping That does not answer your question. My answer supports watching values at runtime. https://mcmap.net/q/706817/-is-it-possible-to-watch-variables-at-runtimeOas
O
3

YES you can!

According to the Android Dev Summit '19, you can easily do that by disabling the Suspended flag in your breakpoint.

Then you can evaluate a log message to the console every time it gets the breakpoint, without suspending!

enter image description here

As you can see, my app fires a log to the console every time it gets to my breakpoint.

In other words, you can view variable changes at run time!

Oas answered 19/1, 2022 at 16:22 Comment(1)
Never stopped to see that feature. It is going to help me from now on, thank you for that!Topping
K
-2

If you know the basics of debugging, you can easily add watches to a variable that stop the program's execution on a given condition. If you didn't want to stop the program every time you want to see the value of a variable then the easy way to see the value of a variable is to use Toasts. A toast provides a sample value of any variable in an operation in a small popup. Toasts automatically disappear after a set timeout.

A simple code example:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

In order to see the variable value in a Toast:

   int var=1;
   Toast.makeText(getApplicationContext(), "vlaue is "+var, Toast.LENGTH_LONG).show();

In order to see the variable type in a Toast:

   Toast.makeText(getApplicationContext(), "type is "+var.getClass().getName(), Toast.LENGTH_LONG).show();
Karl answered 30/7, 2018 at 16:4 Comment(1)
That is a creative and practical way of solving the issue, thank you! My question at the time was if there was a way to do that in the IDEs (even though I didn't specify that in the question), but I think this is not very relevant right now. Again, thanks for your tip! I will remember that next time!Topping

© 2022 - 2024 — McMap. All rights reserved.