Documentation

Installation and initialization

The SDK for Android is a library in AAR (recommended) or JAR format. The library is available in the Maven repository.

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

dependencies {
    // AppMetrica from maven central.
    compile 'com.yandex.android:mobmetricalib:2.76'
}
If you don't use Gradle

Download the library and add it to the project.

Step 2. 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();
          // Initializing the AppMetrica SDK
          YandexMetrica.activate
[no-highlight[

Type

void

Description

Allows you to initialize the library in an app

]no-highlight]
(getApplicationContext(), API_key
[no-highlight[

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

]no-highlight]
);
// Tracking user activity YandexMetrica.enableActivityAutoTracking
[no-highlight[

Type

void

Description

Enables automatic app lifecycle tracking. Supports Android version 4.0 and later. This is an alternative to the methods onResumeActivity(Activity activity) and onPauseActivity(Activity activity). This method must be called immediately after initializing the AppMetrica library, before opening the first app screen

]no-highlight]
(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: For tracking 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
[no-highlight[

Required

No

Description

Allows tracking the device location. More information

]no-highlight]
"/>
<application>...</application> </manifest>

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

dependencies {
    // AppMetrica from maven central.
    compile 'com.yandex.android:mobmetricalib:2.76:jar'
}
If you don't use Gradle

Download the library and add it to the project.

Step 2. Set up data transmission and tracking user location. Make the changes listed below in the AndroidManifest.xml file.

  1. Add the following access permissions before the application element:

    <manifest>
        <!-- required -->
        <uses-permission android:name="android.permission.INTERNET
    [no-highlight[

    Required

    Yes

    Description

    Allows the library to send reports. More information

    ]no-highlight]
    "/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE
    [no-highlight[

    Required

    Yes

    Description

    Allows sending the server data about the user‘s network, internet connection, and related information. More information

    ]no-highlight]
    "/>
    <!-- optional --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION
    [no-highlight[

    Required

    No

    Description

    Allows tracking the device location. More information

    ]no-highlight]
    "/>
    <application></application> </manifest>

  2. Make changes to the application element:

    <manifest>
        <application>        
            <!-- start of Yandex Metrica -->     
            <service
                android:name="com.yandex.metrica.MetricaService"
                android:enabled="true"
                android:exported="true"
                android:process=":Metrica">
    
                <meta-data android:name="metrica:api:level" android:value="61"/>
    
                <intent-filter>
                  <category android:name="android.intent.category.DEFAULT"/>
                  <action android:name="com.yandex.metrica.IMetricaService"/>
                  <data android:scheme="metrica"/>
                </intent-filter>
            </service>
    
            <service
                android:name="com.yandex.metrica.ConfigurationService"
                android:enabled="true"
                android:exported="true"
                android:process=":Metrica"
                tools:ignore="ExportedService">
                
                <meta-data
                  android:name="metrica:configuration:api:level"
                  android:value="1"/>
    
                <intent-filter>
                  <action android:name="com.yandex.metrica.configuration.ACTION_START"/>
                </intent-filter>
            </service>
    
            <receiver
                android:name="com.yandex.metrica.MetricaEventHandler"
                android:enabled="true"
                android:exported="true">
    
                <!-- INSTALL_REFERRER is needed for campaign tracking -->
                <intent-filter>
                    <action android:name="com.android.vending.INSTALL_REFERRER"/>
                </intent-filter>
            </receiver>
            <!-- end of Yandex Metrica -->        
        </application>
    </manifest>

    where metrica:api:level is the level of the API library. The API level must be set. You should not change the level unless the library version has changed.

Step 3. 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();
          // Initializing the AppMetrica SDK
          YandexMetrica.activate
[no-highlight[

Type

void

Description

Allows you to initialize the library in an app

]no-highlight]
(getApplicationContext(), API_key
[no-highlight[

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

]no-highlight]
);
// Tracking user activity YandexMetrica.enableActivityAutoTracking
[no-highlight[

Type

void

Description

Enables automatic app lifecycle tracking. Supports Android version 4.0 and later. This is an alternative to the methods onResumeActivity(Activity activity) and onPauseActivity(Activity activity). This method must be called immediately after initializing the AppMetrica library, before opening the first app screen

]no-highlight]
(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.

Note. 

If you use proguard in your project, you must add the following lines to the proguard.cfg file:

-keep class com.yandex.metrica.** { *; }
-dontwarn com.yandex.metrica.**

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.