Android (Kotlin) SDK Quickstart

Install and use the RudderStack Android (Kotlin) SDK on your Android apps.

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

Prerequisites

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

kotlin
dependencies {
    implementation("com.rudderstack.sdk.kotlin:android:<latest-version>")
}

Configure Maven Central

Make sure that Maven Central is included in your settings.gradle.kts file:

kotlin
pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}

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" />
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.

Step 2: Initialize the SDK

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

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",
            )
        )
    }
}

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

Step 3: Identify users

You can use the identify 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:

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")
    }
)

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 event triggered once the order is completed successfully is shown:

kotlin
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
analytics.track(
    name = "Order Completed",
    properties = buildJsonObject {
        put("revenue", 30)
        put("currency", "USD")
    }
)

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.


Questions? Let's figure it out together.

Join the RudderStack Slack community to connect with other users, customers, and the RudderStack team — or reach out for direct support.