Integrate SDK
Learn how to initialize and start the Android SDK.
Recommended
Get started with our SDK integration wizard
- You must install the Android SDK.
- Ensure that in your app
build.gradlefile,applicationId's value (in thedefaultConfigblock) matches the app's app ID in AppsFlyer. - Get the AppsFlyer dev key. It is required to successfully initialize the SDK.
Initializing the Android SDK
It's recommended to initialize the SDK in the global Application class/subclass. That is to ensure the SDK can start in any scenario (for example, deep linking).
Step 1: Import AppsFlyerLib
In your global Application class, import AppsFlyerLib:
import com.appsflyer.AppsFlyerLib;import com.appsflyer.AppsFlyerLibStep 2: Initialize the SDK
In the global Application onCreate, call init with the following arguments:
AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, null, this);AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, null, this)- The first argument is your AppsFlyer dev key.
- The second argument is a Nullable
AppsFlyerConversionListener. If you don't need conversion data, we recommend passing anullas the second argument. For more information, see Conversion data. - The third argument is the Application Context.
Starting the Android SDK
In the Application's onCreate method, after calling init, call start and pass it the Application's Context as the first argument:
AppsFlyerLib.getInstance().start(this);AppsFlyerLib.getInstance().start(this)Deferring SDK start
Optional
You can defer the SDK initialization by calling start from an Activity class, instead of calling it in the Application class. init should still be called in the Application class.
Typical usage of deferred SDK start is when an app would like to request consent from the user to collect data in the Main Activity, and call start after getting the user's consent.
Important noticeIf the app calls
startfrom an Activity, it should pass the Activity Context to the SDK.
Failing to pass the activity context will not trigger the SDK, thus losing attribution data and in-app events.
Starting with a response listener
To receive confirmation that the SDK was started successfully, create an AppsFlyerRequestListener object and pass it as the third argument of start:
AppsFlyerLib.getInstance().start(getApplicationContext(), <YOUR_DEV_KEY>, new AppsFlyerRequestListener() {
@Override
public void onSuccess() {
Log.d(LOG_TAG, "Launch sent successfully, got 200 response code from server");
}
@Override
public void onError(int i, @NonNull String s) {
Log.d(LOG_TAG, "Launch failed to be sent:\n" +
"Error code: " + i + "\n"
+ "Error description: " + s);
}
});AppsFlyerLib.getInstance().start(this, <YOUR_DEV_KEY>, object : AppsFlyerRequestListener {
override fun onSuccess() {
Log.d(LOG_TAG, "Launch sent successfully")
}
override fun onError(errorCode: Int, errorDesc: String) {
Log.d(LOG_TAG, "Launch failed to be sent:\n" +
"Error code: " + errorCode + "\n"
+ "Error description: " + errorDesc)
}
})- The
onSuccess()callback method is invoked for every200response to an attribution request made by the SDK. - The
onError(String error)callback method is invoked for any other response and returns the response as the error string.
Full example
The following example demonstrates how to initialize and start the SDK from the Application class.
import android.app.Application;
import com.appsflyer.AppsFlyerLib;
// ...
public class AFApplication extends Application {
// ...
@Override
public void onCreate() {
super.onCreate();
// ...
AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, null, this);
AppsFlyerLib.getInstance().start(this);
// ...
}
// ...
}import android.app.Application
import com.appsflyer.AppsFlyerLib
// ...
class AFApplication : Application() {
override fun onCreate() {
super.onCreate()
// ...
AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, null, this)
AppsFlyerLib.getInstance().start(this)
// ...
}
// ...
}See Setting the Customer User ID to associate a CUID with this integration.
Log sessions
The SDK sends an af_app_opened message whenever the app is opened or brought to the foreground. Before the message is sent, the SDK makes sure that the time passed since sending the last message is not smaller than a predefined interval.
Setting the time interval between app launches
Call setMinTimeBetweenSessions to set the minimal time interval that must lapse between two af_app_opened messages. The default interval is 5 seconds.
Logging sessions manually
You can log sessions manually by calling logSession.
Enabling debug mode
Optional
You can enable debug logs by calling setDebugLog:
AppsFlyerLib.getInstance().setDebugLog(true);AppsFlyerLib.getInstance().setDebugLog(true)
NoteTo see full debug logs, make sure to call
setDebugLogbefore invoking other SDK methods.See example.
WarningTo avoid leaking sensitive information, make sure debug logs are disabled before distributing the app.
Test the integration
Easily test with our SDK wizard
Note
If you prefer not to use our recommended wizard, you can find detailed manual testing instructions here.
For a full troubleshooting checklist, see Troubleshooting.
Creating an Android debug app
Optional
You can utilize Android Studio's build variants to configure an easy-to-use debug app for testing purposes.
All tests can be performed for both production and debug apps.
Step 1: Configure Gradle's debug build type
In your app-level build.gradle file, configure the debug build type and set applicationIdSuffix to the test app's name (in this case, .debug).
android {
// ...
buildTypes {
// Prevents a signing error when building the production app
release {
signingConfig signingConfigs.debug
}
debug {
applicationIdSuffix ".debug"
}
}
}Step 2: Add a new app to AppsFlyer
Use the resulting package name as the app ID when adding the app to the AppsFlyer dashboard, or ask a team member with dashboard access to add it.
For example, if you have an app with the package name com.your.app and you use the Gradle configuration above, the test app's name will be com.your.app.debug. Pass this name as the app ID when adding the app to AppsFlyer.
Updated 19 days ago