Set up the Agentedit

Follow these steps to start reporting your Android application’s performance to Elastic APM:

Set up Gradleedit

First, add the Elastic APM agent plugin to your application’s build.gradle file as shown below:

// Android app's build.gradle file
plugins {
    id "com.android.application"
    id "co.elastic.apm.android" version "[latest_version]" 
}

The Elastic plugin declaration must be added below the Android app plugin declaration (com.android.application) and below the Kotlin plugin declaration (if used).

After adding the agent plugin, configure it. A minimal configuration sets the Elastic APM Server endpoint as shown below:

// Android app's build.gradle file
plugins {
    //...
    id "co.elastic.apm.android" version "[latest_version]" 
}

elasticApm {
    // Minimal configuration
    serverUrl = "https://your.elastic.server"

    // Optional
    serviceName = "your app name" 
    serviceVersion = "0.0.0" 
    secretToken = "your server auth token" 
}

You can find the latest version in the Gradle plugin portal.

Defaults to your android.defaultConfig.applicationId value.

Defaults to your android.defaultConfig.versionName value.

Defaults to null.

Set up your applicationedit

After syncing your project with the Gradle changes above, the Elastic APM agent needs to be initialized within your Application class. This example shows the simplest way to configure the agent:

// Your Application class

class MyApp extends android.app.Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ElasticApmAgent.initialize(this); 
    }
}

Initialize the Elastic APM agent once.

Compile and runedit

All that’s left is to compile and run your application. That’s it!

What’s next?edit

After initializing the agent as shown above, your application will automatically create traces for all OkHttp network requests (including those created by tools that make use of OkHttp, like Retrofit) and all Activity and Fragment starting methods. In addition, you’ll get automatic crash reports when an unexpected error occurs in your app.

All of these events will contain a "Session ID" that links related events together—allowing you to make sense of and diagnose any issues that arise. Head to the APM app in Kibana to start exploring your data.

If you need to customize the Elastic APM agent to your project’s needs, see configuration. If you need to create your own custom transactions, see manual instrumentation.