Alibaba Android interview analysis: tracking and analysis of android application crash (crash) issues, Alibaba Security Android

Source: Internet
Author: User

Alibaba Android interview analysis: tracking and analysis of android application crash (crash) issues, Alibaba Security Android

I. Problem Description
A Crash (Crash) occurs when a client program exits the application when it encounters an exception or error that cannot be handled during running, please refer to the causes and solutions of the crash, and how to capture and analyze exceptions after the crash occurs to provide your own solutions.
Taking the Android platform as an example, we will introduce how to capture the flashback information of Android applications to help us locate and solve the code that causes the flashback.
Ii. Flashback in Android
Before explaining the crash in Android, let's take a brief look at the exceptions in Java.
1. Exceptions in Java
Shows the exception hierarchy in Java:


We can see that the Throwable class is the base class in the exception level. The Error class indicates an internal Error, which is beyond our control. Exception indicates an Exception. RuntimeException and its subclass are non-checked exceptions. Such exceptions include ArrayIndexOutOfBoundsException and NullPointerException, we should avoid the occurrence of non-checked exceptions through conditions such as condition judgment. IOException and its subclass are checked exceptions. The Compiler checks whether we provide an exception processor for all checked exceptions that may be thrown. If not, an error is reported. For non-checked exceptions, we do not need to capture (of course, Java also allows us to capture, but what we should do to avoid the occurrence of non-checked exceptions ).
After learning about this, we can probably know that no matter what causes the Android app to crash, we can handle the exception as long as we can catch the exception that causes the crash. For checked exceptions, it is better to say that the number is relatively small and the compiler forces us to ensure that we can set an exception processor for all checked exceptions that may be thrown in code. However, for unchecked exceptions, it is difficult to predict what exceptions will be triggered by code in some extreme situations (such as poor SD cards and poor network speeds, therefore, it is difficult to capture all possible unchecked exceptions. The good news is that Android provides a mechanism for us to obtain the flashback information of Android applications. Let's introduce it as follows.
2. Capture the crash exceptions
An UncaughtExceptionHandler object exists in the running Android process (the exception processor is not captured). We can use the static method setDefaultUncaughtExceptionHandler of the Thread class to set the UncaughtExceptionHandler object of the current process. When an uncaptured exception occurs, the system calls the uncaughtException method of the UncaughtExceptionHandler object of the abnormal process, that is to say, we can obtain the exception information that causes the crash in this method before the Android app returns. UncaughtExceptionHandler is an interface that defines only one uncaughtException method.
Based on the above analysis, we know that as long as the uncaptured exception processor of the current process is set as our custom UncaughtExceptionHandler instance, we can use the UncaughtExceptionHandler method to obtain detailed information about the exception that causes the crash before the crash. In the following code, the custom uncaptured exception processor implements the UncaughtExceptionHandler interface:

Public class CrashHandler implements UncaughtExceptionHandler {@ Override public void uncaughtException (Thread t, Throwable e) {try {// The parameter e passed in this method causes the crash application exception, we can obtain the Exception information here, upload the Exception information to the server for unified analysis, or save it in the file system} catch (Exception e ){...}}}

After the custom uncaptured exception processor is designed, you only need to set it as the uncaptured exception processor of the current process. The related code is as follows:

    Thread.setDefaultUncaughtExceptionHandler(new CrashHandler());

3. Third-party solutions
There are many mature third-party solutions for crash Analysis and Processing of Android applications, such as buugly and ACRA. If you are interested, you can learn about them on your own.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.