禁用"答案"但不是" Crashlytics"

时间:2016-03-15 15:38:48

标签: crashlytics twitter-fabric crashlytics-android

安装" Crashlytics"在我的Android应用程序中,它会自动安装" Answers"。我只想安装" Crashlytics"并希望得到"答案"禁用。有谁知道怎么做?

enter image description here

的build.gradle

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

谢谢!

2 个答案:

答案 0 :(得分:8)

Mike来自Fabric和Crashlytics。

如您所见,Crashlytics默认包含Answers。如果您不希望在您的应用上启用Answers,那么您需要在构建应用时调用CrashlyticsCore()而不是Crashlytics

例如,将这些作为您的导入:

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

然后在初始化Fabric时,使用:

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

这将初始化崩溃报告方面的事情。

答案 1 :(得分:1)

我认为目前不可能,因为Crashlytics正在制作一个Answers实例:

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(答案答案,Beta beta,CrashlyticsCore核心)不公开,所以我们无法实现这一点。答案字段也是最终字段,因此您无法覆盖它。我认为现在有办法让用户决定是否要使用答案。

Hey Fabric / Google ,您应该以编程方式选择退出会话,让程序员选择让用户决定是否要以某种方式计算。< / p>

<强> 修改

我的解决方案是为所有需要的funtioncs和init使用一个包装器,随意使用它:

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