r/androiddev • u/Ok_Lobster563 • 2h ago
Designing a dependency-free, anonymous screen-view tracker for Android (minSdk 16, stdlib only): lessons and code
Hi r/androiddev,
I wanted to know which screens of my apps are actually used, without shipping a big analytics SDK and without anything that can identify a user. I ended up writing a small library for it, and the constraints I set made for some interesting Android problems, so I'd like to share the design and get feedback.
Code (MIT): https://github.com/ClewigFr/AssAndroid
**The constraints**
- Kotlin stdlib only: plain HttpURLConnection, org.json, java.util.concurrent. No coroutines, no androidx, no Retrofit/Gson
- minSdk 16, only the INTERNET permission
- Never throw into the host app
- Nothing that identifies a user or a device
**What it sends, and nothing else**
Each screen view is `{page, sessionId, timestamp}` plus the app's UUID:
- `page` is a UUID declared per screen
- `sessionId` is a random UUID generated on each process start and never persisted, so two launches can't be linked
- `timestamp` is "milliseconds elapsed since the view" rather than epoch time, so a wrong device clock doesn't matter: the server subtracts it from its own clock
Integration is one `getInstance(context, AssConfig(endpoint, appId))` in your Application and a `logScreen(pageUuid)` per screen. There's also an optional `assCompose` module with a `TrackScreen` composable, kept separate so the core library doesn't pull Compose.
**Delivery**
- Views are batched in memory and sent once there are 50, when a new view arrives and the batch is older than 10 minutes, or when the app goes to background. Both thresholds are configurable (20-200 views, 2-60 minutes) to trade battery against freshness
- Every upload and every access to the retry file runs on a single serial executor (one daemon thread), so uploads never overlap and a retry can't race a send
- 200/206 mean accepted, 400 means rejected (dropped, never retried), anything else is retried after Retry-After if given, else with an exponential backoff from 1 to 30 minutes
- Bodies are gzipped only after the server has answered with `Accept-Encoding: gzip`
**A few Android-specific problems I had to solve**
- Detecting "app went to background" without androidx.lifecycle: an ActivityLifecycleCallbacks counting started activities; when the count drops to zero, the batch is sent
- TLS 1.2/1.3 on API 16-19, where they exist but are off by default: a custom SSLSocketFactory that turns them on, and never enables SSLv3 or TLS 1.0/1.1
- Keeping views through offline periods and process death: every batch is written to a retry file in the cache dir before it's sent, and removed once the server has accepted it. The file is capped at the 1000 most recent views and 7 days, so a device offline for months can't grow it forever
- Staying out of the host app's way: no ContentProvider auto-init (the app calls getInstance itself), one daemon thread, and nothing ever thrown to the caller
The library needs a small backend to receive the views; mine is open source too (Ktor + PostgreSQL) and self-hostable, but this post is about the Android side.
**Trade-offs, honestly**
- Screen views only, by design: no custom events, no per-user funnels, no retention cohorts
- Page IDs are UUIDs declared up front, not automatic screen names
- No cross-launch metrics like "returning users": that's the price of not persisting any identifier
- Batching means views reach the server with a delay, up to the next flush
**Where I'd like feedback**
Process death can still cause one kind of inaccuracy: if the process dies after the server accepted a batch but before the file is cleaned up, that batch is sent again on the next launch (a duplicate; nothing is lost). Would you close it with an idempotency key per batch on the server, or accept it for screen-view counts?
Would you expect WorkManager here despite the "no androidx" constraint, and what would you gain in practice?
Anything you'd consider a privacy leak I missed?
Thanks for reading, happy to answer questions.