Lazy initialization of Fabric kits?
Asked Answered
S

2

13

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());
    }
Shankle answered 7/4, 2017 at 17:44 Comment(0)
A
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.

Agueweed answered 13/4, 2017 at 12:6 Comment(2)
thanks for answering! Unfortunately none of those ideal. Is there any chance we can at least make the Twitter initialization faster? I don't know what its doing, but at 450ms (on a Galaxy S7), that's pretty intensive.Shankle
Since Fabric has been acquired by Google, for any slow init times, I'd recommend posting on twittercommunity.com/c/publisher since the Twitter team monitors that for any questions on it's SDK.Agueweed
S
3

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);
Shute answered 11/4, 2017 at 12:29 Comment(3)
that second parameter, named crashlytics, was built that way. Either way - it doesn't really matter, because even with your example it is not obvious: how do you initialize & add other Kits to Fabric later ?Shankle
comment regarding your edit: if you try that you'll see that it doesn't actually work. fabric.getKits() returns a collection that throws an exception if you try to add to it.Shankle
I updated my answer based on the logic or the API being exposed. I am not using Twitter as of now. But if it is an exception then I am sorry. I relied on the Fabric API and logic and as expected it should work as it is very common to what it returns or it should have indicated a warning at compile time or so. I will try to raise a bug after digging into it.Shute
A
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.

Agueweed answered 13/4, 2017 at 12:6 Comment(2)
thanks for answering! Unfortunately none of those ideal. Is there any chance we can at least make the Twitter initialization faster? I don't know what its doing, but at 450ms (on a Galaxy S7), that's pretty intensive.Shankle
Since Fabric has been acquired by Google, for any slow init times, I'd recommend posting on twittercommunity.com/c/publisher since the Twitter team monitors that for any questions on it's SDK.Agueweed

© 2022 - 2024 — McMap. All rights reserved.