How do I obtain crash-data from my Android application?

前端 未结 30 3245
庸人自扰
庸人自扰 2020-11-22 01:10

How can I get crash data (stack traces at least) from my Android application? At least when working on my own device being retrieved by cable, but ideally from any instance

30条回答
  •  生来不讨喜
    2020-11-22 01:40

    For sample applications and debugging purposes, I use a simple solution that allows me to write the stacktrace to the sd card of the device and/or upload it to a server. This solution has been inspired by Project android-remote-stacktrace (specifically, the save-to-device and upload-to-server parts) and I think it solves the problem mentioned by Soonil. It's not optimal, but it works and you can improve it if you want to use it in a production application. If you decide to upload the stacktraces to the server, you can use a php script (index.php) to view them. If you're interested, you can find all the sources below - one java class for your application and two optional php scrips for the server hosting the uploaded stacktraces.

    In a Context (e.g. the main Activity), call

    if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) {
        Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(
                "/sdcard/", "http:///upload.php"));
    }
    

    CustomExceptionHandler

    public class CustomExceptionHandler implements UncaughtExceptionHandler {
    
        private UncaughtExceptionHandler defaultUEH;
    
        private String localPath;
    
        private String url;
    
        /* 
         * if any of the parameters is null, the respective functionality 
         * will not be used 
         */
        public CustomExceptionHandler(String localPath, String url) {
            this.localPath = localPath;
            this.url = url;
            this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
        }
    
        public void uncaughtException(Thread t, Throwable e) {
            String timestamp = TimestampFormatter.getInstance().getTimestamp();
            final Writer result = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            String stacktrace = result.toString();
            printWriter.close();
            String filename = timestamp + ".stacktrace";
    
            if (localPath != null) {
                writeToFile(stacktrace, filename);
            }
            if (url != null) {
                sendToServer(stacktrace, filename);
            }
    
            defaultUEH.uncaughtException(t, e);
        }
    
        private void writeToFile(String stacktrace, String filename) {
            try {
                BufferedWriter bos = new BufferedWriter(new FileWriter(
                        localPath + "/" + filename));
                bos.write(stacktrace);
                bos.flush();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void sendToServer(String stacktrace, String filename) {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            List nvps = new ArrayList();
            nvps.add(new BasicNameValuePair("filename", filename));
            nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
            try {
                httpPost.setEntity(
                        new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                httpClient.execute(httpPost);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    upload.php

    
    

    index.php

    FilenameFiletypeFilesize\n");
        for($index=0; $index < $indexCount; $index++) {
            if ((substr("$dirArray[$index]", 0, 1) != ".") 
                    && (strrpos("$dirArray[$index]", ".stacktrace") != false)){ 
                print("");
                print("$dirArray[$index]");
                print("");
                print(filetype($dirArray[$index]));
                print("");
                print(filesize($dirArray[$index]));
                print("\n");
            }
        }
        print("\n");
    ?>
    

提交回复
热议问题