77

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.

4
  • if (!development) { FirebaseCrash.report(e);} Commented May 23, 2016 at 17:15
  • 1
    Thanks, @James_Parsons, but I did not mean that. I need to disable automatic crash reporting, not just the manual calls to the API.
    – facundomr
    Commented May 23, 2016 at 17:19
  • 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? Commented Jan 1, 2017 at 20:46
  • check this answer: stackoverflow.com/a/48384549/3166417
    – itzhar
    Commented Jan 22, 2018 at 15:09

16 Answers 16

48

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.

11
  • 23
    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. Commented May 24, 2016 at 23:32
  • 8
    @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?
    – Alin
    Commented Jul 14, 2016 at 9:42
  • 5
    for more details on this answer, see: firebase.googleblog.com/2016/08/…
    – Frank
    Commented Nov 11, 2016 at 9:35
  • 3
    @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. Commented Jan 29, 2017 at 21:33
  • 7
    With Google Play Services 11.0 you could now disable crash reporting at runtime. FirebaseCrash.setCrashCollectionEnabled(!BuildConfig.DEBUG); Commented Jun 28, 2017 at 20:13
32

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

FirebaseCrash.setCrashCollectionEnabled(!BuildConfig.DEBUG);
1
  • It is now FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false); Commented Sep 23, 2022 at 8:29
28

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"/>
...
6
  • Does the flag firebase_crash_collection_enabled work for version 9.2.1 of Firebase?
    – rraallvv
    Commented Feb 22, 2018 at 18:14
  • @rraallvv no, the feature was not yet available
    – evi
    Commented Feb 23, 2018 at 20:04
  • 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.
    – Leon
    Commented Feb 28, 2018 at 10:34
  • @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!
    – evi
    Commented Mar 11, 2018 at 10:51
  • Not "firebase_crash_collection_enabled" but "firebase_crashlytics_collection_enabled" - from firebase.google.com/docs/crashlytics/customize-crash-reports Commented Jul 5, 2018 at 8:40
11

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

0
11

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'
1
  • 6
    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.
    – Joel
    Commented Jan 18, 2017 at 4:12
9

The simple and easy trick I used is to add firebase crash reporting dependency in release build only in build.gradle file.

This will remove crash reporting library from debug build type and add this in release build only.

dependencies {
    releaseCompile 'com.google.firebase:firebase-crash:10.2.0'
}
7

Inspired by this related answer and others here, I came up with this handy solution.

Using Timber for logging, I created different implementations of a Tree subclass for debug and release builds. In debug, it defers to DebugTree which writes to logcat. In release, it forwards exceptions and high-priority logs to Firebase, dropping the rest.

build.gradle

dependencies {
  ...
  compile 'com.jakewharton.timber:timber:4.3.0'
  releaseCompile 'com.google.firebase:firebase-crash:9.0.2'
}

src/debug/java/[package]/ForestFire.java

import timber.log.Timber;

public class ForestFire extends Timber.DebugTree {}

src/release/java/[package]/ForestFire.java

import android.util.Log;
import com.google.firebase.crash.FirebaseCrash;
import timber.log.Timber;

public class ForestFire extends Timber.Tree {
  @Override
  protected void log(int priority, String tag, String message, Throwable t) {
    if (Log.WARN <= priority) {
      FirebaseCrash.log(message);
      if (t != null) {
        FirebaseCrash.report(t);
      }
    }
  }
}

App startup

Timber.plant(new ForestFire());
6

First initialize variables in the gradle file and check whether it is in debug or in release mode. The best way to submit the crash report is within the Application class.

Build.gradle

    buildTypes {
         release {
             buildConfigField "Boolean", "REPORT_CRASH", '"true"'
             debuggable false
         }
         debug {
             buildConfigField "Boolean", "REPORT_CRASH", '"false"'
             debuggable true
         }
    }

Now first check the mode and submit the crash report if crashed .

Application.java

    /** Report FirebaseCrash Exception if application crashed*/
    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
    {
        @Override
        public void uncaughtException (Thread thread, Throwable e)
        {
            /** Check whether it is development or release mode*/
            if(BuildConfig.REPORT_CRASH)
            {
                FirebaseCrash.report( e);
            }
        }
    });
1
  • 10
    for the first part you can simply use BuildConfig.DEBUG
    – Bronx
    Commented Nov 18, 2016 at 8:42
4

Currently you can't disable firebase crash reporting although you can deactivate firebase analytics.

So one way to do this is creating another app with different ID within same firebase project. After this you just need to change appID to enable or disable firebase crash reporting. I created below two app for my convenience:

AppID:com.android - For release build type

AppID:com.android.debug - For debug build type

Please follow below link for more details:

https://firebase.googleblog.com/2016/08/organizing-your-firebase-enabled-android-app-builds.html

Edit: You don't need to change appID in android project again and again. There is a better way to use different appID for debug build type-

android {
    defaultConfig {
        applicationId "com.android"
        ...
    }
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
    }
}

Checkout the link for more details:

https://developer.android.com/studio/build/application-id.html

Edit2:

Basically in above solution you are making two different app in Firebase project, And this way you can separate your development and production errors.

FYI Firebase crash reporting is deprecated. You should use Fabrics Crashlytics (Owned by Google). It has some really cool features.

2

For FirebaseAnalytics class.
Disable collection: setAnalyticsCollectionEnabled(false);
Enable collection: setAnalyticsCollectionEnabled(true); or write to AndroidManifest.xml in the application tag: <meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />

Possible use:

if (BuildConfig.DEBUG){ //disable for debug
    mFirebaseAnalytics.setAnalyticsCollectionEnabled(false);
}

Source

1

First you will have to create debug and release build variants and then set a variable with boolean value. Then you will need to get that value from your java file which extends application i.e from where you enable Fabric crash reporting.

A code example is given below.

In your app's build.gradle file, add the following lines to create 2 build variants debug and release and then add a variable with boolean value.

defaultConfig {
    buildConfigField 'boolean', 'ENABLE_ANALYTICS', 'true'
}

buildTypes {
    debug {
        applicationIdSuffix ".debug"
        versionNameSuffix 'DEBUG'
        buildConfigField 'boolean', 'ENABLE_ANALYTICS', 'false'
    }
    release {
        minifyEnabled false
    }
}

Then when you are trying to add Fabric crash reporting check the value for ENABLE_ANALYTICS

public class Test extends Application {

private GoogleAnalytics googleAnalytics;
private static Tracker tracker;

@Override
public void onCreate() {
    super.onCreate();
    if (BuildConfig.ENABLE_ANALYTICS)
        Fabric.with(this, new Crashlytics());
    }
}

You can see the value for ENABLE_ANALYTICS by ctrl + click on the value. Hope this helps.

1

The simplest solution for users when they run app in Debug mode or in Release mode:

AndroidManifest.xml:

<meta-data
            android:name="firebase_crash_collection_enabled"
            android:value="${analytics_deactivated}"/>

build.gradle(Module:app)

buildTypes {

        debug {
            manifestPlaceholders = [analytics_deactivated: "false"]
        }

        release {
            manifestPlaceholders = [analytics_deactivated: "true"]

        }
    }

Hence, when the app is in release mode, the crashlatics will be turned on and app runs in debug mode, it would be turned off.

1

I guess the recent firebase crashlytics has this implementation.

 FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG)
0

I use versionCode as filter for local/production builds.

gradle.properties

VERSION_CODE=1

app/build.gradle

android {
    defaultConfig {
        versionCode VERSION_CODE as int
    }
}

When publishing new version of app, just set new value from command line:

./gradlew build -PVERSION_CODE=new_value

Otherwise, when you're building from Android Studio, you will always get the same versionCode, so you can easily distinguish crash reports in Firebase console.

0

As has been said before - there is no official way to do this. But the worst workaround for me as mentioned @mark-d is to reset DefaultUncaughtExceptionHandler (https://stackoverflow.com/a/39322734/4245651).

But if you just call System.exit(2) as was suggested - the app will be instantly closed on exception, without any dialog message and hard getting debug logs. If this is important to you, there is a way to restore default handler:

if (BuildConfig.DEBUG) {
        final Thread.UncaughtExceptionHandler currentHandler = Thread.getDefaultUncaughtExceptionHandler();
        if (currentHandler.getClass().getPackage().getName()
                                                .startsWith("com.google.firebase")) {
            final Thread.UncaughtExceptionHandler defaultHandler = 
                getPrivateFieldByType(currentHandler, Thread.UncaughtExceptionHandler.class);
            Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
        }
}

Where

public static <T> T getPrivateFieldByType(Object obj, Class<T> fieldType) {
    if (obj != null && fieldType != null) {
        for (Field field : obj.getClass().getDeclaredFields()) {
            if (field.getType().isAssignableFrom(fieldType)) {
                boolean accessible = field.isAccessible();
                if (!accessible) field.setAccessible(true);
                T value = null;
                try {
                    //noinspection unchecked
                    value = (T) field.get(obj);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if (!accessible) field.setAccessible(false);
                return value;
            }
        }
    }
    return null;
}
0
public class MyApp extends Application {
    public static boolean isDebuggable;

    public void onCreate() {
        super.onCreate();
        isDebuggable = (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
        FirebaseCrash.setCrashCollectionEnabled(!isDebuggable);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.