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
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());