# Android (Kotlin) SDK Quickstart


This guide will walk you through setting up the Android (Kotlin) SDK and sending your first events to RudderStack.

{{< version-badge registry="maven" package="com.rudderstack.sdk.kotlin/android" label="Maven Central" fallback="1.7.0" href="https://central.sonatype.com/artifact/com.rudderstack.sdk.kotlin/android" >}}

## Prerequisites

- You must have **Android Studio** installed on your system.
- Sign up for [RudderStack](https://app.rudderstack.com/signup).
- Set up a new Android (Kotlin) source in the RudderStack dashboard and note its {{< glossary_tooltip "write-key" >}}. 
- You will also need the {{< glossary_tooltip "data-plane-url" >}} associated with your RudderStack workspace.

## Step 1: Install Android (Kotlin) SDK

The steps covered in the below sections will help you integrate the RudderStack Android (Kotlin) SDK into your Android project:

### Add dependencies

{{< tabs tabTotal="3" >}}
{{% tab tabName="Kotlin DSL (build.gradle.kts)" %}}
```kotlin
dependencies {
    implementation("com.rudderstack.sdk.kotlin:android:<latest-version>")
}
```
{{% /tab %}}
{{% tab tabName="Groovy (build.gradle)" %}}
```groovy
dependencies {
    implementation 'com.rudderstack.sdk.kotlin:android:<latest-version>'
}
```
{{% /tab %}}
{{% tab tabName="Using version catalogs (TOML file)" %}}
Add the dependency to your `libs.versions.toml` file:
```toml
[versions]
rudderstack = "<latest-version>"

[libraries]
rudderstack-kotlin = { module = "com.rudderstack.sdk.kotlin:android", version.ref = "rudderstack" }
```
Then, include it in your `build.gradle.kts`:
```kotlin
dependencies {
    implementation(libs.rudderstack.kotlin)
}
```
{{% /tab %}}
{{< /tabs >}}

### Configure Maven Central

{{< tabs tabTotal="3" >}}
{{% tab tabName="Kotlin DSL (settings.gradle.kts)" %}}
Make sure that Maven Central is included in your `settings.gradle.kts` file:

```kotlin
pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}
```
{{% /tab %}}
{{% tab tabName="Groovy (settings.gradle)" %}}
For Groovy (`settings.gradle`), add:

```groovy
pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}
```
{{% /tab %}}
{{< /tabs >}}

### Add the `INTERNET` permission

Add the following permission to your app's `AndroidManifest.xml` so the SDK can send network requests:

```xml
<!-- Required: Allows access to the internet for network communication -->
<uses-permission android:name="android.permission.INTERNET" />
```

{{< warning >}}
The Android (Kotlin) SDK does not declare this permission for you. You must add it to your app manifest for the SDK to send network requests.
{{< /warning >}}

## Step 2: Initialize the SDK

Before tracking any events, initialize the Android (Kotlin) SDK in your `Application` class, as shown:

{{< tabs tabTotal="2" >}}
{{% tab tabName="Kotlin" %}}
```kotlin
import android.app.Application
import com.rudderstack.sdk.kotlin.android.Analytics
import com.rudderstack.sdk.kotlin.android.Configuration

class MyApplication : Application() {
    lateinit var analytics: Analytics

    override fun onCreate() {
        super.onCreate()
        analytics = Analytics(
            configuration = Configuration(
                writeKey = "WRITE_KEY",
                application = this,
                dataPlaneUrl = "DATA_PLANE_URL",
            )
        )
    }
}
```
{{% /tab %}}
{{% tab tabName="Java" %}}
```java
import com.rudderstack.sdk.kotlin.android.Configuration;
import com.rudderstack.sdk.kotlin.android.javacompat.ConfigurationBuilder;
import com.rudderstack.sdk.kotlin.android.javacompat.JavaAnalytics;

public class MyApplication extends Application {

    public JavaAnalytics analytics;

    @Override
    public void onCreate() {
        super.onCreate();

        Configuration configuration = new ConfigurationBuilder(this, "WRITE_KEY", "DATA_PLANE_URL")
                .setTrackApplicationLifecycleEvents(true)
                .setGzipEnabled(true)
                .build();

        analytics = new JavaAnalytics(configuration);
    }
}
```
{{% /tab %}}
{{< /tabs >}}

Replace the `WRITE_KEY` and `DATA_PLANE_URL` parameters with the Android (Kotlin) source write key and the data plane URL obtained in [Prerequisites](#prerequisites).

## Step 3: Identify users

You can use the [`identify`]({{< ref "event-spec/standard-events/identify.md" >}}) event to identify a user and associate them with their actions. It also enables you to record any traits about them like their name, email, etc.

You can use the `identify` method as follows:

{{< tabs tabTotal="2" >}}
{{% tab tabName="Kotlin" %}}
```kotlin
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put

analytics.identify(
    userId = "1hKOmRA4el9Zt1WSfVJIVo4GRlm",
    traits = buildJsonObject {
        put("name", "Alex Keener")
        put("email", "alex@example.com")
    }
)
```
{{% /tab %}}
{{% tab tabName="Java" %}}
```java
import java.util.HashMap;

HashMap<String, Object> traits = new HashMap<>();
traits.put("name", "Alex Keener");
traits.put("email", "alex@example.com");

analytics.identify("1hKOmRA4el9Zt1WSfVJIVo4GRlm", traits);
```
{{% /tab %}}
{{< /tabs >}}

## Step 4: Track user actions

Once the Android (Kotlin) SDK is initialized, you can send track user actions and send them as events.

A sample [`track`]({{< ref "event-spec/standard-events/track.md" >}}) event triggered once the order is completed successfully is shown:

{{< tabs tabTotal="2" >}}
{{% tab tabName="Kotlin" %}}
```kotlin
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
analytics.track(
    name = "Order Completed",
    properties = buildJsonObject {
        put("revenue", 30)
        put("currency", "USD")
    }
)
```
{{% /tab %}}
{{% tab tabName="Java" %}}
```java
import java.util.HashMap;

HashMap<String, Object> properties = new HashMap<>();
properties.put("revenue", 30);
properties.put("currency", "USD");

analytics.track("Order Completed", properties);
```
{{% /tab %}}
{{< /tabs >}}

In the above snippet, the `track` method logs an event called `Order Completed` along with two event properties `revenue` and `currency` that provide additional context to the event.

<br />
