Lazy initialization of Fabric kits?

落花浮王杯 提交于 2019-12-22 02:30:33

问题


Is it possible to lazily initialize Fabric Kits? for example, right now I do:

Fabric.with(this, crashlytics, twitterCore, tweetUi); // 500ms

I would like to initialize only Crashlytics (no twitter stuff), like below, because it is 10x faster, and I don't need the Twitter stuff right away

Fabric.with(this, crashlytics); // 50ms

Later on, when the user visits an activity where I need TwitterCore & TweetUi, I'd like to add them to Fabric on the fly before using them.

Is this possible ?

Edit: I managed to do it with reflection, which is obviously not ideal, but it works for the time being. I'm still looking for a proper solution to this. Here's how I did it:

    try {
        final Fabric newFabric = (new Fabric.Builder(context)).kits(crashlytics, twitterCore, tweetUi).build();
        final Method method = Fabric.class.getDeclaredMethod("setFabric", Fabric.class);
        method.setAccessible(true);
        method.invoke(null, newFabric);
    } catch (Exception e) {
        Timber.e(e, e.getMessage());
    }

回答1:


Mike from Fabric here. Currently, we only respect the first initialization of Fabric. One option would to be initialize everything up front or if you're ok for missing some crashes, not initialize Twitter and Crashlytics until later in your app's code.




回答2:


You can use builder pattern for the initialisation and can disable the crash reporting in debug mode:

CrashlyticsCore core =
    new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build(), new Crashlytics());

Update 1: Add more Kit afterwards or Lazy initialization of Fabric kits?:

CrashlyticsCore core =
    new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
//Store the below fabric as an instance member
Fabric fabric = Fabric.with(this, new Crashlytics.Builder().core(core).build(), new Crashlytics
    ());
//To add later:
fabric.getKits().add(YOUR_NEW_KIT);


来源:https://stackoverflow.com/questions/43284196/lazy-initialization-of-fabric-kits

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