# Initialize SDK

To make all of the features of the Playtime SDK available, you need to initialize it first. This initialization happens asynchronously in the background, with its completion indicated by a callback method. Make sure to initialize the SDK immediately after the app launches and after the user login in the app.

### Best practices for the SDK initialization:

* **Initialize early:** Initialize the SDK as soon as possible after your app starts. This ensures all SDK features are ready for use right away.
* **Initialize after your app authentication:** Once a user signs up or logs in in your app, initialize the SDK again with the latest user details. This updates the userID within the SDK and ensures reward will be delivered to the user. If a user is not login in your app, you can pass blank or 0.
* **Initialization triggers:** Call the init method at the app start and after login

### Initialization

The initialization will run in the main application process asynchronously in the background and notify you when it is finished by invoking the OfferWallInitListener's onInitSuccess or onAlreadyInitializing or onInitFailed method

**Important:** Initialization must happen on the main thread since it's a UI-related action. Calling any of the init methods off the main thread could lead to app disruptions.

{% tabs %}
{% tab title="Java" %}

```java
PlaytimeAds.getInstance().init(context, applicationKey, userID, new OfferWallInitListener() {
     @Override public void onInitSuccess() {
      Log.e("TAG", "onInitSuccess");
      }
     @Override public void onAlreadyInitializing() {
      Log.e("TAG", "onAlreadyInitializing");
      }
     @Override public void onInitFailed(String error) {
      Log.e("TAG", "onInitFailed: " + error);
      }
  });
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
PlaytimeAds.getInstance().destroy()// clears your previous session
PlaytimeAds.getInstance().init(context, applicationKey, userID,object : OfferWallInitListener {
      override fun onInitSuccess() {
      Log.e("TAG", "onInitSuccess");
      }
      override fun onAlreadyInitializing() {
      Log.e("TAG", "onAlreadyInitializing");
      }
      override fun onInitFailed(error: String?) {
      Log.e("TAG", "onInitFailed: " + error);
      }
  })
```

{% endtab %}
{% endtabs %}

### Parameters:

| Parameter      | Type   | Explanation                                                                                                                                                                                                      |
| -------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| UserID         | String | A custom that will be used to identify your user uniquely. This will be sent back in the postback. If user is not logged-in in your app, please pass 0 or blank in UserId. Max character limit is 50 characters. |
| applicationKey | String | Application key provided in publisher panel for your application                                                                                                                                                 |

{% hint style="danger" %}
Re-initialize Playtime SDK after user login. Otherwise rewarding the user is not guaranteed

Note: If SDK is already initialized, You need to destroy previous session first in order to initialize it again for new UserId.
{% endhint %}

### Clear Session on Logout

Don't miss to clear Playtime session when users logout from the app.

{% tabs %}
{% tab title="Java" %}

```java
PlaytimeAds.getInstance().destroy(); // clears your previous session
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
PlaytimeAds.getInstance().destroy()// clears your previous session
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.playtimeads.com/sdk-integration/android-integration/initialize-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
