Search code examples
androidcrashlyticstimber-android

Where to initialize timber and crashlytics


My app do lot of background tasks and has few activities and most of the work is done in background.

Suppose I add timber and Crashlytics to my app. How can I make sure than when the app is updated on the device, timber, and Crashlytics get initialized without activity being opened.


Solution

  • When you update your app, it's always stopped and restarted - Timber and Crashlytics can be initialized at this moment.

    When your application starts, method onCreate() of android.app.Application class is invoked. To add extra logic to this method, you need to create new MyApplication class, which extends Application class and override onCreate() method.

    public class MyApplication extends Application {
    
       @Override
       public void onCreate() {
          super.onCreate();
          //here initialize Timber and Crashlytics
       }
    }
    

    By default, android.app.Application class is used to initialize application context. Information about that another Application class is used should be provided in AndroidManifest.xml file.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.myname.app">
    
        <application android:name="com.myname.MyApplication">
            <activity android:name=".MyActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        </application>
    </manifest>