Home » Android » android – How to disable Crashlytics during development

android – How to disable Crashlytics during development

Posted by: admin March 10, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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

Answer:

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

Answer:

If you use Gradle just add this to a flavor:

ext.enableCrashlytics = false

Answer:

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

Answer:

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.

Answer:

Use this in MyApplication#onCreate()

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

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

Answer:

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
        }
        release {
            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)

Answer:

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

Answer:

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

Answer:

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.

Answer:

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

Answer:

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

Answer:

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:[email protected]') {
        transitive = true
    }

Answer:

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.

Answer:

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;    

Answer:

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

Answer:

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

Answer:

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

Answer:

We can use fabric’s isDebuggable() method.

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

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

Happy coding 🙂

Answer:

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.

Answer:

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

Answer:

This work for me:

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

and in buildTypes:

debug {
ext.enableCrashlytics = false
}

Answer:

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.

Answer:

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

Answer:

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.