Is there any way to have conditional Debug in android studio?
for example I wanna go to debug mode when a specific variable value is.
for example:
for(MyClass myclass: ClassList){
String title = myclass.gettitle(); // this is where break point is
}
Before that I do not need the debug mode. I just wanna examine the app on that exact value. for example when title="KickBoxing" in this case I should check the variable value and if it is not my required value I should press F9 to go to next value. maybe this takes 50 times to get to my desired value.
It is possible to add a condition for a breakpoint. Add a breakpoint, as you normally do, and click with the right mouse button on it. You will be prompt with a dialog. In the condition you can put some java code.
E.g. you could put "kickBoxing".equals(title)
this way the debugger will stop on that breakpoint only when the condition is true.
(photo taken from http://javafortesters.com/)
You can do that with Android Studio:
Set a breakpoint on the line you need.
Right-click on the breakpoint
Paste your condition into the field named Condition
For example, your condition should be like myclass.gettitle().equals("KickBoxing")
Related
I am having a problem with a project in android studio.
I don't know which function is changing my variable value and I want to see what's responsible for changing it.
Is there any way to see which function/constructor does modify the variable ?
I have already added the variable to the Watches, but that only shows when the value changes. So is there any way to see the function that does change it specifically after it has another value ?
In Xcode I am able to "po" any values while debugging. Is there a way to do this in Android Studio?
For example, if I hit a breakpoint and want to print out certain values in realtime as opposed to having to use Logs in my code?
You can do the following steps:
1- Set BreakPoint in the line you want to evaluate Ctrl+F8.
2- Run your code in debug mode or press Shift+F9.
3- When reached to the BreakPoint press Alt+F8 or click on the following button.
4- Then type your value in text-field and click on the Evaluate button.
References:
https://www.jetbrains.com/help/idea/debugging-code.html
https://www.jetbrains.com/help/idea/evaluating-expressions.html
Use Watches. It gives you the ability to evaluate expressions.
When you hit debug point, there are two ways to check values :
(1) Add Watch for any variable
(2) Hover over any variable, the value will be displayed there
If it's a c/c++ project, you still can use lldb command in Android Studio.
Here's the official document. When you hit the breakpoint, you will see a tab named "lldb" next to the variables tab.
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!
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.
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.
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.
Keep debugging and you should see the variable from the Watches window frame update when appropriate based on your code.
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!
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!
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();
I know I could set a breakpoint at every line where my code changes the variable, but is there an option such as right-clicking a variable (to "Add to Watches") that would stop when a variable changes value?
I think C++ has this option. See this.
And Eclipse? See this.
Is this implemented in AS?
You can break on value changes of some variables in Android Studio 1.1.0.
Android Studio calls them 'Java Field Watchpoints'.
From the breakpoints windows, (Run -> "View breakpoints...") or Ctrl+Shift+F8,
you can add "Java Field Watchpoints" from the plus in the top left corner, and then select your class and variable.
If the goal is simply to set a watchpoint where execution halts if a variable's value changes, there are two methods:
Pressing Ctrl-shift-F8 to set a watchpoint brings up this dialog:
That looks more powerful but it's confusing because it lacks the option I need; and there's an extra step: filling in the dialog box below:
Once you fill it in correctly, you get the same options for stopping execution as in the simpler method below:
Define the variable on one line ending with a semicolon.
Left-click in the area where you set a normal breakpoint and up pops a dialog box.
Choose options to suit your needs.
Any instance where the value of the variable prefDbExists changes, execution will pause as at any "normal" (unconditional) breakpoint. And that's all I needed when I asked the original question.
EDIT: Fully qualified class name is {package name}.{class name})
What you seek for is commonly known as "WatchPoint".
You will find detailed answer and examples on the doc. page of JetBrains - IntelliJ - Idea which is the basis for Android Studio.
https://www.jetbrains.com/help/idea/2016.3/creating-field-watchpoints.html
For me this works perfectly !
Add a Kotlin Field Watchpoint by clicking on the space next to the line number for the field declaration in Android Studio. Select Kotlin Field Watchpoint from the Set Breakpoint menu. You will now get a breakpoint whenever that value changes.
Is there any way to get the eclipse debugger to report the intermediate return values of the functional subterms of a complex android expression without declaring variables for every subterm?
For example, in the following code I would like to know the return values of each function (getPaddingRight, getPaddingLeft etc.):
if (tview.getPaint().measureText(q_text.toString(<=tview.getWidth()-tview.getPaddingRight()-tview.getPaddingLeft()){
//take actions
}
Edit: if you mouse over the q_text or tview you get the properties for that object, but not the return value of the function. You can just look up the value for getWidth for example by looking at the mMeasuredWidth property of the tview object, but I'm wondering if there is a general solution for the return value of any function.
I finally found an easy way to do this:
Go to eclipse debug view (Widow->Open Perspective->Debug)
Click on the expressions tab (Top right mini-widow, 1 of 3 tabs Variables, Breakpoints, Expressions)
Enter the value of your function or variable or whatever, click on it and the resulting expression will be displayed.
Note: If the tab "Expressions" is not in the upper right window then right click on one of the variabls in the "Variables" tab and click on the "Watch" menu entry and the "Expressions" tab will appear.
Using Helios Service Release 2