How to disable Crashlytics Answers?

后端 未结 3 1671
南笙
南笙 2020-12-17 21:05

Disabling Crashlytics error reporting is relatively straight forward.. I\'d also like to disable Answers for debug builds. However,

new Crashlytics.Builder(         


        
相关标签:
3条回答
  • 2020-12-17 21:32

    For the time being, I solved the problem the old Java way:

    Extend Answers using a sort-of singleton:

    public class CustomAnswers extends Answers {
    
        private static CustomAnswers instance;
    
        private boolean mEnabled;
    
        private CustomAnswers(boolean enabled) {
            super();
            mEnabled = enabled;
        }
    
        public static synchronized void init(boolean enabled) {
            if (instance == null) {
                instance = new CustomAnswers(enabled);
            }
        }
    
        public static synchronized CustomAnswers get() {
            return instance;
        }
    
        @Override
        public void logSignUp(SignUpEvent event) {
            if (mEnabled) {
                super.logSignUp(event);
            }
        }
    
        // (...)
    }
    

    Initialize Crashlytics with Answers implementation:

    boolean isDebug = DebugHelper.isDebugVersion(this);
    CustomAnswers.init(!isDebug);
    CrashlyticsCore crashlyticsCore =
            new CrashlyticsCore.Builder().disabled(isDebug).build();
    Fabric.with(this, new Crashlytics.Builder()
            .core(crashlyticsCore).answers(CustomAnswers.get()).build());
    

    Use Answers implementation for events:

    CustomAnswers.get().logInvite(new InviteEvent());
    

    This will disable events being logged.

    Note that, as described in my first post, Answers.getInstance() will return null and not your CustomAnswers instance in that case.

    0 讨论(0)
  • 2020-12-17 21:40

    on my app we do it the old fashioned way:

    if (!IS_DEBUG) {
       Fabric.with(this, new Crashlytics());
    }
    

    works fine.

    Of course you can initialise with whichever custom parameters you need.

    edit:

    to get the debug boolean is just a matter of using gradle to your favour:

    src/
       main/ // your app code
       debug/
           AppSettings.Java:
                public static final boolean IS_DEBUG = true;
       release/
           AppSettings.Java:
                public static final boolean IS_DEBUG = false;
    

    edit:

    I would advise against using BuildConfig.DEBUG, see this article: http://www.digipom.com/be-careful-with-buildconfig-debug/

    0 讨论(0)
  • 2020-12-17 21:49

    Try this code

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