How to disable Firebase Crash Reporting when the app is running on debug?

22,984

Solution 1

UPDATED: With Google Play Services / Firebase 11+ you could now disable crash reporting at runtime. FirebaseCrash.setCrashCollectionEnabled() (Thanks @Tyler Carberry)

OLD ANSWER:

There is no official support for this, as far as the community has been able to surmise. The best way I would suggest to do this is, set up multiple Firebase apps in your dashboard, one for each build type, and set up multiple google_services.json files directing to each different app depending on the build variant.

Solution 2

With Google Play Services 11.0 you could now disable crash reporting at runtime.

FirebaseCrash.setCrashCollectionEnabled(!BuildConfig.DEBUG);

Solution 3

Recently was introduced the possibility to disable Firebase crash reporting in a official way. You need to upgrade the firebase android sdk to at least version 11.0.0

In order to do so, you need to edit your AndroidManifest.xml and add:

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

Inside the <application> block.

You can check if Firebase crash report is enabled at runtime using FirebaseCrash.isCrashCollectionEnabled().

Below a complete example to disable Firebase crash reporting in your debug builds.

build.gradle:

...
 buildTypes {

    release {
        ...
        resValue("bool", "FIREBASE_CRASH_ENABLED", "true")
    }

    debug {
        ...
        resValue("bool", "FIREBASE_CRASH_ENABLED", "false")

    }

}
...
dependencies {
    ...
    compile "com.google.firebase:firebase-core:11.0.0"
    compile "com.google.firebase:firebase-crash:11.0.0"
    ...
}

AndroidManifest.xml:

 <application>

    <meta-data
        android:name="firebase_crash_collection_enabled"
        android:value="@bool/FIREBASE_CRASH_ENABLED"/>
...

Solution 4

in my Application class, onCreate()

if (BuildConfig.DEBUG) {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
            Log.wtf("Alert", paramThrowable.getMessage(), paramThrowable);
            System.exit(2); //Prevents the service/app from freezing
        }
    });
}

It works because it takes the oldHandler, which includes the Firebase one

 final UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();

out of the processing path

Solution 5

You can change the firebase crash dependency to a release only dependency.

To do this, you define it as a releaseCompile dependency

releaseCompile 'com.google.firebase:firebase-crash:9.4.0'

Now it will only be included in the release builds. If you have other custom build types that you want crash reporting for, you can add it to them to.

customBuildTypeCompile 'com.google.firebase:firebase-crash:9.4.0'
Share:
22,984

Related videos on Youtube

facundomr
Author by

facundomr

Updated on November 22, 2020

Comments

  • facundomr
    facundomr over 3 years

    I have successfully implemented Firebase Crash Reporting, but I need to disable the service when the app is running undo the 'debug' Build Variant, in order to avoid non-real crashes in the console during the development.

    The official documentation doesn't say anything about this.

    • James Parsons
      James Parsons about 8 years
      if (!development) { FirebaseCrash.report(e);}
    • facundomr
      facundomr about 8 years
      Thanks, @James_Parsons, but I did not mean that. I need to disable automatic crash reporting, not just the manual calls to the API.
    • Ankit Mundada
      Ankit Mundada over 7 years
      releaseCompile 'com.google.firebase:firebase-crash:9.8.0' won't this work? it is supposed to add the dependency only for your release builds, hence while developing the library won't be added to the project, right?
    • itzhar
      itzhar over 6 years
  • facundomr
    facundomr about 8 years
    Okay, this could be useful. How can I tweak the google_services.json file so it can depends on my current Build Variant? As far as I know, you can only define different files based on the build variant, if they are in the app/src/{flavor}, but this file is in the app directory root
  • WoogieNoogie
    WoogieNoogie about 8 years
    Yeah, you're exactly right, you just need to move things around. Create app/src/release directory and move the current google_services.json file into there, then put the new files that were generated for each app type into the other folders.
  • facundomr
    facundomr about 8 years
    Thanks! The tweak was useful. Hope to see this feature soon in the Firebase SDK.
  • Doug Stevenson
    Doug Stevenson about 8 years
    Hi, I'm with the Crash Reporting team. We are looking into providing a way for you to disable crash reporting programmatically. We also found that some developers in some parts of the world are not allowed to collect crashes for legal reasons, and they also need a way to disable crash reporting at runtime.
  • Alin
    Alin almost 8 years
    @DougStevenson it has been quite a while since your comment and I don't seem to find any way of disabling the automatic crash reporting. Any way to do it at the moment?
  • Frank
    Frank over 7 years
    for more details on this answer, see: firebase.googleblog.com/2016/08/…
  • KasparTr
    KasparTr over 7 years
    Doesn't FB crash reporting only track apps signed with the specified keystore SHA1 under FB - Project Settings? If so then you are using debug.keystore for debug verison and FB shouldn't track that? I have the same problem as you but don't understand why tracking occurs when signed with another keystore?
  • Bronx
    Bronx over 7 years
    for the first part you can simply use BuildConfig.DEBUG
  • Joel
    Joel over 7 years
    FYI, even using this method, it appears that Firebase saves up all the crashes in a database, and if you ever install a release version on your emulator (to test ProGuard, for instance), it will then upload ALL the saved crashes.
  • Colin White
    Colin White over 7 years
    @DougStevenson Has this been added since your last comment? If not, is there a ticket I can follow for updates?
  • Doug Stevenson
    Doug Stevenson over 7 years
    @ColinWhite Sorry, there is nothing available yet. If you follow Firebase on twitter (or me CodingDoug), you'll likely hear about any new stuff that way.
  • Tyler Carberry
    Tyler Carberry about 7 years
    With Google Play Services 11.0 you could now disable crash reporting at runtime. FirebaseCrash.setCrashCollectionEnabled(!BuildConfig.DEBUG);
  • rraallvv
    rraallvv over 6 years
    Does the flag firebase_crash_collection_enabled work for version 9.2.1 of Firebase?
  • evi
    evi over 6 years
    @rraallvv no, the feature was not yet available
  • Leon
    Leon over 6 years
    This is a great solution and is based on the documented mechanism for disabled Crashlytics. The manifest key is now 'firebase_crashlytics_collection_enabled' by the way.
  • evi
    evi over 6 years
    @Leon, sorry mate, you are wrong. This approach is an android standard de facto for enable/disable things in manifest, the fact that is similar to the approach used by Crashlytics documentation is totally random!
  • Alex Shevelev
    Alex Shevelev almost 6 years
    Not "firebase_crash_collection_enabled" but "firebase_crashlytics_collection_enabled" - from firebase.google.com/docs/crashlytics/customize-crash-reports
  • evi
    evi almost 6 years
    @AlexShevelev Firebase Crashlytics is a different product, here we refer to firebase crash reporting, now deprecated