0

I want to turn off the Crashlytics and Answers for my devices where I develop an app. E.g. attached mobile phone or virtual device. If it was plain java I would set java property. But Android Studio misses such feature. I can see there some install flags but they are not documented.

I found that I can detect running inside Firebase with the following condition

Settings.System.getString(getContentResolver(), "firebase.test.lab") != null

but I have no idea how to set it in Android Studio.

Update:

current code based on Max's answer is

insideFirebase = Settings.System.getString(getContentResolver(), "firebase.test.lab") != null;
if (!insideFirebase) {
    CrashlyticsCore crashlyticsCore = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
    Crashlytics crashlyticsKit = new Crashlytics.Builder().core(crashlyticsCore).build();
    if (BuildConfig.DEBUG) {
        Fabric.with(this, crashlyticsKit);
    } else {
        Fabric.with(this, new Answers(), crashlyticsKit);
    }
}
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • related questions: https://twittercommunity.com/t/can-i-disable-answers-on-my-app-whilst-leaving-crash-reporting-enabled/55249/22, http://stackoverflow.com/questions/31964038/how-to-disable-crashlytics-answers – Leos Literak Jun 25 '16 at 12:32

1 Answers1

2

You can disable crashlytics in gradle only for debug:

android {
    buildTypes {
        debug {
          // Disable fabric build ID generation for debug builds
          ext.enableCrashlytics = false
          ...

And then init crashlytycs in runtime like this:

Crashlytics crashlyticsKit = new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
    .build()

Fabric.with(this, crashlyticsKit);

Here more information in documentation

P.S. You can also provide any flag / variable for build type in gradle product flavors:

productFlavors {
        Dev {
            buildConfigField 'Boolean', 'enableCrashlytics', 'false'
        }

and then check it while running app:

if (BuildConfig.enableCrashlytics == true) {}

UPD: We should use

CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build());

instead Fabric.with(this, crashlyticsKit);

Max Plakhuta
  • 310
  • 2
  • 14
  • What about this shorter code? if (! BuildConfig.DEBUG) { Fabric.with(this, new Answers(), new Crashlytics()) } – Leos Literak Jun 25 '16 at 10:56
  • See updated section. This is for crashlytics 2.3+. With such initialization you don't need any extra checks for crashlytics. It will not crash app in debug when you call crashlytics methods – Max Plakhuta Jun 25 '16 at 11:05
  • My previous comment did not work, it failed with java.lang.IllegalStateException: Must Initialize Fabric before using singleton() at com.crashlytics.android.answers.Answers.getInstance(Answers.java:32). Now I try your solution. – Leos Literak Jun 25 '16 at 11:09
  • Hmm, the Answers still logs my debug build. I can see my debug version in the Fabric dashboard. :-( See updated questions for a source code. How can I turn it off? Thanks – Leos Literak Jun 25 '16 at 11:27
  • The Answers Kit is also included with the Crashlytics Kit. So you don't need to init Crashlytics with `new Answers()`. Try to rid off if-statement and use only `CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build(); Fabric.with(this, new Crashlytics.Builder().core(core).build());` [Here](https://github.com/twitterdev/cannonball-android/tree/master/app/src/main/java/io/fabric/samples/cannonball) is fabtic sample app, but they don't use release-only logic. – Max Plakhuta Jun 25 '16 at 11:44
  • I used your code but it does not work. I ran my app on two devices and I saw two users at Fabric. Probably a bug in Answers. – Leos Literak Jun 25 '16 at 12:31
  • So, there only option: make special version code for debug and disable them in the crashlytics cabinet? (I looked your related links) – Max Plakhuta Jun 25 '16 at 12:35
  • another answer - with the same code: http://stackoverflow.com/questions/38020725/how-to-detect-that-i-run-android-app-from-debugger – Leos Literak Jun 25 '16 at 12:39
  • I use if (! BuildConfig.DEBUG) .. start Fabric.with .. and it works. I must not call any Answer method when it is disabled. – Leos Literak Jun 29 '16 at 05:10