pixel
pixel

Reputation: 10537

Android ACRA Reporting

I am trying to get basic reporting using ACRA in Android Studio in my test app (Lollipop).

So far, I have implemented following:

  1. added dependancy in gradle

    compile 'ch.acra:acra:4.6.2'
    
  2. added MyApplication which extends Application and added ReportsCrashes annotation to it:

    @ReportsCrashes(
    
      resNotifTickerText = R.string.crash_notification_ticker_text,
    
      resNotifTitle = R.string.crash_notification_title,
    
      resNotifText = R.string.crash_notification_text,
    
      resNotifIcon = R.mipmap.error );
    
     public class MyApplication extends Application {
    
            private static final String TAG = MyApplication.class.getSimpleName();
    
             @Override
             public void onCreate(){
                 super.onCreate();
    
                 ACRA.init(this);
             }
         }
    

(BTW, sorry for code formatting above, but StackOverflow refused to format it properly for some reason)

This is based on ACRA documentation provided in github https://github.com/ACRA/acra/wiki/BasicSetup

  1. added application name and INTERNET permission in AndroidManifest

    <!-- add INTERNET permission -->
    <uses-permission android:name="android.permission.INTERNET" />
    
    <!-- add application name -->
    <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>
    

  2. My main activity has just one button, when clicked, it will crash app when it attempts to do division by zero

     public class MainActivity extends AppCompatActivity {
         public final static String TAG = MainActivity.class.getSimpleName();
    
         private Button btnError;
    
         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
    
             btnError = (Button) findViewById(R.id.btnError);
             btnError.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Toast.makeText(getApplicationContext(), getString(R.string.toast_app_crash), Toast.LENGTH_SHORT).show();
    
                     Runnable r = new Runnable() {
                         @Override
                         public void run() {
                             // this will crash your app throwing Arithmetic Exception
                             int number = 7 / 0;
                         }
                     };
    
                     Handler h = new Handler();
                     h.postDelayed(r, 2000);
                 }
             });
         }
     }
    

I am expecting to see some kind of notification and some kind of report to get generated but I dont get any. My app simply crashes at the spot where division by zero is attempted.

I am not sure what is that I am doing wrong.

Thanks,

Upvotes: 0

Views: 2360

Answers (1)

Madhukar Hebbar
Madhukar Hebbar

Reputation: 3173

The type of notification you should select as

 mode = ReportingInteractionMode.TOAST,
   //Available : Dialog,Notification,Toast and Silent
    resToastText = R.string.crash_text_toast

Here is the sample report parameter what i have used in my app.

    @ReportsCrashes(
    formUri="",
formUriBasicAuthLogin = "CloundantAuthLogin",
formUriBasicAuthPassword = "CloundantAuthKeyPassword",
    reportType = org.acra.sender.HttpSender.Type.JSON,
    httpMethod = org.acra.sender.HttpSender.Method.PUT,
    customReportContent = { ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL,ReportField.DEVICE_FEATURES,
    ReportField.USER_APP_START_DATE,ReportField.USER_CRASH_DATE,ReportField.TOTAL_MEM_SIZE,ReportField.USER_COMMENT,
        ReportField.THREAD_DETAILS, ReportField.STACK_TRACE }, 
    mode = ReportingInteractionMode.DIALOG,
    includeDropBoxSystemTags  = true,
    resToastText = R.string.crash_toast_text, // optional, displayed as soon as the crash occurs, before collecting data which can take a few seconds
    resDialogText = R.string.crash_dialog_text,
    resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign
    resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name
    resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. when defined, adds a user text field input with this text resource as a label
    resDialogOkToast = R.string.crash_dialog_ok_toast // optional. displays a Toast message when the user accepts to send a report.
    )

Library used : acra-4.6.2

The best tutorial till date available here : http://www.toptal.com/android/automated-android-crash-reports-with-acra-and-cloudant

Upvotes: 1

Related Questions