This is the 15th day of my participation in the August Text Challenge.More challenges in August

Dealing with bugs in your application is a basic skill of every programmer, and there are a lot of bugs in real projects every day, so learning how to use Logcat, Android Lint, and the built-in debugger in Android Studio is a must.

Exception and stack tracing

Write code, a careless program to write running, do not worry, open the Logcat tool window. Scroll up and down the LogCat window to see the entire red exception or error message. If not, try to select the No Filters filter item of LogCat. In other cases where there is too much information, you can adjust the Log Level bit Error so that the system only outputs logs of serious problems. Or search “FATAL EXCEPTION” directly to locate crash exceptions.

As shown, simulate a crash log:

Note that when Android applications are compiled, Kotlin code is compiled to the same low-level bytecode as Java code, so you shouldn’t be surprised to see java.lang exceptions even if you’re writing in Kotlin.

The above picture shows the exception, we can jump directly from the exception statement to the corresponding line of the source code error, find out the cause of the crash, to correct.

Of course, this is a relatively simple exception, but for really complex projects, looking for the last exception and the first line of the stack trace (corresponding to the source code) in LogCat is the best starting point for finding a solution.

Diagnosing abnormal application

Since some errors do not crash, but do not produce the desired results, we need to print log trace or breakpoint debugging.

Log stack trace logs

Printing logs is also a bit tricky. When we debug a function exception, the TAG can be set to one, so that we can filter the log information and analyze the log.

Github has a great Log output tool for controlling the output of the entire project.

Feel free to recommend two:

The logger: github.com/orhanobut/l…

LogCat:github.com/liangjingka…

To set breakpoints

Breakpoints are also a common way for programmers to debug programs. Breakpoints are stopped one line before the breakpoint setting line, and we can then examine the code line by line.

Then debug and run the project:

Of course, you can also execute the code step by step from the arrow next to it and analyze the code. There is a small computer icon on the top, which is used to do some calculation debugging. It can change some Variables and see some results.

Debug tool control button:

“Recommend” an article devoted to various Android Studio tips, including debugging tips.

Address: jaeger.itscoder.com/android/201…

Summary:

The advantage of printing logs is that multiple stack traces can be seen in the same Log. On the downside, you have to learn how to add logging functions, recompile, run your application, and track down application problems.

Comparatively speaking, the method of code debugging is more convenient. After the application is run in debugging mode, you can set breakpoints in different places while the application is running, looking for clues to solve the problem.

Of course, printing the log will make it easier to find clues for solving problems in different scenarios, such as a loop.

Android specific debugging tools

Using Android Lint

Static Analyzer for Android application code.

Analyze to Inspect Code… The menu item runs Lint manually.

Select Whole Project, and Android Studio immediately runs Lint and some other static profilers to start parsing the code. Once checked, all potential problems will be listed by category.

In general, Lint tools provide detailed information and suggestions for resolution, which you can either resolve or ignore. Really big projects or to see the situation, their own analysis, is not necessary to solve.

Class R problems

Resource compilation errors sometimes persist or appear inexplicably when operations can be tried:

  • Recheck the validity of the XML file in the resource file
  • Build -> Clean Project
  • Sync Project with Gradle Files
  • Running Android Lint (you might find something unexpected)

If you have any unresolved problems, visit the Stack Overfow website or this book’s forum for help! ^_^

Challenge exercise: Explore the layout inspector

Tools – > Layout Inspector

Examine the layout file interactively to see how it renders on the screen.

There is also a useful tool for debugging layouts, SDK ->tools->bin-> UIAutomatorViewer, open this thing, you can also analyze the layout, use it in combination.

Challenge exercise: Explore the Android performance profiler

Android Profiler replaces the Android Monitor tool in Android Studio 3.0 and later. The Android Profiler tool provides real-time data to help you understand your application’s CPU, memory, network, and battery resource usage.

Specific reference: developer.android.com/studio/prof…


🌈 follow me ac~ ❤️

Public account: Ni K Ni K