patali.in

Notes from a perpetual student

Enable/Disabling Firebase Crashlytics on runtime in Android

Posted at — Feb 25, 2020

Recently I integrated Firebase’s Crashlytics solution into one of our main Android products. It’s fairly easy to integrate, just 4 steps as per Crashlytics’s documentation here. But as with any reporting system there involves the issue of privacy and data collection problem. Always important to inform the user if you are collecting any information and also request for their consent to do so. By default Crashlytics’s behavior is to start reporting on every crash. So instead if you want to setup an opt-in process then all you have to do is

Step 1

In your AndroidManifest.xml, add this under

<meta-data
    android:name="firebase_crashlytics_collection_enabled"
    android:value="false" />

Step 2

In your app’s main Activity file, in the onCreate method add this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  
    // toggle Crashlytics on/off based on user consent
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    boolean enableCrashReporting = preferences.getBoolean("enable_crash_report", false);
    if(enableCrashReporting) {
       Fabric.with(this, new Crashlytics());
    }
}

Points to remember

References

✍ Comments

Say something