Disable “Answers” but not “Crashlytics”

前端 未结 2 1740
迷失自我
迷失自我 2020-12-16 18:07

When installing \"Crashlytics\" in my Android App, it automatically installs \"Answers\". I only want to install \"Crashlytics\" and want to have \"Answers\" disabled. Does

2条回答
  •  星月不相逢
    2020-12-16 18:36

    I think it is not possible at the moment, because Crashlytics is making an Answers instance:

    public Crashlytics build() {
                if(this.coreBuilder != null) {
                    if(this.core != null) {
                        throw new IllegalStateException("Must not use Deprecated methods delay(), disabled(), listener(), pinningInfoProvider() with core()");
                    }
    
                    this.core = this.coreBuilder.build();
                }
    
                if(this.answers == null) {
                    this.answers = new Answers();
                }
    
                if(this.beta == null) {
                    this.beta = new Beta();
                }
    
                if(this.core == null) {
                    this.core = new CrashlyticsCore();
                }
    
                return new Crashlytics(this.answers, this.beta, this.core);
            }
    

    Crashlytics(Answers answers, Beta beta, CrashlyticsCore core) is not public, so we cannot instanciate this. Also the answers field is final, so you cannot override it. I think there is now way to let the user decide if they want to use answers.

    Hey Fabric/Google, you should make a programmatically way to opt out for a session, to give the programmer the option to let the user decide if they want to be counted in some way.

    EDIT

    My solution is to to use a wraper for all needed funtioncs and init, feel free to use it:

    public class AnalyticsWrapper {
    
        static AnalyticsWrapper instance;
    
        public static void initialize(Context context, boolean optOut) {
            if (instance != null) throw new IllegalStateException("AnalyticsWrapper must only be initialized once");
            instance = new AnalyticsWrapper(context.getApplicationContext(), optOut);
        }
    
        public static AnalyticsWrapper getInstance() {
            if (instance == null) throw new IllegalStateException("AnalyticsWrapper must be initialized before");
            return instance;
        }
    
        final boolean optOut;
    
        private AnalyticsWrapper(Context context, boolean optOut) {
            this.optOut = optOut;
            initFabric(context);
        }
    
        public boolean isOptOut() {
            return optOut;
        }
    
        private void initFabric(Context context) {
            if (!optOut) {
    
                if (!BuildConfig.DEBUG) {
                    Timber.plant(new CrashlyticsLogExceptionTree());
                    Timber.plant(new CrashlyticsLogTree(Log.INFO));
                }
    
                Crashlytics crashlyticsKit = new Crashlytics.Builder().core(
                        new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG)
                                                     .build())
                                                                      .build();
    
                Fabric fabric = new Fabric.Builder(context).kits(crashlyticsKit)
                                                                 .debuggable(BuildConfig.DEBUG)
                                                                 .build();
    
    
                Fabric.with(fabric);
            }
        }
    
        public void crashlyticsSetString(String key, String value) {
            if (!optOut) {
                Crashlytics.setString(key, value);
            }
        }
    
        public void logException(Throwable throwable) {
            if (!optOut) {
                Crashlytics.logException(throwable);
            }
        }
    
        public void logEvent(CustomEvent event) {
            if (!optOut) {
                Answers.getInstance()
                       .logCustom(event);
            }
        }
    }
    

提交回复
热议问题