catalogue
- 01. Functions of the library
- 02. Analysis of the advantages of the library
- 03. How is the library used
- 04. Reduce unnecessary crashes
- 05. Abnormal recovery principle
- 06. Follow-up requirements description
- 07. Principle of abnormal stack trace
- 08. Partial problem feedback
- 09. Other content description
01. Functions of the library
1.1 Functions
- Some questions to think about after an abnormal crash
- 1. Whether to restore the activity stack and the crashed page data
- 2. Save crash information and catch exceptions, whether it is compatible with Baidu bug crash statistics SDK, etc. Easy access
- 3. Whether to return to the activity at the top of the stack (save stack information)
- 4. What information needs to be collected after the crash? Phone info, APP info, crash stack, memory info, etc
- 5. How to exit from abnormal crash in a friendly manner, and whether there will be abnormal data when calling to restart the APP after the crash
- 6. For native code crashes, how to log to the file
- What can the library do
- 1. Flash back crash information can be displayed on Android phones, and crash details can be saved and shared for development
- The main reason is that the test students found the crash in the test, and then ran to the developer to say that because it is not easy to reproduce, the developer did not recognize the children’s shoes…… Sometimes bug statistics are not so accurate!
- 2. For some devices, such as Kindle development, you can set the app to crash and restart
- 3. Exposed the interface monitoring operation of the crash data captured by the user and the crash restart
- 4. Save a crash log to a file, the file naming convention: version + date + exception 】 【 V1.0 _2020-09-02 _09:05:01 _java. Lang. NullPointerException. TXT
- 5. Crash log list can be obtained, support to view log details, and can share, screenshot, and copy crash information
- 6. Collect crash logs, including device information, process information, crash information (Java crash, Native crash or ANR)
- 7. Collect the memory information during the crash (OOM, ANR, virtual memory exhaustion, etc., many crashes are directly related to memory) and improve it
- 1. Flash back crash information can be displayed on Android phones, and crash details can be saved and shared for development
1.2 The following figure is displayed
1.3 Logging After a crash
1.4 Crash Flowchart
02. Analysis of the advantages of the library
- Low-intrusion access to the lib will not affect your other business. Expose crash reboot, and support developers to capture crash data interface! The ability to collect crash log writing files, including device information, process information, crash information (Java crash, Native crash or ANR), and crash memory information into the file file. Users can obtain the crash list, jump to the crash log details page, and can share the crash log, cut off the graph, copy and other operations. It makes it easier for testing and production to provide development with the location log of the occasional bug, so that development doesn’t always acknowledge the occasional release crash… Development always fails to acknowledge…
03. How is the library used
- How do I introduce the library
implementation 'cn. Yc: ToolLib: 1.0.0' / / lot code https://github.com/yangchong211/YCAndroidTool Copy the code
- The initialization code is shown below. It is recommended to initialize…
CrashHandler.getInstance().init(this.new CrashListener() { /** * Restart app */ @Override public void againStartApp(a) { CrashToolUtils.reStartApp1(App.this.1000); //CrashToolUtils.reStartApp2(App.this,1000, MainActivity.class); //CrashToolUtils.reStartApp3(AppManager.getAppManager().currentActivity()); } /** * Allow developers to upload their own crash data *@param ex ex */ @Override public void recordException(Throwable ex) { // Customize crash upload, allowing developers to upload crash data captured by themselves //StatService.recordException(getApplication(), ex);}});Copy the code
- There are three apis for restarting an App
// Start a new service, KillSelfService, to restart the APP. CrashToolUtils.reStartApp1(App.this.1000); // Use to restart the APP[use alarm clock, whole restart, temporary data clearing (recommended)] CrashToolUtils.reStartApp2(App.this.1000, MainActivity.class); // Retrieve the LauncherActivity from the project and set the activity's flag and component to launch the app. CrashToolUtils.reStartApp3(AppManager.getAppManager().currentActivity()); Copy the code
- About getting crash directory API
/ / crash file storage path: / storage/emulated / 0 / Android/data/your package name/cache/crashLogs / / breakdown page screenshots store path: / storage/emulated / 0 / Android/data/your package name/cache/crashPics String crashLogPath = ToolFileUtils.getCrashLogPath(this); String crashPicPath = ToolFileUtils.getCrashPicPath(this); Copy the code
- About crash logging
- Logging path: / storage/emulated / 0 / Android/data/your package name/cache/crashLogs
- The log file name: V1.0 _2020-09-02 _09:05:01 _java. Lang. NullPointerException. TXT “version + date + exception”
- About jump error log list list page
- The jump log list page is shown below, where a single line of code is invoked. Click the list item on this page to enter details
CrashToolUtils.startCrashListActivity(this); Copy the code
- So how do you get a list of all the crash logs? It is recommended to put the child thread processing!!
List<File> fileList = ToolFileUtils.getCrashFileList(this); // If you want to get the documents yourself, you are advised to arrange them by time / / sorting Collections.sort(fileList, new Comparator<File>() { @Override public int compare(File file01, File file02) { try { // Sort by modification time long lastModified01 = file01.lastModified(); long lastModified02 = file02.lastModified(); if (lastModified01 > lastModified02) { return -1; } else { return 1; }}catch (Exception e) { return 1; }}});Copy the code
- How do I delete a single file operation
// The deletion succeeds if true is returned boolean isDelete = ToolFileUtils.deleteFile(file.getPath()); Copy the code
- How to delete all files. It is recommended to put the child thread processing!!
File fileCrash = new File(ToolFileUtils.getCrashLogPath(CrashListActivity.this)); ToolFileUtils.deleteAllFiles(fileCrash); Copy the code
- How do I get the contents of a crashed file
// Get the content String crashContent = ToolFileUtils.readFile2String(filePath); Copy the code
- There are also some other apis, as follows. This is mainly convenient to test students or products, to avoid development does not recognize the kind of occasional crash bug…
Copy = toolFileutils.copyFile (srcFile, destFile); // Share files. This is called primary share CrashLibUtils. ShareFile (CrashDetailsActivity. This destFile); // The screenshot crashes and is saved to the album. Screenshot - > create screenshots stored file path - > save pictures the picture quality, zoom ratio and sampling rate compression 】 the final Bitmap Bitmap. = ScreenShotsUtils measureSize (this view); String crashPicPath = ToolFileUtils.getCrashPicPath(CrashDetailsActivity.this) + "/crash_pic_" + System.currentTimeMillis() + ".jpg"; boolean saveBitmap = CrashLibUtils.saveBitmap(CrashDetailsActivity.this, bitmap, crashPicPath);Copy the code
05. Abnormal recovery principle
- In the first way, start a new service KillSelfService to restart the APP.
CrashToolUtils.reStartApp1(App.this.1000); Copy the code
- The second way is to use the alarm clock to delay and then restart the app
CrashToolUtils.reStartApp2(App.this.1000, MainActivity.class); Copy the code
- Third, retrieve the LauncherActivity from the project and set its flag and Component to launch the app
CrashToolUtils.reStartApp3(AppManager.getAppManager().currentActivity()); Copy the code
- Detailed introduction to the app startup mode
- Introduction to App Startup
06. Follow-up requirements description
- May not be compatible
- The library has not been tested with a multi-process application. If you test with this configuration, please provide feedback!
- If your application initializes listening or error activity crashes, it is possible to enter an infinite restart cycle (in most cases, the library checks for this, but it may occur in rare cases).
- Fixed an Activity life cycle exception caused by Android P reflection restriction and could not finish the Activity. Some models are still incompatible…
- Description of collecting information about App crashes
- Collect basic crash information
- Process (foreground or background)
- Thread (whether UI thread or not)
- Crash stack (crash in system code or in our own code)
- Crash stack types (Java crash, Native crash or ANR)
- Collect system information during a crash
- Model, system, vendor, CPU, ABI, Linux version, etc. (Look for commonalities)
- Logcat. (Including the running logs of the application and the system, which will record some basic information about the running of the App)
- Collect information about memory crashes (OOM, ANR, virtual memory running out, etc., many crashes are directly related to memory)
- Remaining system memory. (System free memory is very small – less than 10% of MemTotal, OOM, massive GC, system suicide pull, etc.)
- Virtual memory (many problems like OOM, TGkill, etc.)
- Application memory usage (figure out the size and distribution of the application’s own memory usage)
- The number of threads
- Collect application information during a crash
- Crash scenario (which Activity or Fragment the crash occurred in, and which business it occurred in)
- Critical action path (Record the critical user action path, this will help us to reproduce the crash)
- Other customized information (Different applications have different priorities. Such as running time, whether patches are loaded, whether the installation is new or upgraded, etc.)
- Collect basic crash information
07. Principle of abnormal stack trace
- Why does Android crash when an exception occurs
- Once the Thread has an exception is thrown, and we didn’t catch the case, the JVM will call dispatchUncaughtException method in the Thread of the exception is passed to the Thread uncaught exception handler. Found that will eventually use to the Thread. The getDefaultUncaughtExceptionHandler ()
- Since Android exception encountered will collapse, then find some where to use set setDefaultUncaughtExceptionHandler, can locate the RuntimeInit class.
- Find the RuntimeInit class and then the KillApplicationHandler class. First look at the main entrance of the method – > commonInit () – >, and then go down, and find setDefaultUncaughtExceptionHandler code. When an exception occurs, it is a try-catch and kills the app operation directly in finally.
- Details can be seen: Android project crash analysis
- How is the exception stack chain formed after a crash
- To be perfected, see: exception stack trace handling
08. Partial problem feedback
- What happens when the exception is caught?
- Thread. SetDefaultUncaughtExceptionHandler (handler) method, if be called multiple times in the last time the handler shall prevail, so if you measure the module, with the third party may be failure. In this case, before setting default hander, can pass getDefaultUncaughtExceptionHandler hander () method to obtain and keep the old, UncaughtException = uncaughtException = uncaughtException = uncaughtException = uncaughtException
- This section describes uploading logs
- After this exception is initialized, the system prompts you to collect information as soon as possible when entering the global exception and the process is terminated. Therefore, you cannot upload network crash information at this time. You can write the error log to file or sp at this point.
- For example, use SharedPreferences to write the path of error logs into the configuration file. During startup, check whether the configuration file contains error logs. If so, read the file and upload the logs. Delete the SP file after uploading……
- Can crashes and ANR be intercepted using Looper
- Can be implemented to intercept the UI thread crash, time-consuming performance monitoring. However, it is not possible to intercept all exceptions. If a crash occurs in the Activity’s onCreate, causing the Activity to fail to be created, a black screen will appear.
- Looper.loop() is called at the end of the main method in ActivityThread after the app process is forked out. This method handles the main thread scheduling and exits the app once executed.
- If the main thread is abnormal, it will exit the loop, which means the APP crashes, so we need to try catch to prevent the APP from exiting, and then start a looper.loop () to execute the main thread task, and it will not exit.
- Looper intercepting crash or ANR has a huge problem with the button not moving or not responding. May lead to other problems… This should be used carefully
09. Other content description
- confusion
- -keep class com.yc.toollib.** { *; }
- -keepnames class com.yc.toollib.** { *; }
- The library notes are introduced
- Deep exploration of collapse principle
- Permanent application post-crash processing
- Abnormal stack trace processing
- Loop intercepts crashes and ANR
- There are several ways to restart the App
- Other Items recommended
- 1. Open source blog aggregation
- 2. Reduce the Crash library
- 3. Video player packaging library
- 4. State switch manager encapsulates the library
- 5. Complex RecyclerView packaging library
- 6. Popover encapsulation library
- 7. Version update package library
- 8. Status bar encapsulates library
- 9. Lightweight thread pool encapsulation library
- 10. Multicast map encapsulation library
- 11. Audio player
- 12. Gallery and picture zoom control
- 13.Python multi-channel packaging
- 14. Overall sideslip animation package library
- 15.Python crawler girl graph
- 17. Customize the progress bar
- 18. Custom collapse and expand layouts
- 19. Product details page is loaded in pages
- 20. Set the red dot control on any View control
- 21. Play video library by swiping one page at a time