NATIVE ANDROID | GOOGLE CRASH ANALYTICS | FIREBASE

Share the post

GOOGLE CRASH ANALYTICS | FIREBASE

Hello All, Today we will see google crash analytics in native android application. In this topic, we discuss what is google crash analytics? how to implement crash analytics in native android mobile applications?. What are the benefits of crash analytics?

What is Google Crash Analytics ?

Google crash analytics is the most important topic in android application development. By using this feature app developer can able to detect live application errors. For example, If you have an android application that is live on the play store and has a good download. This application is an online application & you are calling multiple APIs within it. If the android application crashes on some user’s mobile then you must have to detect those defects? Thus, crash analytics came into the picture. If crash analytics implemented on your android application then you don’t have to worry about it. You can able to get those errors directly on your google firebase console. How? We will see this with an example.

How to implement google crash analytics in android application ?

Please find below steps :

  1. Create a new android project
  2. Copy MainActivity.java code as shown below :
import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;


import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    private EditText editTextEmail;
    private EditText editTextPassword;
    private Button buttonRegister;
    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;


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


    private void init(){
        progressDialog = new ProgressDialog(this);
        firebaseAuth = FirebaseAuth.getInstance();
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        buttonRegister = (Button) findViewById(R.id.buttonRegister);
        buttonRegister.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if(v == buttonRegister){
            registerUser();
        }
    }


    private void registerUser(){
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();
        if(email == ""){
            Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
            return;
        }


        if(password == ""){
            Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
            return;
        }


        progressDialog.setMessage("Registering User...");
        progressDialog.show();


        firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    //user registered successfully
                    Toast.makeText(MainActivity.this,"Registered successfully",Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this,"Something went wrong...please try again",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

3. Copy acitivity_main.xml from below :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true">


<EditText
    android:id="@+id/editTextEmail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter email id"
    android:inputType="textEmailAddress"/>


    <EditText
        android:id="@+id/editTextPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter password"
        android:inputType="textPassword"/>


    <Button
        android:id="@+id/buttonRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register User"
        android:layout_margin="15dp"/>


</LinearLayout>

4. Modify your app.gradle file as shown below :

Install the dependency of firebase analytics & firebase crash analytics. Please see below image for more details :

app.gradle
app.gradle file

5. Now apply plugin to app.gradle file as shown in below image :

Plugin
apply plugin

6. Now add dependency in project.gradle file as shown in below image :

Project.gradle
project.gradle file

7. Now comment the line as shown below image to see the actual error in firebase console using crash analytics 

Force_Error
Comment code to get the error

8. Now, We have commented on a few lines as shown in the above point. As soon as user open the file they will get the error & we can able to see that error in crash analytics as shown below :

Report
firebase console

As shown in the above image we can able to find our crash with full details of the error. So, you can able to implement this feature anywhere in the application & detect the error without having the actual device.

Leave a Comment