SDKの実装

Android SDKを初期化し開始する方法を説明します。

始める前に

  • Android SDKをインストールする必要があります
  • Ensure that in your app build.gradle file, applicationId's value (in the defaultConfig block) matches the app's app ID in AppsFlyer.
  • AppsFlyer Devキーを取得してください。SDKを正常に初期化するために必要です
  • The codes in this document are example implementations. Make sure to change the <AF_DEV_KEY> and other placeholders as needed.
  • このドキュメントに含まれているステップは、特に明記されていない限り全て必須です。

Android SDKの初期化

グローバルApplicationクラス/サブクラス内でSDKを初期化することをお勧めします。これにより、全てのシナリオ(例:ディープリンク)でSDKが開始できます。

ステップ1:AppsFlyerLibのインポート
In your global Application class, import AppsFlyerLib:

import com.appsflyer.AppsFlyerLib;
import com.appsflyer.AppsFlyerLib

ステップ2: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. 第一引数には、AppsFlyer Devキーを指定してください。
  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. 第三引数にはApplication Contextを指定します。

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

オプション
You can defer the SDK initialization by calling start from an Activity class, instead of calling it 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.

⚠️

重要なお知らせ

アプリがアクティビティから start を呼び出す場合は、アクティビティコンテキストをSDKに渡す必要があります。
アクティビティコンテキストを渡さないと、SDKがトリガーされないため、アトリビューションデータとアプリ内イベントが失われます。

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 every 200 response 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.

次の例は、ApplicationクラスからSDK初期化を起動する方法を示しています。

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)
        // ...
    }
    // ...
}

Githubリンク

デバッグモードの有効化

オプション
You can enable debug logs by calling setDebugLog:

AppsFlyerLib.getInstance().setDebugLog(true);
AppsFlyerLib.getInstance().setDebugLog(true)

📘

注意

To see full debug logs, make sure to call setDebugLog before invoking other SDK methods.

を参照してください。

🚧

警告

機密情報の漏洩を防ぐため、アプリをリリースする前にデバッグログが無効になっていることを確認してください。

連携のテスト

オプション
連携テストの詳細な手順は、Android SDK連携テストガイドに関する記事を参照してください。