> For the complete documentation index, see [llms.txt](https://docs.sentiance.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sentiance.com/getting-started/sdk-integration/3.-sdk-initialization/android.md).

# Android

Initialization allows the SDK to perform detections in the background. You must always initialize the SDK before interacting with it. Only a limited set of SDK methods are safe to invoke on an uninitialized SDK.

## Steps

{% stepper %}
{% step %}

### Create an Application Class

Initialization must be done in the <mark style="color:$success;">**`onCreate()`**</mark> method of your <mark style="color:$success;">**`Application`**</mark> class. If you don't yet have a custom application class, first create a new class that extends <mark style="color:$success;">**`Application`**</mark>.

```kotlin
package com.example.myapp

import android.app.Application

class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
    }
}
```

Then reference this new class in the application tag of the <mark style="color:$success;">**`AndroidManifest.xml`**</mark>

{% code title="AndroidManifest.xml" %}

```xml
<application
        android:name="com.example.myapp.MyApplication"
        ...
        >
        ...
</application>
```

{% endcode %}
{% endstep %}

{% step %}

### Initialize the SDK

In the <mark style="color:$success;">**`onCreate()`**</mark> method of your <mark style="color:$success;">**`Application`**</mark> class, call <mark style="color:$success;">**`initializeAsync()`**</mark>.

{% hint style="danger" %}
You must call **`initializeAsync()`**  on the main thread, directly from within **`onCreate()`**. Do not postpone the call or move it to a background thread, otherwise SDK detections will become unreliable.

To minimize the impact of initializing the SDK on your app's startup duration, we have designed **`initializeAsync()`** to be very light and fast.

For more information about this requirement, see [this page](/sdk/appendix/user-deletion.md).
{% endhint %}

```kotlin
package com.example.myapp

import android.app.Application
import android.util.Log
import com.sentiance.sdk.Sentiance

class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
        
        val options = SentianceOptions.Builder(this).build()
        Sentiance.getInstance(this).initializeAsync(options)
            .addOnCompleteListener { operation ->
                if (operation.isSuccessful) {
                    Log.d(TAG, "Initialization succeeded");
                } else {
                    val reason = operation.error.failureReason.name
                    val throwable = operation.error.throwable
                    Log.e(TAG, "Intialization failed with reason ${reason}", throwable);
                }
            }
    }
    
    companion object {
        private const val TAG = "Sentiance SDK"
    }
}
```

{% endstep %}

{% step %}

### Customize the Android Notification (Optional)

When the Sentiance SDK runs in the background, it starts a foreground service. This causes Android to display a notification in the notification shade, and an icon on the system bar.

You can customize this notification to tailor its appearance and behavior to match the branding and user experience of your application. This allows you to provide a seamless and consistent experience to your users while the Sentiance SDK operates in the background.

Please note that customizing the notification should be done carefully to ensure that it complies with Android guidelines and user preferences. By doing so, you can enhance the overall user experience and maintain a cohesive presentation of your app and the Sentiance SDK's background functionality.

You can specify the notification that Android should use via the SDK's initialization option:

```kotlin
override fun onCreate() {
    super.onCreate()

    val options = SentianceOptions.Builder(this)
        .setNotification(notification, notificationId)
        .build()
    Sentiance.getInstance(this).initializeAsync(options)
        ..
}
```

If the <mark style="color:$success;">**`notificationId`**</mark> that you specify here matches the ID of another notification, Android will show only one notification (the last published one). If your app has an ongoing notification of its own, you can pass the same ID to avoid multiple notifications during SDK detections.

Be sure to check our [notification management page](https://docs.sentiance.com/important-topics/sdk/appendix/android/notification-management) for more on the best practices of defining a service notification.
{% endstep %}

{% step %}

### Next: User Creation

After having successfully initialized the Sentiance SDK, you must now create a user. To do so, follow the instructions on [this page](/getting-started/sdk-integration/4.-user-creation.md).
{% endstep %}
{% endstepper %}
