Get access to Android Application crash logs and stacktrace

How to obtain error logs from your Android Application? I did all the testing I could possibly do and rolled out my application into the world thinking it was perfect. But like with any program got complaints that my app is crashing sometimes. Now what do I do? I don't have the device in front of me that I can hook it up with my laptop using the USB and debug. Looking thru the internet found this simple solution. It sends you the stacktrace whenever your android application crashes. Here is how to implement ...

  • Download the latest trace.jar file found here.
  • Create a lib folder in your Android Project and drop the trace.jar file in there. 
    • Then add the jar file to your project Java Build Path.
  • In your android manifest, you must enable internet access for your application
    • <uses-permission android:name="android.permission.INTERNET">
  • In the onCreate method of your activity or in your service, you must call public static boolean register(Context context) found in the class ExceptionHandler.
    • ExceptionHandler.register(this, "http://your.domain/yourpath/AndroidCrashReports");

Java Servlet Source code for AndroidCrashReports.java

package com.as400samplecode;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AndroidCrashReports extends HttpServlet {
    private static final long serialVersionUID = 1L;
      
    public AndroidCrashReports() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
        String package_name = request.getParameter("package_name");
        String package_version = request.getParameter("package_version");
        String stacktrace = request.getParameter("stacktrace");
       
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        DateFormat timeFormat = new SimpleDateFormat("kkmmss");
       
        System.out.println("Android Error Log Start ***");
        System.out.println("Date: " + dateFormat.format(System.currentTimeMillis()));
        System.out.println("Time: " + timeFormat.format(System.currentTimeMillis()));
        System.out.println("Package name: " +  package_name);
        System.out.println("Package version: " +  package_version);
        System.out.println("Stacktrace: " +  stacktrace);
        System.out.println("Android Error Log End ***");
        System.out.println("\n\n");
       
    }

}

The application does HTTP post of the following variables
  • package_name
  • package_version
  • stacktrace

Tip: You can modify the AndroidCrashReports.java to write the info to a file.

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.