Disable “Answers” but not “Crashlytics”

拟墨画扇 提交于 2019-11-29 03:57:20

Mike from Fabric and Crashlytics here.

As you saw, Crashlytics by default includes Answers. If you don't want Answers enabled on your app, then you want to invoke CrashlyticsCore() when building your app instead of Crashlytics.

For example, have these as your imports:

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.core.CrashlyticsCore;
import io.fabric.sdk.android.Fabric;

Then when you initialize Fabric, use:

Fabric.with(this, new CrashlyticsCore());

and that will initialize only the crash reporting side of things.

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);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!