SDK integration
The AppMetrica SDK for Android is distributed as an AAR library. The library is available in the Maven repository.
The steps below explain how to enable and initialize the AppMetrica SDK:
Step 1. Add the AppMetrica library to your project
If you use Gradle to build your app, add the following dependency to your app's Gradle file:
dependencies {
// AppMetrica SDK.
implementation("io.appmetrica.analytics:analytics:7.5.0")
}
dependencies {
// AppMetrica SDK.
implementation 'io.appmetrica.analytics:analytics:7.5.0'
}
Step 2. Initialize the AppMetrica library
Alert
During initialization, consider AppMetrica library specifics. For more information, see How the AppMetrica library works.
Initialize the AppMetrica library in your app. To do this, 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);
}
}
What is the API key?
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. To do this, 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 detection helps you understand where your users are located geographically. By default, AppMetrica determines the device's location based on its IP address, with accuracy down to the country level.
To determine device location down to the city level, in the AndroidManifest.xml file of your app, add the uses-permission
element before the application
element as follows:
<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
- Code conflict when using automatic collection of Ad Revenue data for Fyber
- 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.