61

I have recently switched to new Firebase Crashlytics from Fabric one and I can't find alternative for disabling Crashlytics in debug mode.

Fabric:

val crashlytics = Crashlytics.Builder().core(CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build()
Fabric.with(this, crashlytics, Answers())

Anyone know answer? Ive seen that FirebaseCrashlytics class has its core set up internally now. I've tried FirebaseCrashlytics(CrashlyticsCore.??).getInstance(), but that kind of constructor is not working.

Also CrashlyticsCore class no longer has .Builder() available

martin1337
  • 2,384
  • 6
  • 38
  • 85

3 Answers3

82

To do it programmatically use below code in Application class

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG)
//enabled only for signed builds

Enable collection for select users by calling the Crashlytics data collection override at runtime. The override value persists across launches of your app so Crashlytics can automatically collect reports for future launches of that app instance. To opt out of automatic crash reporting, pass false as the override value. When set to false, the new value does not apply until the next run of the app.

Here is the link to documentation https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#enable-reporting

Manohar
  • 22,116
  • 9
  • 108
  • 144
  • 5
    To disable the crash logs while in debug mode you must pass `!BuildConfig.DEBUG`. Enabled only for signed builds `FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG);` So the value will be false and it will only log crash for release build. – Vijay Desai Oct 07 '20 at 08:33
  • 1
    Since Crashlytics is enabled by default, I prefer this: `if (BuildConfig.DEBUG) { FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false) }` – Ananthakrishnan K R Jul 03 '21 at 15:43
69

I have tried once some time ago which worked for me . Add this to build.gradle.

android {
  buildTypes {
     debug {
        manifestPlaceholders = [crashlyticsCollectionEnabled:"false"]
        ...
     }

    release {
        manifestPlaceholders = [crashlyticsCollectionEnabled:"true"]
        ...
    }
  }
}

And then set this attribute in manifest .

<meta-data
        android:name="firebase_crashlytics_collection_enabled"
        android:value="${crashlyticsCollectionEnabled}" />

If you log manually also then you can use something like this at runtime :-

FirebaseCrashlytics.getInstance().recordException(RuntimeException("Invalidtoken"))

Also Check this out and crashlytics opt-in.

Slion
  • 2,558
  • 2
  • 23
  • 27
ADM
  • 20,406
  • 11
  • 52
  • 83
  • 10
    More safe way to add manifest placeholders in place of replace all of them will be: `manifestPlaceholders.firebasecrashlyticsCollectionEnabled = false` – wrozwad Oct 06 '20 at 09:03
  • @sosite can you please tell me in which tag i should add this line? – Tanjim ahmed Jul 06 '21 at 09:23
  • 1
    @Tanjimahmed You use it directly in `debug` or `release` build types – wrozwad Jul 07 '21 at 12:45
  • 1
    @wrozwad For the given example it should be `manifestPlaceholders. crashlyticsCollectionEnabled = false` - or alternatively `manifestPlaceholders["crashlyticsCollectionEnabled"] = false`. – Klemens Zleptnig Feb 24 '22 at 08:53
3

This works for me

manifest.xml
<meta-data             
   android:name="firebase_crashlytics_collection_enabled"            
   android:value="${crashlyticsCollectionEnabled}" />
build.gradle
buildTypes {
        release {
            manifestPlaceholders["crashlyticsCollectionEnabled"] = true
        }
        debug {
            manifestPlaceholders["crashlyticsCollectionEnabled"] = false
        }
    }

check from official docs. firebase and Inject build variables into the manifest

kadirgun
  • 151
  • 1
  • 6