How to disable Crashlytics during development
AndroidCrashlyticsTwitter FabricAndroid Problem Overview
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
Android Solutions
Solution 1 - Android
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 https://stackoverflow.com/questions/30488575/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
}
}
Solution 2 - Android
Marc from Crashlytics here. Here's a couple of ways to disable Crashlytics while you are doing your debug builds!
-
Use a different android:versionString for debug and release builds and then disable crash reporting from the Crashlytics web dashboard for the debug version.
-
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: https://stackoverflow.com/questions/7085644/how-to-check-if-apk-is-signed-or-debug-build
Solution 3 - Android
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");
}
Solution 4 - Android
If you use Gradle just add this to a flavor:
ext.enableCrashlytics = false
Solution 5 - Android
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);
Solution 6 - Android
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.
Solution 7 - Android
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)
Solution 8 - Android
Use this in MyApplication#onCreate()
if (!BuildConfig.DEBUG) Crashlytics.start(this);
EDIT If you upgraded to Fabric, use this answer instead.
Solution 9 - Android
As per google use this code for disable Crashlytics and it will also improve build process.
reference-https://developer.android.com/studio/build/optimize-your-build
Solution 10 - Android
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}" />
Solution 11 - Android
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
Solution 12 - Android
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.
Solution 13 - Android
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());
}
Solution 14 - Android
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
}
Solution 15 - Android
There are two options in order to disable Firebase Crashlytics for the following version com.google.firebase:firebase-crashlytics:17.0.0:
- Add meta-tag to app's Manifest
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />
OR
- 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)
Solution 16 - Android
2022 answer using FirebaseCrashlytics.
There are two cases:
- 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
tofalse
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}" />
- 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:
Solution 17 - Android
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.
Solution 18 - Android
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;
Solution 19 - Android
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 );
Solution 20 - Android
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"
}
Solution 21 - Android
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);
Solution 22 - Android
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.
Solution 23 - Android
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"
........>
Solution 24 - Android
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
Solution 25 - Android
We can use fabric's isDebuggable() method.
import static io.fabric.sdk.android.Fabric.isDebuggable;
if(! isDebuggable()){
// set Crashlytics ...
}
Happy coding :)
Solution 26 - Android
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.
Solution 27 - Android
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
Solution 28 - Android
This work for me:
releaseCompile 'com.crashlytics.sdk.android:crashlytics:2.9.9'
and in buildTypes:
debug {
ext.enableCrashlytics = false
}
Solution 29 - Android
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.
Solution 30 - Android
-
Add this to your app’s build.gradle:
android { buildTypes { debug { // Disable fabric build ID generation for debug builds ext.enableCrashlytics = false ...
-
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);
-
In AndroidManifest.xml, add
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />
Solution 31 - Android
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.