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

后端 未结 16 1295
谎友^
谎友^ 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:34

    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.

    0 讨论(0)
  • 2020-12-04 14:38

    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);
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-04 14:39

    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'
    }
    
    0 讨论(0)
  • 2020-12-04 14:39

    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());
    
    0 讨论(0)
  • 2020-12-04 14:40

    I guess the recent firebase crashlytics has this implementation.

     FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG)
    
    0 讨论(0)
  • 2020-12-04 14:41

    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 讨论(0)
提交回复
热议问题