Top Banner
Timber + Crashlytics A match made in heaven DAMIAN MARUŠIĆ
16

Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

Apr 11, 2017

Download

Software

Infinum
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

Timber + Crashlytics → A match made in heavenDAMIAN MARUŠIĆ

Page 2: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

MAIN TOPICS

• Timber

• Crashlytics

• App crash handler

Page 3: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

WHAT DO WE WANT ?

• Development - logging to logcat

• Production - crash reporting to Crashlytics

Page 4: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

TIMBER

• Utility on top of Android's normal Log class

• by Jake Wharton

Page 5: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

Timber.plant(new Timber.DebugTree());

Timber.d(...) Timber.i(...) Timber.v(...) Timber.e(...) Timber.w(...) Timber.wtf(...)

Page 6: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

CRASHLYTICS

• Crash reporting tool

• Automatic and manual crash reports

• Stack trace

• Android version

• Device model

• etc.

Page 7: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven
Page 8: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

CRASH REPORTING TREE

private static class CrashReportingTree extends Timber.Tree { @Override protected void log(int priority, String tag, String message, Throwable t) { if (priority == Log.VERBOSE || priority == Log.DEBUG) { return; } // will write to the crash report but NOT to logcat Crashlytics.log(message); if (t != null) { Crashlytics.logException(t); } }}

Page 9: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

CRASH REPORTING TREE

@Overridepublic void onCreate() { super.onCreate(); CrashlyticsCore crashlyticsCore = new CrashlyticsCore.Builder()

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

if (BuildConfig.DEBUG) { Timber.plant(new Timber.DebugTree()); } else { Timber.plant(new CrashReportingTree()); } }

Page 10: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

USAGE

try {

...

} catch (Exception e) { Timber.e(e, "Failure in " + getClass().getSimpleName()); }

Page 11: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

APP CRASH HANDLER

• No more ugly system dialogs

• Restart app

• Simple configuration

• by Square

Page 12: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

APP CRASH HANDLER

Page 13: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

APP CRASH HANDLER EXAMPLE

public class AppCrashHandler implements Thread.UncaughtExceptionHandler { private Activity liveActivity; public AppCrashHandler(Application application) { application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { @Override public void onActivityResumed(Activity activity) { liveActivity = activity; } @Override public void onActivityPaused(Activity activity) { liveActivity = null; } }); } @Override public void uncaughtException(Thread thread, Throwable ex) { if(liveActivity != null){ Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); liveActivity.finish(); liveActivity.startActivity(intent); } System.exit(0); } }

Page 14: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

APP CRASH HANDLER EXAMPLE

@Overridepublic void onCreate() { super.onCreate(); Thread.setDefaultUncaughtExceptionHandler(new AppCrashHandler(this)); }

Page 15: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

Thank you! [email protected]

Visit infinum.co or find us on social networks:

infinum.co infinumco infinumco infinum

Page 16: Infinum Android Talks #15 - Timber + Crashlytics -> A match made in heaven

REFERENCES

• Timber - https://github.com/JakeWharton/timber

• Square’s approach to Android crashes - https://vimeo.com/145042944

• Željko’s crash handler lib - https://github.com/zplesac/android-crash-handler