3

I have a project with multiple modules. The common code of the modules is in a library module. The problem is that we added recently Crashlytics to our project (in the library module), and we keep receiving error reports even when we are in Debug mode.

I searched on the internet and I found out that a library is always seen as a Release mode. Now my question is, is there a way to disable Crashlytics in my case?

halfer
  • 19,824
  • 17
  • 99
  • 186
Laura
  • 2,653
  • 7
  • 37
  • 59
  • Check out stackoverflow.com/questions/35312743 if you want to remove the "release only" restriction on your library module. I ran into the same issue on my project. – jwBurnside Feb 14 '16 at 17:11

2 Answers2

12

In my app (a single module, multiple flavors), I detect the flavor, and only initialize Crashlytics in the flavors that I want.

In my case, I add a variable to the flavor in build.gradle , like so:

productFlavors {
        Dev { // i.e. gradlew assembleDevDebug
            buildConfigField 'Boolean', 'enableCrashlytics', 'false'
        }

        Qa { // i.e. gradlew assembleQaDebug
            buildConfigField 'Boolean', 'enableCrashlytics', 'true'
        }
}

Then, in my Application class, I conditionally start Crashlytics:

if(BuildConfig.enableCrashlytics == true) {
   Fabric.with(this, new Crashlytics());
}
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • 1
    I've seen this answer at several places on stack, but it causes a crash anywhere I call Crashlytics.log(). Can anyone confirm this? – jwBurnside Feb 14 '16 at 17:07
  • @jwBurnside I recognize this is years late, but will state this for future readers: yes, calling Crashlytics.log() without first calling Fabric.with() WILL crash the app, so I do not advise the approach of saying "we'll only initialize Crashlytics in release builds". – Chad Schultz Jul 26 '16 at 17:33
  • This is a good point. I keep track (in my Application class) of whether Crashlytics has been initialized or not, and only call Crashlytics.log() if it has been. Actually, I use Timber for logging, and have different logging "Trees" for dev and prod. The prod tree calls Crashlytics.log() when logging an error, and the dev log does not. – GreyBeardedGeek Jul 26 '16 at 18:58
  • I disable Crashlytics in this manner CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build(); Fabric.with(this, new Crashlytics.Builder().core(core).build()); – Kamlesh Jul 11 '17 at 12:25
6

Assuming you enable Crashlytics/Fabric from your main module (which is recognised as being in debug), just conditionally initialise it so it does not activate in Debug mode.

e.g

if (!BuildConfig.DEBUG) {
    Fabric.with(this, new Crashlytics()); 
}
Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • Yes, I now I should check if it is in debug mode or not, but this is the problem. A library module is always seen as in release. – Laura Feb 25 '15 at 15:53
  • But as I said, you are running the above code from your main app not from your library. – Kuffs Feb 25 '15 at 21:14