Crash Reporting with ACRA

 Using crash reporting tools is a great way to unearth the hidden bugs that are causing crashes. This tutorial will demonstrate an easy to use crash reporting tool ACRA (Application Crash Reports for Android) to send crash logs when crash occurs.

At first step we shall include the dependency of ACRA in our build.gradle file. The version 4.9.2 was the latest at the time of writing this tutorial.

compile 'ch.acra:acra:4.9.2'

At next step we have to create our Application class integrate ACRA there.

@ReportsCrashes(
        formUri = "",
        mailTo = "yourmail@yourdomain.com",
        customReportContent = {
                ReportField.APP_VERSION_CODE,
                ReportField.APP_VERSION_NAME,
                ReportField.ANDROID_VERSION,
                ReportField.PHONE_MODEL,
                ReportField.BRAND,
                ReportField.CUSTOM_DATA,
                ReportField.INITIAL_CONFIGURATION,
                ReportField.CRASH_CONFIGURATION,
                ReportField.USER_CRASH_DATE,
                ReportField.STACK_TRACE,
                ReportField.LOGCAT},
        resToastText = R.string.crash_toast_text,
        mode = ReportingInteractionMode.TOAST)
public class MyApplication extends Application {
 
    @Override
    public void onCreate() {
        super.onCreate();
        ACRA.init(this);
    }
}

The code is very much self explanatory, we need to set the email address to whom crash report will be send, set some content and select how will the app interact with the user when it is crashed.

We have to set the MyApplication as the application class and add the following internet, read logs and read phone state permission. The AndroidManifest.xml file will look like the following.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.acrademo">
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_LOGS" />
 
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Finally in our MainActivity, we shall add a button and add code that will force the app crash when button is clicked to test our app.

public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    public void onCrash(View view){
        throw new RuntimeException("App Crashed");
    }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...