How to get APP crash log for Android

Keywords: Android Unix

Once any error occurs in android APP, it will stop running, which is a headache for many developers,

In many cases, some recessive bugs pass the test department, and a small area crash occurs after they are put on the shelf. In this case, there is no log for BUG reporting, so bugs cannot be found.

So, can only through user feedback consume a lot of manpower and time to reproduce it?

In fact, as long as you insert a small piece of code into the Application, you can capture all the complete error logs:

        //Record crash information
        final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
                //Get UNIX timestamp at Crash Time
                long timeMillis = System.currentTimeMillis();
                //Transform the time stamp into a human readable format and build a String splicer
                StringBuilder stringBuilder = new StringBuilder(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(timeMillis)));
                stringBuilder.append(":\n");
                //Get error information
                stringBuilder.append(throwable.getMessage());
                stringBuilder.append("\n");
                //Get stack information
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                throwable.printStackTrace(pw);
                stringBuilder.append(sw.toString());

                //This is the complete error information. You can upload it to the server, or save it as a local file, etc
                String errorLog = stringBuilder.toString();

                //Finally, how to deal with the crash? Here, use the default processing method to stop the APP
                defaultHandler.uncaughtException(thread, throwable);
            }
        });

Engineers with self-cultivation don't get up quickly~

Finally, it's the universal convention. If you think this blog can help you, send a red envelope to the blogger~

 

Posted by aravind_mg on Sun, 12 Jan 2020 07:57:15 -0800