How to disable Crashlytics during development

后端 未结 28 2458
心在旅途
心在旅途 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:43

    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");
    }
    
    0 讨论(0)
  • 2020-11-29 15:43

    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)
    
    0 讨论(0)
  • 2020-11-29 15:43

    Use this in MyApplication#onCreate()

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

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

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

    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

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

    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 );
    
    0 讨论(0)
  • 2020-11-29 15:47

    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.

    0 讨论(0)
提交回复
热议问题