-->

Disable “Answers” but not “Crashlytics”

2019-01-24 06:02发布

问题:

When installing "Crashlytics" in my Android App, it automatically installs "Answers". I only want to install "Crashlytics" and want to have "Answers" disabled. Does anyone know how to do that?

build.gradle

dependencies {
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}

Thanks!

回答1:

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.



回答2:

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);
        }
    }
}