Android Quick Start

πŸ“˜

SDK Integration

If you haven't already integrated the SDK with your project, follow this guide first.

Now that the SDK has been integrated with your project, let's take a look at how to set up Specto in your application and send your first performance metrics.

Setup

The Specto.init function must be called at application launch. This must be done as early as possible in Application.onCreate. If you are using a crash reporting library, make sure that Specto.init is called after the crash reporter setup.

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        // Crash reporter setup should go before Specto.init
        Specto.init(this)
        // Any other application setup should go after Specto.init
        // ...
    }
}

If you do not yet have a custom application class you will need to create it and specify its name as the android:name attribute in your AndroidManifest.xml's <application> tag.

πŸ“˜

We rely on the Jetpack WorkManager library to efficiently schedule background tasks. We recommend configuring it for on-demand initialization following these instructions so that it doesn't impact your application's startup time.

Tracing App Startup

Setting up your first performance metric is easy: Specto makes measuring & profiling app startup a one liner. From Google's documentation on app startup:

Users expect apps to be responsive and fast to load. An app with a slow start time doesn’t meet this expectation, and can be disappointing to users. This sort of poor experience may cause a user to rate your app poorly on the Play store, or even abandon your app altogether.

Find the point in your app's startup lifecycle where you consider the app startup to be complete β€” this is typically when the initial content has been loaded and fully rendered, but the definition will vary per-application. At this point, call Specto.markAppStartupComplete(). Multiple calls to this method will have no effect, and only the first call will be considered.

If your application is already making a call to the Activity.reportFullyDrawn() function, that is likely when you would call Specto.markAppStartupComplete() as well.

// This is an example of an application using a view model to load its data,
// yours may be very different.

class MainActivity: Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        // …

        val model: MyViewModel by viewModels()
        model.getData().observe(this, Observer<List<Data>> { data ->
            // Update the UI…

            Specto.markAppStartupComplete()
        })
    }
}

If you do not want to trace startup, startup tracing can be disabled during the initialization call:

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        Specto.init(this) {
            traceStartup(false)
        }

        // …
    }
}
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        SpectoConfiguration config = new SpectoConfiguration.Builder()
                .traceStartup(false)
                .build();
        Specto.init(this, config);

        // …
    }
}

❗️

Calling Specto.markAppStartupComplete() is not optional. You must call this method at some point in your app lifecycle, even if you have disabled startup tracing. The framework will not allow other traces to start until the startup trace has completed (or timed out), so not calling this method may lead to performance data not being collected correctly.

To confirm that the startup trace was uploaded, open the Traces tab in the dashboard and check that there was a trace with the interaction dev.specto.startup for your current application version:

🚧

Don't see a startup trace?

Check the following:

  1. If this is the first launch of your app since you integrated the Specto SDK, you may have to launch your app a second time. This is because Specto downloads a remote configuration on the first launch that tells the framework whether tracing should be enabled for that device, and this configuration does not take effect for the startup trace until the next launch.
  2. Check that you are calling Specto.markAppStartupComplete()
  3. Check that the Gradle plugin has been integrated and is running during your build
  4. Still doesn't work? Contact us!

What’s Next

Add more instrumentation to your codebase by learning about traces and spans: