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

后端 未结 16 1402
谎友^
谎友^ 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: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());
    

提交回复
热议问题