5

From my search, i got the below code for getting the Crash Log .

try {
      Process process = Runtime.getRuntime().exec("logcat -d");
      BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(process.getInputStream()));

      StringBuilder log=new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null) 
      {
        log.append(line);
      }

But where do i add this code, so that i should get the crash report whenever my app crashes .

Also i want to email it or send to the server, but after the app getting crashed, how to call the action to send email/HTTP post method .

Please advise and thanks in advance .

VIGNESH
  • 2,023
  • 8
  • 31
  • 46
  • [This SO question](http://stackoverflow.com/questions/10171336/accessing-android-crash-reports?rq=1) may give you some idea about your requirement – Lukasz 'Severiaan' Grela Dec 05 '13 at 11:59
  • If you want crash report, there are many free solution like Crashlytics(http://www.crashlytics.com/) or open soure solution like ARCA(https://code.google.com/p/acra/) – Hein Dec 05 '13 at 11:59

7 Answers7

19

The best way to handle crash logs is creating an UncaughtExceptionHandler and handling it as per your requirement. Create a BaseActivity class and extend all the Activities with that and put this code stuff in the BaseActivity class.

private Thread.UncaughtExceptionHandler handleAppCrash = 
                                         new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            Log.e("error", ex.toString());
            //send email here
        }
    };

Then just enable is inside onCreate() method of your BaseActivity by using

Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);

So, now whenever there will be a crash in your Application uncaughtException() will be called and you will have to handle the crash accordingly.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
3

I recommend you use ARCA https://github.com/ACRA/acra.

Include arca in your build.gradle--it uses the apache 2.0 license as of 10/29.

compile 'ch.acra:acra:4.9.0' //TODO:  Apache 2.0 license https://github.com/ACRA/acra

In your class that extends Application, drop this on top of the "class" declaration.

@ReportsCrashes(mailTo = "someone@somewhere.com",
        customReportContent = { ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT },
        mode = ReportingInteractionMode.TOAST,
        resToastText = R.string.resToastText) //you get to define resToastText
public class MyApplication extends Application {

Then override the following method from the same Application class like so:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);

    // The following line triggers the initialization of ACRA
    ACRA.init(this);
}
CamHart
  • 3,825
  • 7
  • 33
  • 69
2

In my case I have a bug that i cant replicate on my phone I just want the stack trace back from a lone tester. The simplest way I could find to do this was to get it copied into the users clipboard and ask them to send it to me here is the code:

import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Copies the stack trace the exception that is causing your application to crash into the clip board.
 * Ask your testers to paste it into an email / text message to you.
 *
 * @author Stuart Clark
 */

public class CrashDebugApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
      @Override
      public void uncaughtException(Thread thread, Throwable e) {
        // Get the stack trace.
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);

        // Add it to the clip board and close the app
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("Stack trace", sw.toString());
        clipboard.setPrimaryClip(clip);
        System.exit(1);
      }
    });

  }
}

Then set the android:name property in Android Manifesto i.e.

<application android:icon="@mipmap/ic_launcher" android:name=".CrashDebugApplication">
Stuart Clark
  • 611
  • 8
  • 13
2

Here is a full explanation for setup your own crash reporter, it will show a dialog like this to the user when your app starts again and asks him/her to send the log with email:

enter image description here

1- Create a class names UnexpectedCrashSaver:

public class UnexpectedCrashSaver implements Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler defaultUEH;
private Context app = null;
public UnexpectedCrashSaver(Context app) {
    this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    this.app = app;
}
public void uncaughtException(Thread t, Throwable e) {
    StackTraceElement[] arr = e.getStackTrace();
    String report = e.toString()+"\n\n";
    report += "--------- Stack trace ---------\n\n";
    for (int i=0; i<arr.length; i++) {
        report += "    "+arr[i].toString()+"\n";
    }
    report += "-------------------------------\n\n";
    App.instance().toastShort("saving!");

    report += "--------- Cause ---------\n\n";
    Throwable cause = e.getCause();
    if(cause != null) {
        report += cause.toString() + "\n\n";
        arr = cause.getStackTrace();
        for (int i=0; i<arr.length; i++) {
            report += "    "+arr[i].toString()+"\n";
        }
    }
    report += "-------------------------------\n\n";
    try {
        FileOutputStream trace = app.openFileOutput("stack.trace",
                Context.MODE_PRIVATE);
        trace.write(report.getBytes());
        trace.close();
    } catch(IOException ioe) {
        // ...
    }
    defaultUEH.uncaughtException(t, e);
}
}

2- Add this line to your onCreate() method of Application class:

        Thread.setDefaultUncaughtExceptionHandler(new UnexpectedCrashSaver(this));

if you don't have application class add this code to your onCreate() method of all activities: (if you have BaseActivity just put it in onCreate() method of BaseActivity

Thread.setDefaultUncaughtExceptionHandler(new UnexpectedCrashSaver(ActivityName.this));

3- create a layout named checkbox.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<CheckBox
    android:id="@+id/checkbox"
    style="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp" />
 </FrameLayout>

4- Add the following method to your MainActivity class:

 private void checkForCrash()
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean crash_never_ask_again = preferences.getBoolean("crash_never_ask_again", false);
    if(crash_never_ask_again)//Previously user check the checkbox of never ask me again about sending crash dialog
        return;
    String dialog_message = "In the last run, the program encountered an error, we apologize for this, you can kindly send us the error information to fix this error in future updates.";
    String button_positive_text = "Send";
    String button_negative_text = "Close";
    String checkbox_text = "Never ask again";
    String email = "crashreport@example.com";

    String line;
    String trace="";
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(MainActivity.this.openFileInput("stack.trace")));
        while((line = reader.readLine()) != null) {
            trace += line+"\n";
        }
    } catch(FileNotFoundException fnfe) {
        // ...
    } catch(IOException ioe) {
        // ...
    }
    if(trace.length() < 10)//We didn't have any crash
        return;

    View checkBoxView = View.inflate(this, R.layout.checkbox, null);
    CheckBox checkBox =  checkBoxView.findViewById(R.id.checkbox);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("checkbox_checked",true);
            editor.apply();
        }
    });
    checkBox.setText(checkbox_text);

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setCancelable(true);
    //builder.setIcon(R.drawable.ic_setting);
    builder.setMessage(dialog_message);
    builder.setView(checkBoxView);
    builder.setCancelable(false);
    String finalTrace = trace;
    builder.setPositiveButton(button_positive_text, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent sendIntent = new Intent(Intent.ACTION_SEND);
            String subject = "Error report";
            String body = "Mail this to appdeveloper@gmail.com: " + "\n" + finalTrace + "\n";

            sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {email});
            sendIntent.putExtra(Intent.EXTRA_TEXT, body);
            sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
            sendIntent.setType("message/rfc822");
            MainActivity.this.startActivity(Intent.createChooser(sendIntent, "Title:"));
            MainActivity.this.deleteFile("stack.trace");
        }
    });
    builder.setNegativeButton(button_negative_text, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            MainActivity.this.deleteFile("stack.trace");
            boolean checkbox_checked = preferences.getBoolean("checkbox_checked", false);
            if(checkbox_checked)
            {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("crash_never_ask_again",true);
                editor.apply();
            }
                dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

}

5- call the method created in step 4 in onCreate method of MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkForCrash();
    setContentView(R.layout.activity_main);
    //...

Thats all!

farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30
0

Have a look at this projekt. LINK It is a small projekt to post the stacktrace to your server, so you have them on your own server.

A.S.
  • 4,574
  • 3
  • 26
  • 43
0

I haven't tried it but looking at it gives the current log and not the crash report refer to How do I obtain crash-data from my Android application?

Community
  • 1
  • 1
Piyush
  • 2,040
  • 16
  • 14
0

Create BaseActivity.

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);
    Thread.setDefaultUncaughtExceptionHandler(handleCrash);
}

private Thread.UncaughtExceptionHandler handleCrash =
        new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable e) {
                Log.e("error", e.toString());
                //send email here
            }
        };

}

Note that BaseActivity is extending AppCompatActivity. And YourActivity is extending BaseActivity

public class YourActivity extends BaseActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_your);
     //Basically your activity is just extending BaseActivity, and all exceptions are 
     //are taken care of in BaseActivity.
 }
}

Now when there is any unhandled exception detected from any activities of your app, it'll be detected in BaseActivity, then you can send error report to your email.

Yosidroid
  • 2,053
  • 16
  • 15