How to disable Crashlytics during development

后端 未结 28 2459
心在旅途
心在旅途 2020-11-29 15:42

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 othe

相关标签:
28条回答
  • 2020-11-29 15:54

    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.

    0 讨论(0)
  • 2020-11-29 15:56

    We can use fabric's isDebuggable() method.

    import static io.fabric.sdk.android.Fabric.isDebuggable;
    
    if(! isDebuggable()){
        // set Crashlytics ... 
    }
    

    Happy coding :)

    0 讨论(0)
  • 2020-11-29 15:56

    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

    0 讨论(0)
  • 2020-11-29 15:57

    If you use Gradle just add this to a flavor:

    ext.enableCrashlytics = false
    
    0 讨论(0)
  • 2020-11-29 15:59

    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.

    0 讨论(0)
  • 2020-11-29 15:59

    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
        }
    
    0 讨论(0)
提交回复
热议问题