Tuesday, March 7, 2017

android - Firebase crash reporting example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f7f9f5"
    >
    <Button
        android:id="@+id/btn_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply Red Text"
        android:layout_gravity="top|center_horizontal"
        android:layout_margin="20dp"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:text="Hello Firebase Crash Reporting!"
        android:layout_margin="20dp"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.crash.FirebaseCrash;


public class MainActivity extends AppCompatActivity{
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TextView mTextView;
    private Button mButtonRed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Put a simple log on firebase crash reporting
        FirebaseCrash.log("This is a sample log using firebase crash");
        // Report a sample error on firebase crash reporting
        FirebaseCrash.report(new Exception("This is a sample exception using firebase crash"));

        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTextView = (TextView) findViewById(R.id.tv);
        mButtonRed = (Button) findViewById(R.id.btn_red);

        mButtonRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    Thread.sleep(2000);
                    mTextView.setTextColor(Color.RED);
                }catch (InterruptedException e){
                    // Report the exception on firebase to see it in online
                    FirebaseCrash.report(e);
                }
            }
        });
    }
}
build.gradle dependencies (app level)

compile 'com.google.firebase:firebase-crash:10.2.0'