Installation and initialization

The SDK for Android is a library in the AAR format. The library is available in the Maven repository.

Step 1. Add the library to your project

If you use Gradle for building the app, set this dependency in the dependencies block in the build.gradle file:

dependencies {
    // AppMetrica SDK.
    implementation 'com.yandex.android:mobmetricalib:3.2.2'
    // Optionally. Play Install Referrer library.
    implementation 'com.android.installreferrer:installreferrer:1.0'
}
Adding the Play Install Referrer library improves quality of install source tracking.
If you don't use Gradle

Download the library and add it to the project.

Step 2. Initialize the library

Initialize the library in the app and set up user activity tracking. Extend the Application class and override the onCreate() method as follows:
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Creating an extended library configuration.
        YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key).build();
        // Initializing the AppMetrica SDK.
        YandexMetrica.activate(getApplicationContext(), config);
        // Automatic tracking of user activity.
        YandexMetrica.enableActivityAutoTracking(this);
    }
}
What is the API key?

The API key is a unique application identifier that is issued in the AppMetrica web interface during app registration.

Make sure you have entered it correctly.

AppMetrica allows tracking pre-installed apps. To use this feature, you must initialize the library with the extended configuration.

Step 3. (Optional) Make changes to AndroidManifest.xml

For tracking the device location, open the AndroidManifest.xml file and make the following changes before the application element:

<manifest>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <application>...</application>
</manifest>

ACCESS_COARSE_LOCATION allows you to track the device's location. Learn more.

How the AppMetrica library works

The AppMetrica library consists of two sections: the client and the service.

The client section pre-validates data and transmits it to the service section, which is responsible for storing data and sending it to the server.

The service runs in a separate process. The reason for this is so the library has a minimal affect on the application in performance and memory. In addition, it frees the library from dependency on the stability of the app, so it can store and deliver data even if the app crashes or is unstable. However, this introduces certain specifics to working with AppMetrica.

If the app itself has multiple processes, the AppMetrica initialization must be performed the same way and using the same configuration. Otherwise, you could encounter a situation when the configuration of the AppMetrica library depends on which of the processes is initialized first (because each process has its own instance of the AppMetrica client part, while there is one shared service part).

We recommend initializing the AppMetrica library in the Application.onCreate() method (this way the library fulfills the Application.onCreate() method call in each process). However, you should account for the lifecycle of ContentProvider. There is no guarantee that an Application instance will be created before a ContentProvider instance is created. Also keep in mind that the code in the Application.onCreate() method runs for the service process as well.

So if you don't want some code executed more than once, don't put it in the Application.onCreate() method. It would be better to run this code when creating other components, or to put it in a separate Service. You should also avoid executing lengthy operations inside the Application.onCreate() method.