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

后端 未结 16 1358
谎友^
谎友^ 2020-12-04 13:42

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-r

16条回答
  •  死守一世寂寞
    2020-12-04 14:49

    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.

提交回复
热议问题