Michael
Michael

Reputation: 22957

How to disable Crashlytics during development

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

Upvotes: 271

Views: 112165

Answers (30)

Mahmoud
Mahmoud

Reputation: 2893

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

Upvotes: 12

Ahmed Mkaouar
Ahmed Mkaouar

Reputation: 708

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

Upvotes: 5

Niaz Ahmed
Niaz Ahmed

Reputation: 310

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"
        ........>

Upvotes: 7

Albert Vila Calvo
Albert Vila Calvo

Reputation: 15835

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)

Upvotes: 24

Viktor Ivanov
Viktor Ivanov

Reputation: 159

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)

Upvotes: 6

Will Calderwood
Will Calderwood

Reputation: 4636

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.

Upvotes: 1

Amirhosein
Amirhosein

Reputation: 4446

This work for me:

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

and in buildTypes:

debug {
ext.enableCrashlytics = false
}

Upvotes: 0

100rbh
100rbh

Reputation: 763

  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" />
    

Upvotes: -1

Paul Spiesberger
Paul Spiesberger

Reputation: 5770

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");
}

Upvotes: 51

thanhbinh84
thanhbinh84

Reputation: 18464

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

Upvotes: 1

arbuz
arbuz

Reputation: 3150

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}" />

Upvotes: 11

josias
josias

Reputation: 771

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.

Upvotes: 3

Lovekush Vishwakarma
Lovekush Vishwakarma

Reputation: 3179

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

Upvotes: 10

sadiq
sadiq

Reputation: 2308

We can use fabric's isDebuggable() method.

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

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

Happy coding :)

Upvotes: 1

Yan
Yan

Reputation: 1726

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
    }

Upvotes: 5

M. Reza Nasirloo
M. Reza Nasirloo

Reputation: 16614

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

Upvotes: 2

William henderson
William henderson

Reputation: 377

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

Upvotes: 6

Neria Nachum
Neria Nachum

Reputation: 1529

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;    

Upvotes: 3

ribhu
ribhu

Reputation: 171

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"
    }

Upvotes: 2

fahmy
fahmy

Reputation: 3672

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.

Upvotes: 24

Abhishek Patidar
Abhishek Patidar

Reputation: 1577

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

Upvotes: 28

xialin
xialin

Reputation: 7756

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
    }
}

Upvotes: 399

Tej42
Tej42

Reputation: 1

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.

Upvotes: -9

Bj&#246;rn Kechel
Bj&#246;rn Kechel

Reputation: 8483

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.

Upvotes: 3

Khronos
Khronos

Reputation: 381

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

Upvotes: 8

Austyn Mahoney
Austyn Mahoney

Reputation: 11408

Use this in MyApplication#onCreate()

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

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

Upvotes: 15

Vaiden
Vaiden

Reputation: 16132

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

Upvotes: 2

Ariel Cabib
Ariel Cabib

Reputation: 2102

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.

Upvotes: 6

user1998494
user1998494

Reputation: 634

If you use Gradle just add this to a flavor:

ext.enableCrashlytics = false

Upvotes: 30

neteinstein
neteinstein

Reputation: 17613

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.

Upvotes: 0

Related Questions