Official Letsdiskuss Logo
Official Letsdiskuss Logo

Language



Blog

rahul shuklamgs

Mentor of Change at NITI Aayog | Posted on |


DEBUGGING IN ANDROID STUDIO

0
0



Begin troubleshoot mode

When you need to begin the troubleshooting mode, first ensure your gadget is setup for investigating and associated with USB, and open your task in Android Studio (AS) and simply tap the Debug symbol Debug Icon. At that point select your gadget in the Choose Device window, and Android Studio will dispatch your application in the troubleshoot mode. Android Studio will likewise naturally open the Debug device. You can turn it on physically by clicking Debug! at the base of Android Studio. Another approach to begin investigating without restarting your application is by tapping on "Connect debugger to Android process" . You likewise need to choose the gadget and the application (process) you need to join the debugger to. This choice is additionally exceptionally helpful on the off chance that you need to troubleshoot an explicit component, and you need to experience two or three different screens meanwhile. Since a few tasks run slower in the troubleshoot mode experiencing the application without the investigate mode on is a lot quicker. When you are a single tick far from the thing you need to investigate, join a debugger to your application and begin troubleshooting in that spot.

Investigate utilizing Logs

The least demanding approach to troubleshoot your code is to utilize Log. This is an utility that enables you to send log yields, which you would then be able to see in Logcat in AS. You don't have to run your application in the investigate mode to do this. Just use android.util.Log in mix with one of the accompanying strategies: Log.v(), Log.d(), Log.i(), Log.w(), Log.e() or Log.wtf(). Every one of them use 2 string parameters: TAG and your message. You can likewise pass Throwable as a third parameter, which will log your TAG, message and exemption log. By utilizing diverse techniques — dimensions of verbosity (VERBOSE, DEBUG, INFO, WARNING, ERROR) — you can later sift the logs through or even set up AS and redo the Logcat content hues and content foundation for each dimension.

Here's a straightforward model:

private static last String TAG = "MyActivity";

...

button.setOnClickListener(new OnClickListener(){

@Override

open void onClick(View view){

Log.d(TAG, "Tap on catch");

}

}

Verbose ought to never be gathered when you are doing the discharge work of your application, however you can do this amid the advancement procedure. Investigate logs are kept out at runtime, while Error, Warnings and Info logs are constantly kept in. Along these lines, in the event that you utilize any of these logs for creating purposes, keep in mind to expel them particularly on the off chance that you log some delicate information. Even better, wrap these logs into an if-explanation.

For instance:

on the off chance that (BuildConfig.DEBUG) {

Log.d("Sensitive message");

}

Or then again make your very own BuildConfig field and set it up in a gradle document.

Logcat

At STRV we utilize the Logcat utility, which handles if-explanation in addition to other things. Look at it here. In build.gradle we have construct config property LOGS, which can be set to valid in the troubleshoot fabricate type and to false in the discharge type. Logcat dependably check the LOGS property before logging any message. At that point you don't need to stress over enclosing it by any if-proclamation.

Just call the technique:

Logcat.d("Your message here");

You can likewise set different parameters in this class. The message can incorporate "Code area". This implies it shows the name of the technique, the line of code and furthermore the string where Logcat is called. Another cool thing about Logcat is that you can log designed messages. So rather than:

Logcat.d("Message with list: " + someInteger)

we use:

Logcat.d("Message with list: %d", someInteger)

This strategy enables you to have the same number of parameters as you need.

Breakpoints

When you are at the point where you can't settle a bug just by taking a gander at your code, it's a great opportunity to utilize breakpoints. Breakpoints enable you to stop the execution of your application at a specific line of code.

Setting up breakpoints

Simply go to the record that you need to troubleshoot, discover a line of code where you need to begin and tap on the sidebar to one side of this line. You will see a red dab there. At that point run your application in the troubleshoot mode or join a debugger (see above).

Debugger

You can likewise tweak your breakpoint designs. Set up your breakpoints by right tapping on the red dab and after that input a condition when you need to flame this breakpoint. It's exceptionally helpful when this bit of code is called on numerous occasions however you need to stop at specific point. You can likewise open the breakpoints settings — View Breakpoints — on the left half of the Debug device window. You will have the capacity to see all breakpoints, incapacitate/empower each breakpoint or select one breakpoint from the rundown and design it. For instance, you can debilitate one breakpoint until another breakpoint is hit and after that evacuate it once it's hit.

Troubleshoot with breakpoints

Android Studio will feature whatever line of code the application stops at when a breakpoint is hit. The crucial step begins in the Debug window — looking at each factor that is at present designated and going line by line or hopping to another breakpoint. Extend the tree of an article, and you will have the capacity to see all its field esteems. In the event that it's a custom article, it will demonstrate indistinguishable incentive from the toString() technique. On the off chance that you are keen on a specific field in this article or you need some simple to-peruse result, supersede the toString() strategy. You won't have to open the tree, you will see the outcome beside the base of the tree.

The cool thing is that on the off chance that you have a bitmap you can likewise observe the picture while investigating.