Android (Kotlin)
Kotlin library for Android apps, with Jetpack Compose helpers. minSdk 21.
Events, screen views, identify, revenue and feature flags, with an offline queue. No Google Play services, no advertising ID, no OkHttp and no AndroidX dependency in the core library. Read Mobile apps for how sessions, screens and offline delivery work.
| Module | Artifact | What it is |
|---|---|---|
core | com.stancelens:stancelens-core | Pure Kotlin/JVM: queue, batching, retry, sessions, HTTP (HttpURLConnection). |
android | com.stancelens:stancelens-android | The Stancelens object, storage, device context, lifecycle flush. |
compose | com.stancelens:stancelens-compose | TrackScreen and TrackNavigation for Jetpack Compose. |
Install from source
The artifacts aren't on Maven Central yet. Build them into a local Maven repository from a checkout of the Stancelens repository. android and compose are only built when an Android SDK is configured (ANDROID_HOME, or sdk.dir in local.properties).
cd stancelens/packages/android
./gradlew publishAllPublicationsToLocalRepository # writes build/repo// settings.gradle.kts
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url = uri("/path/to/stancelens/packages/android/build/repo") }
}
}
// app/build.gradle.kts
dependencies {
implementation("com.stancelens:stancelens-android:1.0.0")
implementation("com.stancelens:stancelens-compose:1.0.0") // optional, Compose helpers
}The library manifest adds the INTERNET permission. R8/ProGuard needs no extra rules.
Quick start (Compose)
class App : Application() {
override fun onCreate() {
super.onCreate()
Stancelens.init(this, site = "SITE_ID", host = "https://stancelens.com")
}
}
@Composable
fun SettingsScreen() {
TrackScreen("Settings")
Button(onClick = { Stancelens.track("theme_changed", mapOf("theme" to "dark")) }) {
Text("Dark theme")
}
}With Navigation-Compose, track every destination instead of calling TrackScreen per screen:
val nav = rememberNavController()
TrackNavigation(nav) // screen name = route pattern, e.g. "profile/{id}"
NavHost(nav, startDestination = "home") { /* ... */ }Pass screenName = { destination, args -> ... } to choose names; return null to skip one. TrackScreen fires each time the composable enters the composition (first show, coming back, and after a rotation unless your Activity handles configuration changes). TrackNavigation fires once per destination change.
API
Stancelens.init(context, site, host, options = StancelensOptions())
Stancelens.track(name, properties = null)
Stancelens.screen(name, properties = null) // page view with path "/" + name
Stancelens.identify(userId, traits = null) // sends $identify
Stancelens.reset() // logout: new anonymous ID and session
Stancelens.revenue(amount, currency = "USD", event = "purchase", properties = null)
suspend fun Stancelens.flag(key): Flag // reports $exposure once per flag value
Stancelens.flag(key) { flag -> } // callback on the main thread
suspend fun Stancelens.flags(): Map<String, Flag> // no exposure events
Stancelens.optOut() / Stancelens.optIn() / Stancelens.isOptedOut
Stancelens.flush()
suspend fun Stancelens.flushAndWait(): BooleanFlaghaskey,enabled,variant(null for boolean flags) andvalue(false,trueor the variant key). Flags are cached for 60 seconds; when the server can't be reached, the last values for the same user are used, otherwise the flag is disabled.- All calls return immediately, are safe from any thread or coroutine, and run in call order on one background thread. Calls before
initare ignored with a Logcat warning.
Options
StancelensOptions(
flushAt = 20, // send when this many events are queued
flushIntervalMs = 30_000, // and on this interval
maxQueueSize = 1000, // oldest events dropped beyond this
sessionTimeoutMs = 30 * 60_000L,
flagsCacheMs = 60_000,
trackActivities = false, // screen view per Activity start ("CheckoutActivity" -> "Checkout")
activityScreenName = null, // (Activity) -> String?, null result skips
debug = false, // log to Logcat, tag "Stancelens"
)What it sends
Events go to POST {host}/api/event in batches of up to 50 with site, name, path (current screen), title (screen views), props, revenue, traits, ts (time of the call), vid, uid, session_id, platform (android), os (Android), os_version, device_model (manufacturer and model), device_type (mobile, tablet at 600 dp smallest width, or tv), app_version (versionName (versionCode)), language, timezone, screen (size in dp) and sdk (stancelens-android/1.0.0). The User-Agent is the system http.agent plus stancelens-android/<version>.
Behaviour
- IDs: a random install ID (
vid) in a private SharedPreferences file. No advertising ID, Android ID or hardware identifiers.reset()replaces it. - Queue: every event is appended to
noBackupFilesDir/stancelens-queue.jsonlbefore sending, so events survive process death and offline periods. The queue and IDs are excluded from backups. - Sending: when
flushAtevents are queued, everyflushIntervalMs, when the app goes to the background (last Activity stopped), and onflush(). - Retry: network errors, 408, 429 and 5xx keep the batch and retry with exponential backoff (about 1 s, doubling, with jitter, capped at 5 minutes;
Retry-Afterhonoured). Other 4xx responses drop the batch. - Offline limit: the server accepts events up to 72 hours old; older ones are dropped. Each event carries an event ID and each request
sent_at, so the server drops a repeat and corrects a wrong device clock. - Sessions: a new
session_idafter 30 minutes without events, background included; a session survives process restarts within that window. - Opt-out:
optOut()deletes queued events and stops tracking untiloptIn(), across launches. Flag lookups still work but send no exposures.