Disable firebase crashlytics for Android

时光总嘲笑我的痴心妄想 提交于 2019-12-05 11:44:04

Disable Crashlytics at runtime

// Set up Crashlytics, disabled for debug builds
    Crashlytics crashlyticsKit = new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
    .build();

    Fabric.with(this, crashlyticsKit);       

Ex

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set up Crashlytics, disabled for debug builds
    Crashlytics crashlyticsKit = new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
    .build();

    Fabric.with(this, crashlyticsKit);
    setContentView(R.layout.activity_main);

}

More

boolean Agrees = value;
if(Agrees)
{
    Fabric.with(this,new Crashlytics());
}
else
 {
   CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(true).build();
   Fabric.with(this,  new Crashlytics.Builder().core(core).build());

throw new RuntimeException("why doesn't this show up in Firebase Crashlytics?");
 }

Edit 2

Ref : Fabric's Crashlytics with Firebase can't be disabled for DEBUG builds

The Firebase Crashlytics documentation explains that once reporting is enabled in an app session, it cannot be disabled.

By default, Crashlytics reporting is enabled in a ContentProvider named CrashlyticsInitProvider that executes before your Application instance is created. CrashlyticsInitProvider enables or disables reporting based on the meta-data value firebase_crashlytics_collection_enabled, which by default is true.

If you want reporting disabled, it's critical that the manifest meta-data be present and set to false:

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

Look in the logcat during app initialization for the message:

CrashlyticsInitProvider: CrashlyticsInitProvider initialization successful

If the message is present, firebase_crashlytics_collection_enabled is true. If the message is not present, you have successfully set the meta-data to disable crash reporting.

If the meta-data is missing or set to true, you cannot disable reporting in your code using a call to Fabric.with(...).

In a comment to another answer, you indicate that you tried disabling reporting using the meta-data and were not successful. Check for a typo and ensure the declaration is correctly placed in the <application> element. In my tests, I am able to disabling reporting using the meta-data and then enable at run time.

FirebaseCrash.setCrashCollectionEnabled(true); is no longer supported in new version of Firebase Crash which is called Crashlytics. For More https://firebase.google.com/docs/crash/

FirebaseCrash.setCrashCollectionEnabled(true);

is replaced by

Fabric.with(this, new Crashlytics());

Take a look https://firebase.google.com/docs/crashlytics/upgrade-from-crash-reporting#set_up_manual_initialization

If you would like to completely disable Firebase Crash reporting AND also not have to add the

com.crashlytics.sdk.android:crashlytics:2.9.1

dependency, then:

Add this to your app's build.gradle:

android {

    // ...

    defaultConfig {
        manifestPlaceholders = [enableCrashReporting:"false"]
        ....
    }

}

and then also add this to your AndroidManifest.xml:

<application ...>

    // ...

    <meta-data 
            android:name="firebase_crashlytics_collection_enabled" 
            android:value="${enableCrashReporting}" />
    <meta-data
            android:name="firebase_analytics_collection_deactivated"
            android:value="true"/>
</application>

In your module build gradle...

    release {
        //true value to send the crashlytics to the firebase
        manifestPlaceholders = [crashlyticsCollectionEnabled: "true"]
    }

    debug {
        //false value to stop sending the crashlytics to the firebase
        manifestPlaceholders = [crashlyticsCollectionEnabled: "false"]
    }

In the manifest

 <meta-data
       android:name="firebase_crashlytics_collection_enabled"
       android:value="${crashlyticsCollectionEnabled}" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!