1, the preface

For a business project, quality should be the lifeblood of the r&d students.

There was a massive crash or unavailability on the line, and it was just too beautiful to imagine. This is also the reason why any commercial project will spend a lot of effort on performance optimization and high availability when it gets big. In this process, various APM tools and HotFix solutions have been born to ensure performance to a certain extent and provide a guarantee line for emergency repair.

Here’s a question: Suppose an application that has been through the process of gatekeeping still has problems online, and HotFix doesn’t work, is it doomed?

2. The origin of safe mode

In a nutshell: Avoid continuous, severe failures during startup when your application crashes and HotFix fails to take effect.

Here is an example: suppose an Application crashes during startup because of an error in the Application, and the hot fix has not yet been pulled, then the Application will fall into a serious situation of continuous startup crash. The ultimate fate must be uninstalled by the user.

This is where the safe mode for application startup comes in.

3, safety mode of thinking

It is important to be clear that any technology serves a specific business scenario, and the safe mode for startup is to solve the serious situation of startup crash without HotFix.

Let’s consider the following questions:

3.1 What causes startup crashes?

Nowadays, each App has developed in business for many years, and the technological innovation of mobile terminal has been carried out for many times, so the application needs to do more and more things in the start-up stage, and the causes of startup collapse may be as follows:

  • Copy or operation failure of various files including but not limited to database and XML;
  • Various network requests deliver dirty data;
  • Dirty data caused by downloading and merging various resource packages, including but not limited to flash screen, Zip package, repair package, etc.
  • Dirty data caused by users upgrading to the latest version of the App from a lower version that spans more than N versions;

It can be seen from the above that the application is not safe in the startup phase, and a problem in any of these links can lead to a serious accident.

3.2 When does the Security Mode Take Effect?

UncaughtExceptionHandler is set for the main thread to catch run-time crashes. It is tempting to associate the safe mode with UncaughtExceptionHandler, but this approach has significant limitations: The inability to do anything about Native abnormalities is obviously not accurate enough;

So let’s go the other way:

  • A crash number is recorded when entering the application. After meeting certain conditions, it is considered that there is no abnormality in the startup stage, and the crash number is reset to the initial state.
  • When the number of anomalies reaches a certain level, it enters the safe mode.

Need to maintain a crash count:

  • Add crashes +1 when entering the app.

Reset crash times if certain conditions are met:

  • The user exits the application normally.
  • The user opens the app for 10 seconds;

3.3 What can Safe Mode Do?

  • Perform local self-repair tasks on the client, for example, delete some caches or hot repair packages or other resource packages.
  • Clear the entire App data and reset it to the initial installation state;
  • Blocking the process, prioritising the execution of preset tasks, such as requests and running hot fixes, and waiting for the completion of the normal process;

4. How to design a safe mode library?

4.1 What capabilities should secure mode provide?

  • Abnormal startup detection and classification strategy: detect APP startup abnormalities, and know the level of abnormalities in fine granularity;
  • The ability to apply self-healing;
  • The ability to perform synchronous hot repairs;
  • Support to get detailed crash information and crash callback;

4.2 Design of expansibility and ease of use

  • scalability:
    • For all apps, the processing of security mode is common, but there are always scenarios that need to be customized, so security mode should be able to implement customized policies;
  • Ease of use:
    • App can be quickly accessed and policies can be quickly verified;

4.3 Overall Flow Chart

5, other

This article is from the point of view of designing a library to think about the application startup continuous crash processing, now I am very close to recommend a library about startup protection for you:StartUp-Protector: (Github.com/liuzhao2007…), simple and convenient to use, low invasion, perfect function, strong customization, welcome to use:

  1. Crash detection and classification policy: execute level 1 security mode for two crashes, execute level 2 security mode for three crashes;
  2. Provide self-repair capability, you can customize the processing policy to enter safe mode;
  3. Provides blocking process capability to perform synchronous hot repair;
  4. Provide detailed crash information and crash callback capabilities;
  5. Customize post-crash policies, such as ignore policies for restarts;
  6. The ability to provide rapid regression;

Welcome to pay attention to wechat public number: regularly share Java, Android dry goods!