SDK integration
The SDK for Android is an AAR library. The library is available in the Maven repository.
This section describes the steps to enable and initialize AppMetrica SDK:
Step 1. Add the library to your project
If you use Gradle to build the app, add the dependency to the app's Gradle file:
dependencies {
// AppMetrica SDK.
implementation("io.appmetrica.analytics:analytics:6.5.0")
}
dependencies {
// AppMetrica SDK.
implementation 'io.appmetrica.analytics:analytics:6.5.0'
}
Step 2. Initialize the library
Alert
You should take into account some features of the AppMetrica library during initialization. For more information, see Features of the AppMetrica library.
Initialize the library in the app. Extend the Application
class and override the onCreate()
method as follows:
class YourApplication : Application() {
override fun onCreate() {
super.onCreate()
// Creating an extended library configuration.
val config = AppMetricaConfig.newConfigBuilder(API_KEY).build()
// Initializing the AppMetrica SDK.
AppMetrica.activate(this, config)
}
}
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Creating an extended library configuration.
AppMetricaConfig config = AppMetricaConfig.newConfigBuilder(API_KEY).build();
// Initializing the AppMetrica SDK.
AppMetrica.activate(this, config);
}
}
The API key is a unique app ID issued in the AppMetrica web interface at app registration. You can find it under Settings.
Optional
When using Firebase Performance Monitoring in Firebase version 31.0.0+, activate FirebaseApp for all processes, including the AppMetrica SDK process. In Application#onCreate()
, call FirebaseApp.initializeApp(this)
before activating the AppMetrica SDK. Otherwise, the AppMetrica SDK won't be activated.
Example:
class YourApplication : Application() {
override fun onCreate() {
super.onCreate()
// Init FirebaseApp for all processes
FirebaseApp.initializeApp(this)
// Then activate AppMetrica SDK
val config = AppMetricaConfig.newConfigBuilder(API_KEY).build()
AppMetrica.activate(this, config)
}
}
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Init FirebaseApp for all processes
FirebaseApp.initializeApp(this);
// Then activate AppMetrica SDK
AppMetricaConfig config = AppMetricaConfig.newConfigBuilder(API_KEY).build();
AppMetrica.activate(this, config);
}
}
Step 3. (Optional) Configure location detection
Location allows you to estimate the geographical distribution of users. By default, AppMetrica determines the device's location based on its IP, with accuracy down to the country level.
To determine device location down to the city level, open the AndroidManifest.xml file and add the uses-permission
element 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. For more information, see Android documentation.
Step 4. (Optional) Configure sending of events, profile attributes, and revenue
To collect information on user actions in the app, set up sending your own events. For more information, see Sending your own events.
To collect information about users, set up sending profile attributes. For more information, see Profiles.
Note
Unlike events, a profile attribute can take only one value. When you send a new attribute value, the old value is overwritten.
To track in-app purchases, set up Revenue sending. For more information, see In-App purchases.
Step 5. Test the library operation
Alert
Before testing the library, ensure that the SDK is initialized in compliance with recommendations.
Before starting testing, we recommend setting up data transmission to an additional API key or adding an app with a new API key. This will help you separate test data from your core statistics.
To test how the library works:
- Start the app with the AppMetrica SDK and use it for a while.
- Make sure your device is connected to the internet.
- In the AppMetrica interface, make sure that:
Troubleshooting
- Number of sessions staying the same
- No events in the report
- Error adding a library to a project
- Error initializing AppMetrica with third-party libraries
- Invalid duration of user session at manual tracking
- High power consumption by the AppMetrica library
- My problem is not listed
Learn more
- How do I enable user location sending?
- How can I make sure that I have the latest versions of the Android libraries installed?
If you didn't find the answer you were looking for, you can use the feedback form to submit your question. Please describe the problem in as much detail as possible. Attach a screenshot if possible.