Integrar o SDK
Saiba como inicializar e iniciar o Android SDK.
Antes de começar
- Você deve instalar o SDK do Android.
- Ensure that in your app
build.gradle
file,applicationId
's value (in thedefaultConfig
block) matches the app's app ID in AppsFlyer. - Obtenha a chave do desenvolvedor da AppsFlyer. É necessário inicializar com êxito o SDK.
- The codes in this document are example implementations. Make sure to change the
<AF_DEV_KEY>
and other placeholders as needed. - Todas as etapas deste documento são obrigatórias, salvo quando sinalizado do contrário.
Inicializando o SDK do Android
É recomendável inicializar o SDK na classe/subclasse global Aplicativo. Isso é para garantir que o SDK possa começar em qualquer cenário (por exemplo, deep links).
Etapa 1: importar AppsFlyerLib
In your global Application class, import AppsFlyerLib
:
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.AppsFlyerLib
Etapa 2: inicializar o 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)
- O primeiro argumento é sua chave do desenvolvedor da AppsFlyer.
- The second argument is a Nullable
AppsFlyerConversionListener
. If you don't need conversion data, we recommend passing anull
as the second argument. For more information, see Conversion data. - O terceiro argumento é o Contexto do Aplicativo.
Iniciando o SDK do Android
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
OPCIONAL
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 notice
If the app calls
start
from 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 every200
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.
Exemplo completo
O exemplo a seguir demonstra como inicializar e iniciar o SDK a partir da classe Aplicativo.
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)
// ...
}
// ...
}
Ativação do modo de depuração
OPCIONAL
You can enable debug logs by calling setDebugLog
:
AppsFlyerLib.getInstance().setDebugLog(true);
AppsFlyerLib.getInstance().setDebugLog(true)
Observação
To see full debug logs, make sure to call
setDebugLog
before invoking other SDK methods.Veja o exemplo.
Aviso
Para evitar o vazamento de informações confidenciais, verifique se os registros de depuração estão desativados antes de distribuir o aplicativo.
Testando a integração
OPCIONAL
Para obter instruções detalhadas sobre testes de integração, consulte o Guia de testes de integração do SDK para Android.
Atualizado 2 dias atrás