Android Debugging and Logging
What we will cover?
- Ways to enable Debugging on Android devices.
- Logging and useful commands
If you’re learning and already into Android Application development, this blog will be useful for you.
Isn’t it a fun to be able to debug easily no matter how much complex filter it is?
So let’s get going!
Ways to enable Debugging on Android devices
a. USB Debugging
b. Debugging over WiFi
a. USB Debugging
To enable USB Debugging, first we will need to enable “Developer Options”. This option doesn’t appear by default, so to bring it, open Settings app. Then at the bottom you’ll see “System” option. Tap on “System” then Tap on “About Phone” and Scroll Down and look for “Build Number”. Once you find it, Tap on it 7 times. A screenshot for reference:
After tapping 7 times, you’ll see a Toast saying “You’re now a developer”. Now press back button and Check Advance Settings on “System” Settings window. You should be able to see “Developer Options”:
Now tap on “Developer options” and enable it on clicking toggle button on top of the Developer Options window. And then Scroll down and enable “USB Debugging” option. A Screenshot for reference:
Now when you’ll connect this mobile device to your PC, you should be able to see your device connected.
Please note that first time it’ll install USB driver of that device to enable debugging from PC.
b. Debugging over WiFi
For debugging over WiFi step “a” mentioned above is needed anyway. Now connect the device via USB with USB Debugging enabled to PC. and open Terminal. And write below commands one by one:
Now If you’ll look for devices; it’ll show the device connected over wifi:
Important: “Note that Android Mobile Device and PC should be in same network for WiFi Debugging”
Now we will move to next topic which we want to cover as well.
Logging and useful commands
There are log levels and are in this precedence order VERBOSE >> DEBUG >> INFO >> WARNING >> ERROR. So if we filter logcat with VERBOSE it will print all logs i.e. D, I, W and Error logs. And if we filter logs with INFO, it’ll print logs from I, W to Error logs and so on. So how to filter logs based on log level ? Check these commands
Now to filter logs with a specific string, we can use below command:
Here “ActivityManager” is the string for which we want to print logs and V is log level VERBOSE.
Now if we want to print everything but exclude logs from some TAGs. To do that:
Above command will print everything and exclude every logs coming from “ActivityManager” and “LocationManager” tags.
In production, we don’t want to enable debug logs, So we restrict that by putting check like this:
Now to print this log during development, we will need to enable the log by adding this tag as log property. Here is how:
There would be a need to share the whole log to fellow developers, so in that case we want to get the logs in a text file. To do that execute logcat command with the filters or without the filters and give the filename preceded by greater than symbol; Example:
However my preference to print logs is always to go to adb shell window and print. Because that way we will be able to use linux command “grep” and we can also keep some logs printing in the background while continue executing other commands be it logs or launching any activity setting property etc.
Hope this article would be useful to readers! Happy Debugging & Logging! :)