> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trevosdk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Android

> Experiments and tracking for Kotlin and Java Android apps, assigning the same variants as the web.

`com.trevosdk:trevosdk-android` runs experiments and records events in Android apps, assigning
the same variant the web SDK does for the same user. Kotlin, no dependencies beyond the Kotlin
standard library, Android 5.0 (API 21) and later. Kotlin projects need Kotlin 2.0 or later; Java
projects work as they are.

```kotlin theme={null}
// build.gradle.kts
dependencies {
    implementation("com.trevosdk:trevosdk-android:0.1.3")
}
```

The library declares the `INTERNET` permission, which merges into your manifest.

## Create the client

Use a **Mobile** key (`tsk_live_…`). Keep it in your build configuration, for example a
`buildConfigField`, and create one client in `Application.onCreate`:

```kotlin theme={null}
class App : Application() {
    override fun onCreate() {
        super.onCreate()
        trevo = TrevoClient.create(this, TrevoOptions(apiKey = BuildConfig.TREVO_API_KEY))
    }

    companion object {
        lateinit var trevo: TrevoClient
    }
}
```

The client reads the build's `versionName`, so `minAppVersion` gating and the version on every
event work with no setup. Every method is safe to call from any thread. Create it in the main
process only: a second process would share its files.

## Read a variant

`getVariant` is synchronous and safe on the main thread. It returns `control` until stored
state and the first config fetch have loaded, so wait for `ready()` (a suspend function) or
`whenReady { }` before showing experiment UI:

```kotlin theme={null}
@Composable
fun CheckoutButton() {
    var variant by remember { mutableStateOf<String?>(null) }
    LaunchedEffect(Unit) {
        App.trevo.ready()
        variant = App.trevo.getVariant("checkout-v2")
    }
    when (variant) {
        null -> Unit
        "treatment" -> Button(onClick = { App.trevo.track("checkout_started") }) { Text("Start free trial") }
        else -> Button(onClick = { App.trevo.track("checkout_started") }) { Text("Subscribe") }
    }
}
```

Each call records an **exposure** the first time. Pass `trackExposure = false` to read without
recording one. `onChange` calls a listener on the main thread whenever config or identity
changes; keep the returned `TrevoSubscription` and `cancel()` it when the screen goes away.

## Parameters

A [parameter](https://docs.trevosdk.com/concepts/parameters.md) lets later experiments change a value with no release:

```kotlin theme={null}
val headline = trevo.getParameter("paywall.headline", "Subscribe")
val trialDays = trevo.getParameter("paywall.trial_days", 7)
```

There are overloads for `String`, `Boolean`, `Int`, `Long` and `Double`. A value of another
type, or a number that isn't whole for `Int` or `Long`, serves the default.

A `String` parameter can carry [text per language](https://docs.trevosdk.com/concepts/parameters.md#lists-and-translated-text):
pass your app's own translation as the default, and if users can pick the app's language, call
`trevo.setLocale("pt-BR")` when they do. Without it the SDK reads the app's configured locale.

## Track events and identify users

```kotlin theme={null}
trevo.track("checkout_completed", mapOf("plan" to "pro", "seats" to 3))

trevo.identify(user.id)   // after sign-in, and on launch when auth restores a user
trevo.reset()             // on sign-out: clears the user and starts a new anonymous id
```

Property values are strings, numbers, booleans, lists, maps or null. Events are queued on disk
and sent in batches every 15 seconds, when the app moves to the background, and on `flush()`.
See [Mobile experiments](https://docs.trevosdk.com/concepts/mobile-experiments.md) for how experiments ramp on mobile, how
the SDK survives a lost network, and the Google Play data safety answers.

## Options

| Option | Default | Notes |
| - | - | - |
| `apiKey` | | Required. A Mobile key, `tsk_live_…` |
| `appVersion` | The app's `versionName` | Sent on every event; compared with `minAppVersion` |
| `bootstrapConfig` | empty | Assign before storage and the network answer |
| `pollIntervalMs` | 300000 | Config is also refreshed on every foreground |
| `flushIntervalMs` | 15000 | Also flushes when the app moves to the background |
| `maxBatchSize` | 50 | Capped at 500 by the server |
| `configUrl`, `ingestionUrl` | Trevo | Endpoint overrides for proxied installs |
| `storage`, `transport`, `lifecycle` | Files, HttpURLConnection, activity lifecycle | Replace for tests or your own stack |
| `onError` | Logcat | Background failures; the SDK never throws |

## Correctness

The library runs the shared conformance vectors in CI, so a change that would assign a user
differently from the web fails the build. The rules are in the
[bucketing spec](https://docs.trevosdk.com/reference/bucketing-spec.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.