Integrate SDK

Learn how to initialize and start the Android SDK.

Before you begin

  • You must install the Android SDK
  • Get the AppsFlyer dev key. It is required to successfully initialize the SDK
  • The codes in this document are example implementations. Make sure to change the <AF_DEV_KEY> and other placeholders as needed
  • All the steps in this document are mandatory unless stated otherwise.

Initializing the Android SDK

Starting the SDK and getting attribution data in Android

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.AppsFlyerLib

Step 2: Initialize the SDK
In the global Application onCreate, call init with the following arguments:

AppsFlyerLib.getInstance().init(<AF_DEV_KEY>, null, this);
AppsFlyerLib.getInstance().init(<AF_DEV_KEY>, null, this)
  1. The first argument is your AppsFlyer dev key.
  2. The second argument is a Nullable AppsFlyerConversionListener. If you don't need conversion data, we recommend passing a null as the second argument. For more information, see Conversion data.
  3. 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.

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(<AF_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(<AF_DEV_KEY>, null, this)
        AppsFlyerLib.getInstance().start(this)
        // ...
    }
    // ...
}

Enabling debug mode

You can enable debug logs by calling setDebugLog.

Testing the integration

For detailed integration testing instructions, see the testing article.

Updated 22 days ago



Integrate SDK


Learn how to initialize and start the Android SDK.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.