Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Crashlytics during development

People also ask

How do I disable firebase Crashlytics in debug mode?

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. To disable the crash logs while in debug mode you must pass ! BuildConfig.

What is the use of Crashlytics in Android?

Crashlytics saves you troubleshooting time by intelligently grouping crashes and highlighting the circumstances that lead up to them. Find out if a particular crash is impacting a lot of users. Get alerts when an issue suddenly increases in severity.

How do I debug Crashlytics?

Enable debug logging for Crashlytics If you don't see your test crash in the Crashlytics dashboard, you can use debug logging for Crashlytics to help track down the problem. Enable debug logging: In Xcode, select Product > Scheme > Edit scheme. Select Run from the left menu, then select the Arguments tab.


I found the solution from Crashlytics (with Fabric integration)

Put following code inside your Application class onCreate()

Crashlytics crashlytics = new Crashlytics.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, crashlytics);

EDIT:

In Crashalitics 2.3 and above, this is deprecated. The correct code is:

CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build());

or

Fabric.with(this, new Crashlytics.Builder().core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build());

(copied from Crashlytics deprecated method disabled())


EDIT2:

You can also optionally add this to your buildType in gradle. This command disables sending the crashlytics mapping file and generating an ID for each build, which speeds up gradle builds of those flavors. (It doesn't disable Crashlytics at run time.) See Mike B's answer here.

buildTypes {
    release {
           ....
    }
    debug {
        ext.enableCrashlytics = false
    }
}

Marc from Crashlytics here. Here's a couple of ways to disable Crashlytics while you are doing your debug builds!

  1. Use a different android:versionString for debug and release builds and then disable crash reporting from the Crashlytics web dashboard for the debug version.

  2. Wrap the call to Crashlytics.start() in an if statement that checks a debug flag. You could use either a custom flag or an approach like the ones proposed here: How to check if APK is signed or "debug build"?


The chosen answer is not correct any more. Google changed the integration of Crashlytics. My current version is 2.9.1 and the only thing I had to do is, to add implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' to my Gradle file. No further things required, nice but this means that Crashlytics is always running.

Solution 1

Only compile Crashlytics in release version:

dependencies {
   ...
   releaseImplementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' // update version
}

Solution 2

If you want to additionally configure Crashlytics then Solution 1 is not working, since the Crashlytics classes will not be found in Debug Builds. So change the Gradle implementation back to:

implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' // update version

Then go to your Manifest and add the following meta-data tag inside the application tag:

<application
        android:name="...>

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

...

</application>

Add to your Launch-Activity (only one-time required, not every Activity)

if (!BuildConfig.DEBUG) { // only enable bug tracking in release version
   Fabric.with(this, new Crashlytics());
}

This will only enable Crashlytics in release versions. Be careful, also check for BuildConfig.DEBUG when you then configure Crashlytics, E.g:

if (!BuildConfig.DEBUG) {
   Crashlytics.setUserIdentifier("HASH_ID");
}

If you use Gradle just add this to a flavor:

ext.enableCrashlytics = false

Check out the latest doc. https://docs.fabric.io/android/crashlytics/build-tools.html#gradle-advanced-setup.

Apart from adding ext.enableCrashlytics = false in build.grade you need to do,

Crashlytics crashlyticsKit = new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
    .build();

// Initialize Fabric with the debug-disabled crashlytics.
Fabric.with(this, crashlyticsKit);

I found this to be the the easiest solution:

    release {
        ...
        buildConfigField 'Boolean', 'enableCrashlytics', 'true'
    }
    debug {
        buildConfigField 'Boolean', 'enableCrashlytics', 'false'
    }

The above lines will create a static boolean field called enableCrashlytics in the BuildConfig file which you can use to decide whether to initiate Fabric or not:

    if (BuildConfig.enableCrashlytics)
        Fabric.with(this, new Crashlytics());

NOTE: With this method Fabrics is initialised only in release builds (as indicative in the above code). This means you need to put calls to statics methods in the Crashlytics class in an if block which checks whether Fabrics has been initialised as shown below.

if (Fabric.isInitialized())
    Crashlytics.logException(e);

Otherwise the app will crash with Must Initialize Fabric before using singleton() error when testing on the emulator.


2019 Answer

I've been trying to only enable Crashlytics in release and disable in debug for 2 hours, checking the Firebase console to see if the Exceptions where uploaded or not.

There are 2 possible ways to do this.

OPTION 1

It works, but if you call any Crashlytics method on debug builds the app will crash.

app/build.gradle

android {
    buildTypes {
        release {
            manifestPlaceholders = [crashlyticsEnabled: true]
        }
        debug {
            manifestPlaceholders = [crashlyticsEnabled: false]
        }

AndroidManifest.xml

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

OPTION 2

An alternative if that allows you to call Crashlytics methods without checking BuildConfig.DEBUG first. With this setup you can safely call methods like Crashlytics.logException() - they simply do nothing in debug builds. I don't see the reports being uploaded in debug.

app/build.gradle

android {
    buildTypes {
        release {
            ext.enableCrashlytics = true
        }
        debug {
            ext.enableCrashlytics = false
        }

AndroidManifest.xml

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

Application onCreate()

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