270

Is there any simple way to turn Crashlytics Android SDK off while developing ?

I don't want it to send a crash every time I do something stupid

On the other hand I don't want to comment out Crashlytics.start() and possibly risk forgetting to uncomment it and commit

Michael
  • 22,196
  • 33
  • 132
  • 187
  • Have you tried just removing your api key from manifest, I don't remember if thats a crash. – Timmetje Jun 07 '13 at 14:48
  • @timmied It crashes. Also commenting the whole line in the `Manifest` crashes the app so this makes the question a little more legit. – Michael Jun 07 '13 at 14:59

31 Answers31

399

I found the solution from Crashlytics (with Fabric integration)

Put following code inside your Application class onCreate()

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

EDIT:

In Crashalitics 2.3 and above, this is deprecated. The correct code is:

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

or

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

(copied from Crashlytics deprecated method disabled())


EDIT2:

You can also optionally add this to your buildType in gradle. This command disables sending the crashlytics mapping file and generating an ID for each build, which speeds up gradle builds of those flavors. (It doesn't disable Crashlytics at run time.) See Mike B's answer here.

buildTypes {
    release {
           ....
    }
    debug {
        ext.enableCrashlytics = false
    }
}
Community
  • 1
  • 1
xialin
  • 7,686
  • 9
  • 35
  • 66
  • 2
    This is much nicer to use and will stop your app crashing if you makes calls to Crashlytics in your code, outside of your Application class. – speedynomads Jan 13 '15 at 12:31
  • It works great! Now I can remove all BuildConfig.DEBUG check on all my analytic wrapper methods without fear of crashes. – akhy Feb 02 '15 at 08:03
  • 1
    It got deprecated in Crashlytics 2.3.0 :( – Damian Walczak May 27 '15 at 13:59
  • @DamianWalczak feel free to send the support team an Email for new answer then. They're responding fast! – xialin May 28 '15 at 01:55
  • For projects that use new Fabric-powered Crashlytics this should be the marked answer as correct and most straightforward, IMO. P. S.: just was that it's deprecated. @DamianWalczak Any info on what should be used instead? – Den Drobiazko Jun 10 '15 at 08:25
  • the method in **EDIT2** not worked for me, anyone has the same problem? When I move it into flavors config, Crashlytics will throw `CrashlyticsMissingDependencyException` – CzBiX Jun 24 '15 at 02:36
  • 1
    `ext.enableCrashlytics = false` does not work for me either with 2.5. Actually, it never worked. Even before Fabric. – Bao-Long Nguyen-Trong Aug 05 '15 at 23:46
  • I am disabling it at RunTime but Edit 1 is not working for me. – Shajeel Afzal Aug 23 '15 at 16:02
  • 2
    I have a concern here. Will this enable Answer and Beta ? It looks like this should be more correct : CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build(); Fabric.with(this, new Answers(), new Beta(), new Crashlytics.Builder().core(core).build()); – gbero Nov 25 '15 at 15:32
  • 1
    ext.enableCrashlytics = false does not crash if you use this correctly. How to overcome the crash is in Fabrics documentation: https://docs.fabric.io/android/crashlytics/build-tools.html. – Frank Jan 29 '16 at 08:34
  • Fabric say that I must do ", new Crashlytics()" in this case can it be ignored ? – An-droid Mar 22 '16 at 12:43
  • 1
    in addition to this answer, I had to add followings in strings.xml file, @string/RANDOM_UUID as per the documentation, this is generated by the plugin but I never used plugin because of personal preference. – Dhunju_likes_to_Learn Apr 01 '16 at 01:29
  • 1
    ext.enableCrashlytics = false does not work for me either with 2.6 – Gabriel Ferreira Aug 13 '16 at 23:49
  • 1
    If anyone wonders, this solution will not disable Answers and Beta. –  Dec 23 '16 at 18:12
  • **This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up, install an Android build tool and ask a team member to invite you to this app's organization.** – Iman Marashi May 06 '19 at 14:51
  • @gbero I also had this concern. But when I initialize Crashlytics like mentioned in the answer it seems like answers and beta are initialized correctly `Initializing io.fabric.sdk.android:fabric [Version: 1.4.8.32], with the following kits: com.crashlytics.sdk.android:crashlytics [Version: 2.10.1.34] com.crashlytics.sdk.android:beta [Version: 1.2.10.27] com.crashlytics.sdk.android:answers [Version: 1.4.7.32] com.crashlytics.sdk.android.crashlytics-core [Version: 2.7.0.33]` – Wirling Oct 09 '19 at 07:09
  • @xialin Any updated method to disable Firebase Crashlytics reporting in BUILD mode – Marsad Mar 24 '20 at 17:17
  • 1
    **this works for me**--> in manifest, `` in gradle, `buildTypes { release { manifestPlaceholders["crashlyticsCollectionEnabled"] = true}` `debug {manifestPlaceholders["crashlyticsCollectionEnabled"] = false}` – kadirgun Mar 17 '23 at 07:52
176

Marc from Crashlytics here. Here's a couple of ways to disable Crashlytics while you are doing your debug builds!

  1. Use a different android:versionString for debug and release builds and then disable crash reporting from the Crashlytics web dashboard for the debug version.

  2. Wrap the call to Crashlytics.start() in an if statement that checks a debug flag. You could use either a custom flag or an approach like the ones proposed here: How to check if APK is signed or "debug build"?

Community
  • 1
  • 1
marcr
  • 2,800
  • 1
  • 18
  • 7
  • 6
    @marcr How about just using BuildConfig.DEBUG? – dannyroa Dec 06 '13 at 07:18
  • 3
    @dannyroa BuildConfig.DEBUG is not a standard flag that works across all build environments. I believe it is set consistently when building with Eclipse & ADT, but not elsewhere. – marcr Jan 03 '14 at 19:41
  • 1
    @marcr it would be good if we can disable task in gradle for specific flavors/types. this way we can save time on compiling resources – robotoaster Jan 27 '14 at 19:22
  • @marcr I see `Crashlytics.getInstance().setDebugMode(debug)` Does this have any relevance to the original question? – Kyle Ivey Mar 24 '14 at 05:23
  • 14
    `BuildConfig.DEBUG` should be used if you are building using Gradle. It will always be generated properly. – Austyn Mahoney May 22 '14 at 01:02
  • 3
    @marcr how about in the latest version of crashlytics (as it seems merged with Fabric), does the library make internal check to `BuildConfig.DEBUG`? – akhy Jan 16 '15 at 04:38
  • 2
    @akhyar It is not checked automatically, I use: if (!BuildConfig.DEBUG) {Fabric.with(this, new Crashlytics());} – Björn Kechel Sep 29 '15 at 16:25
  • 2
    @marcr don't you think Crashlytics should provide methods to enable/disable Crashlytics at rumtime. For example see this question http://stackoverflow.com/questions/32168754 –  Sep 30 '15 at 13:52
  • Just in case someone is looking how to implement option #1 for iOS. Run script phase to change version string from `1.0` to `1.0-debug`: `versionString=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${INFOPLIST_FILE}")` and `/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $versionString-debug" "${BUILT_PRODUCTS_DIR}/${INFOPLIST_PATH}"` – Roman B. Sep 08 '17 at 01:55
  • 2
    The second method is wrong because `Crashlytics.logException(e);` throws an exception in debug builds because Fabric singleton is not initialized. – naXa stands with Ukraine Sep 08 '18 at 17:50
  • Hi Marc I am facing following error related to crashlytics, Can you please check* What went wrong: Execution failed for task ':quickRide:packageQrReleaseBundle'. > java.util.concurrent.ExecutionException: com.android.tools.build.bundletool.exceptions.ValidationException: Modules 'base' and 'AdharVerificationDynamicFeature' contain entry 'assets/crashlytics-build.properties' with different content. – Prakash Apr 15 '20 at 08:06
50

The chosen answer is not correct any more. Google changed the integration of Crashlytics. My current version is 2.9.1 and the only thing I had to do is, to add implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' to my Gradle file. No further things required, nice but this means that Crashlytics is always running.

Solution 1

Only compile Crashlytics in release version:

dependencies {
   ...
   releaseImplementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' // update version
}

Solution 2

If you want to additionally configure Crashlytics then Solution 1 is not working, since the Crashlytics classes will not be found in Debug Builds. So change the Gradle implementation back to:

implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' // update version

Then go to your Manifest and add the following meta-data tag inside the application tag:

<application
        android:name="...>

        <meta-data
            android:name="firebase_crashlytics_collection_enabled"
            android:value="false" />

...

</application>

Add to your Launch-Activity (only one-time required, not every Activity)

if (!BuildConfig.DEBUG) { // only enable bug tracking in release version
   Fabric.with(this, new Crashlytics());
}

This will only enable Crashlytics in release versions. Be careful, also check for BuildConfig.DEBUG when you then configure Crashlytics, E.g:

if (!BuildConfig.DEBUG) {
   Crashlytics.setUserIdentifier("HASH_ID");
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Paul Spiesberger
  • 5,630
  • 1
  • 43
  • 53
  • 2
    This seems clean. Instead of initializing in the primary activity, when not in the Application instance instead? – jules May 02 '18 at 01:40
  • They state on the website: `Enable collection for selected users by initializing Crashlytics from one of your app's activities` but I guess it doesn't change much if you initialize Crashlytics in the application. Did you try it? If it works, then I can add it to my answer. https://firebase.google.com/docs/crashlytics/customize-crash-reports – Paul Spiesberger May 02 '18 at 06:11
  • 2
    I couldn't get any of the other solutions to work, in order to disable crashlytics at runtime. Solution 1 just worked perfectly - why didn't I think of that. –  Jun 10 '18 at 10:09
  • Thanks for the solution. When I set `firebase_crashlytics_collection_enabled` to false in manifest the crash does not appear on console (I use v2.9.9). So I fixed this by adding separate manifest for debug build with `firebase_crashlytics_collection_enabled=false` and `true` for release – Vasily Kabunov Apr 16 '19 at 04:24
30

If you use Gradle just add this to a flavor:

ext.enableCrashlytics = false
user1998494
  • 614
  • 7
  • 7
  • 1
    that's only for flavor? what about debug vs. release? I tried to disable for debug but still send the crash – xialin Nov 25 '14 at 04:24
  • I think it only works on flavors. IMO using the flag Austyn and Marcc pointed is the easiest. – user1998494 Nov 27 '14 at 13:43
  • I found out the solution. but not sure if it's compatible for old Crashlytics. it's for the new Crashlytics in Fabric SDK. check my answer below – xialin Nov 28 '14 at 14:59
  • 2
    This command disables sending the crashlytics mapping file and generating an ID for each build, which speeds up gradle builds of those flavors. (It doesn't disable Crashlytics at run time.) See Mike B's answer here: http://stackoverflow.com/questions/28339323/disable-importing-of-crashlytics/28357998#28357998 – Aphex Dec 02 '15 at 14:33
  • 19
    This caused a crash... "`This app relies on Crashlytics.`" – Sakiboy Jan 29 '16 at 17:52
  • It caused the same crash for me, so I changed it to disable on Application's onCreate method as suggest on the answer above and it worked. – cristianorbs May 12 '16 at 18:55
28

Check out the latest doc. https://docs.fabric.io/android/crashlytics/build-tools.html#gradle-advanced-setup.

Apart from adding ext.enableCrashlytics = false in build.grade you need to do,

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

// Initialize Fabric with the debug-disabled crashlytics.
Fabric.with(this, crashlyticsKit);
TooCool
  • 10,598
  • 15
  • 60
  • 85
Abhishek Patidar
  • 1,557
  • 1
  • 16
  • 25
  • I tried this way, and still app crashes with `This app relies on Crashlytics. Please sign up for access at` – Kostadin Georgiev Oct 17 '18 at 14:00
  • I guess you are missing `ext.enableCrashlytics = false` in build.gradle. – Abhishek Patidar Oct 17 '18 at 14:14
  • No, i've added in the debug build type in build.gradle file, it's in `buildTypes -> debug, also i'm applying the plugin via `apply plugin: 'io.fabric'` – Kostadin Georgiev Oct 17 '18 at 14:33
  • I am not sure why this solution was even upvoted 24 times. Crashes with `This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up` – trod Feb 18 '19 at 23:24
24

I found this to be the the easiest solution:

    release {
        ...
        buildConfigField 'Boolean', 'enableCrashlytics', 'true'
    }
    debug {
        buildConfigField 'Boolean', 'enableCrashlytics', 'false'
    }

The above lines will create a static boolean field called enableCrashlytics in the BuildConfig file which you can use to decide whether to initiate Fabric or not:

    if (BuildConfig.enableCrashlytics)
        Fabric.with(this, new Crashlytics());

NOTE: With this method Fabrics is initialised only in release builds (as indicative in the above code). This means you need to put calls to statics methods in the Crashlytics class in an if block which checks whether Fabrics has been initialised as shown below.

if (Fabric.isInitialized())
    Crashlytics.logException(e);

Otherwise the app will crash with Must Initialize Fabric before using singleton() error when testing on the emulator.

Dennis Zinkovski
  • 1,821
  • 3
  • 25
  • 42
fahmy
  • 3,543
  • 31
  • 47
24

2019 Answer

I've been trying to only enable Crashlytics in release and disable in debug for 2 hours, checking the Firebase console to see if the Exceptions where uploaded or not.

There are 2 possible ways to do this.

OPTION 1

It works, but if you call any Crashlytics method on debug builds the app will crash.

app/build.gradle

android {
    buildTypes {
        release {
            manifestPlaceholders = [crashlyticsEnabled: true]
        }
        debug {
            manifestPlaceholders = [crashlyticsEnabled: false]
        }

AndroidManifest.xml

<manifest
    <application
        <meta-data
            android:name="firebase_crashlytics_collection_enabled"
            android:value="${crashlyticsEnabled}" />

OPTION 2

An alternative if that allows you to call Crashlytics methods without checking BuildConfig.DEBUG first. With this setup you can safely call methods like Crashlytics.logException() - they simply do nothing in debug builds. I don't see the reports being uploaded in debug.

app/build.gradle

android {
    buildTypes {
        release {
            ext.enableCrashlytics = true
        }
        debug {
            ext.enableCrashlytics = false
        }

AndroidManifest.xml

<manifest
    <application
        <meta-data
            android:name="firebase_crashlytics_collection_enabled"
            android:value="false" />

Application onCreate()

val crashlytics = Crashlytics.Builder()
    .core(CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
    .build()
Fabric.with(this, crashlytics)
Sarankumar
  • 60
  • 1
  • 8
Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
  • 2
    I think `android:value="false"` has to be changed to `android:value="${enableCrashlytics}"`. Isnt it? – JaydeepW Jul 29 '19 at 07:44
  • Another advantage with Option #2 is that you start with analytics collection initially disabled, and then can ask the user first it it's OK to track their app usage (think GDPR). Then you'd only do the Fabric.with call if the user consented to tracking. – Per Christian Henden Feb 03 '20 at 12:56
  • The only downside of option #2, is that it still creates debug versions in Firebase dashboard, even though no crashes will be shown there (since they are disabled). This makes two problems - first, it makes harder to find release versions; second - Firebase dashboard shows [only last 100 versions](https://firebase.googleblog.com/2019/03/crashlytics-versions.html) - which might prevent you from seeing crashes in some old versions. In Fabric dashboard you could disable a particular version, [this is not possible](https://stackoverflow.com/a/59963555/1233652) in Firebase's dashboard. – Alex Lipov Apr 26 '20 at 15:34
15

Use this in MyApplication#onCreate()

if (!BuildConfig.DEBUG) Crashlytics.start(this);

EDIT If you upgraded to Fabric, use this answer instead.

Community
  • 1
  • 1
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
  • BuildConfig.DEBUG isn't always set properly. Relying on it to enable/disable Crashlytics caused quite a few problems for me while using IntelliJ. – Zeb Barnett Jun 06 '14 at 08:07
  • 5
    What build tools are you using? Gradle will ALWAYS set that value. It was a problem a year ago, but the new build tools are much better. – Austyn Mahoney Jun 10 '14 at 20:54
  • I'm on v0.9.+ of the Gradle plugin for IntelliJ and v1.11 for Gradle itself. – Zeb Barnett Jun 10 '14 at 21:31
  • I have not seen any issues in any of my apps. `BuildConfig` is generated by a Gradle task, which is guaranteed to run. I also use `buildConfigField` to set custom fields and those always work. http://tools.android.com/recent/androidstudio045released also suggests that you use `BuildConfig.DEBUG`. – Austyn Mahoney Jun 10 '14 at 21:44
  • As an idealist I sure would like to be able to use it as it would simplify the not-quite-automated build process for the small company I work for. It's just that we released into production a build that was depending on that flag and Crashlytics never saw it go live. After we went back to toggling it manually Crashlytics saw it immediately. – Zeb Barnett Jun 10 '14 at 22:00
  • Given your experience contradicts what the actual documentation says, I am going to continue using the flag. I've seen plenty of other open source applications using it also. – Austyn Mahoney Jun 10 '14 at 22:43
11

Another simple solution that I like, because it doesn't require different manifest files:

Step 1 - define manifest placeholders in build.gradle

android {
    ...
    buildTypes {
        release {
            manifestPlaceholders = [crashlytics:"true"]
        }
        debug {
            manifestPlaceholders = [crashlytics:"false"]
        }
    }
    ...
}

Step 2 - use them in your AndroidManifest.xml

<meta-data
        android:name="firebase_crashlytics_collection_enabled"
        android:value="${crashlytics}" />
arbuz
  • 2,950
  • 1
  • 18
  • 16
10

As per google use this code for disable Crashlytics and it will also improve build process.

enter image description here

reference-https://developer.android.com/studio/build/optimize-your-build

Lovekush Vishwakarma
  • 3,035
  • 23
  • 25
  • 2
    Unfortunately, that documentation is deprecated, and the flags that you are referencing are not supported anymore, check it here: https://firebase.google.com/docs/crashlytics/upgrade-sdk?hl=da&platform=android – Max Cruz Jul 12 '20 at 10:23
10

2022 answer using FirebaseCrashlytics.

There are two cases:

  1. If you want to disable Crashlytics data collection for all app runs (such as disabling Crashlytics in debug mode), then you need to disable it the manifest file by setting the flag firebase_crashlytics_collection_enabled to false

build.gradle (:app)

// Next two flags to enable/disable Crashlytics
def enableCrashlyticsInDebugBuild = false
def enableCrashlyticsInReleaseBuild = true


android {
  buildTypes {

        release {
                manifestPlaceholders = [crashlyticsEnabled:"${enableCrashlyticsInReleaseBuild}"]
        }

        debug {
                manifestPlaceholders = [crashlyticsEnabled:"${enableCrashlyticsInDebugBuild}"]
        }
  }
}

Then in the manifest file add this under the application tag.

<meta-data
            android:name="firebase_crashlytics_collection_enabled"
            android:value="${crashlyticsEnabled}" />
  1. If you want to disable Crashlytics for some users when they opt out from data collection.

To handle that you should use the method setCrashlyticsCollectionEnabled.

Kotlin API (The Java API is similar):

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false)

When set to false, the new value does not apply until the next run of the app.

References:

  1. https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#enable-reporting

  2. https://firebase.google.com/docs/reference/kotlin/com/google/firebase/crashlytics/FirebaseCrashlytics#setcrashlyticscollectionenabled

Mahmoud
  • 2,683
  • 1
  • 30
  • 32
  • This worked for me. Before, I had been using `extra["enableCrashlytics"] = false` which wasn't working. – Mark Jul 19 '22 at 01:01
  • Please correct to manifestPlaceholders["crashlyticsEnabled"] = false/true. Also using a variable doesn't adds value to the solution in this concrete case. – David Jan 13 '23 at 19:28
  • @David Adding the flag give the developer more control to enable/disable without going into details internally. The variable is descriptive. Change it directly and go. N need to check and read the code to know where to update. – Mahmoud Jan 17 '23 at 13:45
8

If you want to capture all crashes (for debug and release builds) but want to separate them in the Crashlytics Dashboard, you can add this line of code to build.gradle:

debug {
    versionNameSuffix "-DEBUG"
}

For example, if your app's versionName is 1.0.0, your release builds will be tagged as 1.0.0 while debug builds will be 1.0.0-DEBUG

Khronos
  • 377
  • 6
  • 17
6

Notice that you can also disable the annoying uploading of symbols in debug build:

def crashlyticsUploadStoredDeobsDebug = "crashlyticsUploadStoredDeobsDebug"
def crashlyticsUploadDeobsDebug = "crashlyticsUploadDeobsDebug"
tasks.whenTaskAdded { task ->
    if (crashlyticsUploadStoredDeobsDebug.equals(task.name) ||
            crashlyticsUploadDeobsDebug.equals(task.name)) {

        println "Disabling $task.name."
        task.enabled = false
    }
}

Just put it into the build.gradle of your application module.

Ariel Cabib
  • 2,042
  • 22
  • 14
6

There are plenty of good answers here, but for my testing I use debug builds for in-house betas and out-of-the-lab testing where crash logs are still very useful and I would still like to report them. Like the OP, all I wanted was to disable them during active development where I am often causing and quickly resolving crashes.

Rather than remove ALL debug crashes you can choose to only disable the reports while a device is connected to your development machine with the following code.

if (!Debug.isDebuggerConnected()) {
    Fabric.with(this, new Crashlytics());
}
Andrea Thacker
  • 3,440
  • 1
  • 25
  • 37
  • It's wrong. I log non-fatal exceptions in my code using `Crashlytics.logException(e)` and this statement throws an exception in debug builds because Fabric singleton is not initialized. If you use Crashlytics then always initialize Fabric singleton. See [fahmy's answer](https://stackoverflow.com/a/30837601/1429387). – naXa stands with Ukraine Sep 08 '18 at 17:54
5

The problem is that none of the solutions does work for the latest crashlytics sdk. (I'm using 2.9.0)

You can't disable it by code since it compiles into your project and runs before even a call onCreate of your application. So other solution is simple - don't compile crashlytics when not needed. Replace the 'compile' call with 'releaseCompile' within build.gradle file.

 releaseCompile('com.crashlytics.sdk.android:crashlytics:2.9.0@aar') {
        transitive = true
    }
Yan
  • 1,569
  • 18
  • 22
5

There are two options in order to disable Firebase Crashlytics for the following version com.google.firebase:firebase-crashlytics:17.0.0:

  1. Add meta-tag to app's Manifest

<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />

OR

  1. Configure directly in the app (keep in mind when set to false, the new value does not apply until the next run of the app)

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)

Viktor Ivanov
  • 139
  • 3
  • 6
4

Create a class that extends from Application if you don't already have one in your application.

Then do the following :

public class BaseApplication extends Application { // extend from MultidexApplication if multidex is required by your application 

    @Override
    public void onCreate() {
        super.onCreate();

        /*
         *  Crashlytics is enabled by default,
         *  Disable it for debug builds & USB Debugging
         * */

        if(BuildConfig.DEBUG){ 
            FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false);
        }
  }
}

and don't forget to add the class in your AndroidManifest.xml

 <application
        android:name=".BaseApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        ........>
Niaz Ahmed
  • 220
  • 2
  • 12
4

Updated answer: Disable crashlytics from the gradle config to benefit from improved build speeds.

android {
    ...
    buildTypes {
        debug {
            ext.enableCrashlytics = false
        }
    }
}

or kotlin kts:

android {
    ...
    buildTypes {
        getByName("debug") {
            extra["enableCrashlytics"] = false
        }
    }
}

and then programatically:

// Initializes Fabric for builds that don't use the debug build type.
Crashlytics.Builder()
        .core(CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
        .build()
        .also { crashlyticsKit ->
            Fabric.with(this, crashlyticsKit)
        }

You can also keep using crashlytics on debug builds but still benefit from improvements in build speed by preventing crashlytics from generating a new ID for each build:

android {
    ...
    buildTypes {
        getByName("debug") {
            extra["alwaysUpdateBuildId"] = false
        }
    }
}

or groovy:

android {
    ...
    buildTypes {
        debug {
            ext.alwaysUpdateBuildId = false
        }
    }
}

Check the documentation link below: https://developer.android.com/studio/build/optimize-your-build#disable_crashlytics

Ahmed Mkaouar
  • 638
  • 6
  • 4
3

Up to date easiest version when using Gradle to build:

if (!BuildConfig.DEBUG) {
    Fabric.with(this, new Crashlytics());
}

It uses the new Built-In Syntax from Fabric for Crashlytics and works automatically with the a Gradle build.

Björn Kechel
  • 7,933
  • 3
  • 54
  • 57
3

A strange problem that I encountered: I followed xialin's answer (which also appears on the official website) and it didn't work. Turned out that I was referencing BuildConfig in Fabric's package which also contains a static DEBUG variable that was set to false even in debug mode.

So, if you follow the aforementioned solution and you still get debug reports, make sure that you're referencing this:

import com.yourpackagename.BuildConfig;

And not this:

import io.fabric.sdk.android.BuildConfig;    
Neria Nachum
  • 1,519
  • 1
  • 20
  • 37
2

If you're worried about BuildConfig.DEBUG not being set up correctly, use ApplicationInfo instead:

boolean isDebug = ( mAppContext.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) != 0;
Crashlytics crashlytics = new Crashlytics.Builder().disabled( isDebug ).build();
Fabric.with( uIContext, crashlytics );
Vaiden
  • 15,728
  • 7
  • 61
  • 91
2

Use flavors or build configs. Use a separate build identifier for dev build and all your crashes will keep going to a separate app. Can come handy in case of sharing the build with peers or using it without a debugger. Something like this -

    productFlavors {
    dev {
        applicationId "io.yourapp.developement"
    }
    staging {
        applicationId "io.yourapp.staging"
    }

    production {
        applicationId "io.yourapp.app"
    }
ribhu
  • 171
  • 1
  • 5
2

If you want a debuggable release build, here's the way:

buildTypes {
    release {
        signingConfig signingConfigs.config
        debuggable true //-> debuggable release build
        minifyEnabled true
        multiDexEnabled false
        ext.enableCrashlytics = true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField 'boolean', 'BUILD_TYPE_DEBUG', 'false'
    }
    debug {
        minifyEnabled false
        multiDexEnabled true
        ext.enableCrashlytics = false
        ext.alwaysUpdateBuildId = false
        // Disable fabric build ID generation for debug builds
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField 'boolean', 'BUILD_TYPE_DEBUG', 'true'
    }
}

When you set debuggable true your BuildConfig.DEBUG will initialized with true, that's why I added that variable in BuildConfig class.

Init Fabric:

Crashlytics crashlytics = new Crashlytics.Builder()
            // disable crash reporting in debug build types with custom build type variable
            .core(new CrashlyticsCore.Builder().disabled(BuildConfig.BUILD_TYPE_DEBUG).build())
            .build();

    final Fabric fabric = new Fabric.Builder(this)
            .kits(crashlytics)
            //enable debugging with debuggable flag in build type 
            .debuggable(BuildConfig.DEBUG)
            .build();

    // Initialize Fabric with the debug-disabled crashlytics.
    Fabric.with(fabric);
M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
2

2020 Post Fabric Answer

Paste the code below in your Application class and call the method setCrashlyticsState from your application onCreate. You may optionally add your test device Ids to the debugDevices HashSet too so that your personal devices can be ignored, even when building in release mode.

Note. The device id returned by Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID); is not guaranteed to be unique or constant (It can change on a factory reset or manually on a rooted device). But it should be good enough.

private final HashSet<String> debugDevices = new HashSet<String>(Arrays.asList("6a3d5c2bae3fd32c"));

private boolean isDebugDevice(String deviceId) {
    return debugDevices.contains(deviceId);
}

private void setCrashlyticsState() {
    @SuppressLint("HardwareIds")
    String deviceId = Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
    if (BuildConfig.DEBUG || isDebugDevice(deviceId)) {
        Log.v("DeviceId", deviceId);
        FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false);
    }
}

Check that BuildConfig. is looking at the correct BuildConfig class. There are often several options and the wrong one could be dragged in.

Will Calderwood
  • 4,393
  • 3
  • 39
  • 64
  • 1
    To add to this answer, most developers will be just interested by `if (BuildConfig.DEBUG) { FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false); }` – Soufiane Sakhi Aug 25 '20 at 19:28
1

We can use fabric's isDebuggable() method.

import static io.fabric.sdk.android.Fabric.isDebuggable;

if(! isDebuggable()){
    // set Crashlytics ... 
}

Happy coding :)

sadiq
  • 2,238
  • 19
  • 21
1

You can use a dedicated manifest file for debug mode (works for me with Crashlytics 2.9.7):

Create the file app/src/debug/AndroidManifest.xml and add the following:

<application>

    <meta-data
        android:name="firebase_crashlytics_collection_enabled"
        android:value="false"/>

</application>

Note that this meta-data element must be put into debug/AndroidManifest.xml only, and not into the regular AndroidManifest.xml

The solution which uses CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build() did not work for me, and I found out that crashlytics is initialized by the CrashlyticsInitProvider before Application.onCreate() is called or an any activity is started, which means that manually initializing fabric in the application or an activity has no effect because fabric is already initialized.

josias
  • 586
  • 4
  • 9
1

Step 1: In build.grade

buildTypes {
        debug {
            debuggable true
            manifestPlaceholders = [enableCrashlytic:false]
        }
        release {
            debuggable false
            manifestPlaceholders = [enableCrashlytic:true]
        }
    }

Step 2: In manifest

<meta-data
            android:name="firebase_crashlytics_collection_enabled"
            android:value="${enableCrashlytic}" />

Step 3: In Application or first Activity

private void setupCrashReport() {
        if (BuildConfig.DEBUG) return;
        Fabric.with(this, new Crashlytics());
    }

I am not sure if the step 3 is necessary, but to make sure the release version should work without crash. source: https://firebase.google.com/docs/crashlytics/customize-crash-reports#enable_opt-in_reporting

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
0

Another way if you only want to do it on your IDE is to logout of the plugin. Apparently it will stop sending reports while you're generating builds without logging in again.

neteinstein
  • 17,529
  • 11
  • 93
  • 123
0

This work for me:

    releaseCompile  'com.crashlytics.sdk.android:crashlytics:2.9.9'

and in buildTypes:

debug {
ext.enableCrashlytics = false
}
Amirhosein
  • 4,266
  • 4
  • 22
  • 35
-1
  1. Add this to your app’s build.gradle:

    android {
        buildTypes {
            debug {
              // Disable fabric build ID generation for debug builds
              ext.enableCrashlytics = false
              ...
    
  2. Disable the Crashlytics kit at runtime. Otherwise, the Crashlytics kit will throw the error:

    // Set up Crashlytics, disabled for debug builds
    // Add These lines in your app Application class onCreate method
    
    Crashlytics crashlyticsKit = new Crashlytics.Builder()
        .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build())
        .build();
    
    // Initialize Fabric with the debug-disabled crashlytics.
    Fabric.with(this, crashlyticsKit);
    
  3. In AndroidManifest.xml, add

    <meta-data
    android:name="firebase_crashlytics_collection_enabled"
    android:value="false" />
    
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
100rbh
  • 763
  • 1
  • 7
  • 10
-9

This is silly answer, I know
Just comment out Fabric.with(this, new Crashlytics());, work on that and uncomment it when you want to release it.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Tej42
  • 1
  • 1