Android integration
How an Android build reaches the API: the base URL per build, the staging key policy, the release guard, and how to talk to the API (sign-in, tokens, lists, errors, idempotency). The iOS lines are kept where both apps share a rule.
Rendered from docs/mobile-api-connection.md at commit 8565409 (main). The schema of record is the API reference; production, staging and main differ where Versions says so.
Base URL per build type
// app/build.gradle.kts
android {
buildFeatures { buildConfig = true }
buildTypes {
debug {
buildConfigField("String", "ROOL_API_BASE_URL", "\"https://api.staging.rool.am\"")
}
release {
buildConfigField("String", "ROOL_API_BASE_URL", "\"https://api.rool.am\"")
}
}
}
Use flavors instead if you need several debug targets (local backend on the emulator, staging). A signed build for testers is a staging variant (the staging URL and, if needed, the key), not the release build type that ships to production; release points at production and has no key (§4a).
4a. The staging key (staging builds)
api.staging.rool.am answers 404 to any request without the team key header X-Rool-Staging-Key. This section is the accepted policy for where that key may live (decided by Rudolf, recorded under ROOL-217; not up for renegotiation here).
Policy.
- One shared key. It may be built into staging builds for the team and for invited beta testers. The permission is for staging builds, whether or not they are signed; it is not limited to the debug build type.
- Closed distribution only (an internal or invitation-only channel). Never a public store listing.
- Production builds must not contain the key. A build that targets
https://api.rool.amhas no staging key in code, resources or configuration; make the build fail if one is configured. - The key enters a build through local or CI configuration, never through Git, build logs, crash reports or screenshots. Get it from the server admins; do not commit it (read it from a local, untracked config or a CI secret).
- It can be extracted from an APK or IPA, so treat it as known to every tester. It gates the staging environment (test data) and does not replace user sign-in: users still authenticate with their own staging accounts.
- Rotation. The key is changed after the beta, and earlier if it leaks. A rotation makes every tester build that carries the old key stop working until it is replaced with a build carrying the new one.
// Android, staging variant only: an OkHttp interceptor (the key from local or CI configuration)
class StagingKeyInterceptor(private val key: String) : Interceptor {
override fun intercept(chain: Interceptor.Chain) = chain.proceed(
chain.request().newBuilder().header("X-Rool-Staging-Key", key).build()
)
}
// iOS, staging configuration only (never the production configuration)
request.setValue(stagingKey, forHTTPHeaderField: "X-Rool-Staging-Key")
The header sits alongside Authorization: Bearer <access token>; the two do not interfere.
Behaviour, verified 2026-10-06: without the header, or with a wrong one, every path answers 404 with an empty body and no content type (not problem+json), including /health/live. With the right header the API answers exactly as production does. An empty 404 means "key missing or wrong", never "resource not found". The key is not in this repository; the server admins keep it in their password manager, and a rotation makes every copy stale. Never put it in a production build, a log or a crash report.
5. Guard in both apps
At startup, in release builds only:
#if !DEBUG
precondition(baseURL.scheme == "https", "Release builds must use HTTPS")
#endif
if (!BuildConfig.DEBUG) {
check(BuildConfig.ROOL_API_BASE_URL.startsWith("https://")) { "Release builds must use HTTPS" }
}
6. Talking to the API
Smoke test. GET {base}/health/live answers {"status":"ok"}. GET {base}/api/v1/makes answers {"data":[],"page":{...}} on the VPS today: an empty catalog is expected, not a bug. /health/ready and /internal/* are not reachable from outside.
Models. Generate them from openapi.json at a known commit of rool-backend and follow the client rules in public-api-contract.md §5: ignore unknown fields, map unknown enum values to a safe fallback, treat ids as opaque strings, branch on code and never on text. Operations marked x-implemented: false (Google sign-in, intelligence) are not served yet: build against the fixtures.
UI strings come from GET /api/v1/localization/{en|hy|ru}, public, with a strong ETag: cache each language with its ETag, revalidate with If-None-Match, keep the cache on 304 or any error, fall back to the bundled catalog. The full client contract is localization.md §3.
Lists use { "data": [...], "page": { "nextCursor": ..., "hasMore": ... } }. Fetch the next page with ?cursor=<nextCursor>, optionally &limit= (1–100).
Errors are application/problem+json. Show title or your own text, branch on code, log requestId. 5xx responses carry no detail.
Creates that declare Idempotency-Key (vehicles, odometer readings, service events) get a fresh UUID per user action; reuse it only to retry that same action.
Sign-in
| Step | Call | Notes |
|---|---|---|
| Register / log in | POST /api/v1/auth/register, POST /api/v1/auth/login | Returns an access token (JWT, 15 min) and a refresh token (opaque, 60 days) |
| Authenticated call | Authorization: Bearer <access token> | |
| Access token expired | 401 unauthenticated → refresh once, retry the call once | |
| Refresh | POST /api/v1/auth/refresh | Rotates: store the new refresh token. Reusing an old one revokes the whole session family |
| Refresh refused | 401 refresh_token_invalid or session_revoked | Sign in again |
| Log out | POST /api/v1/auth/logout | Bearer token and { refreshToken }. 204, effective at once. A second logout with the same (revoked) token is 401: treat 401 as "already signed out" |
| Too many attempts | 429 rate_limited with Retry-After (seconds) |
- Auth0 sign-in (
POST /api/v1/auth/auth0, ROOL-165, ADR 0018) is optional and off on the VPS until it is configured; it ends in the sameAuthSessionas email sign-in. - Store the refresh token in the Keychain (iOS) or Android Keystore-backed storage. Never in plain
UserDefaultsorSharedPreferences, never in logs. - Allow one refresh in flight at a time. Two parallel refreshes with the same token look like reuse and sign the user out on every device in that family (verified on staging: one call answers 200, the other 401
session_revoked, and the winner's new tokens die with it). - A refresh whose response you did not see cannot be retried. The server may already have rotated the token, and there is no grace window, so a retry with the old token answers
session_revokedand ends the sign-in. Save the new token before using it, and send the user to sign-in when the outcome is unknown. Details and a checklist: public-api-contract.md §10.1. - Clear local caches when switching between the local mock or fixtures and a real backend: fixture ids are not production ids.
7. Before a release build ships
- The base URL is
https://. - The release Info.plist has no
NSAppTransportSecurity; the iOS build phase check passes. - The merged release manifest has no cleartext permission and no debug network config.
- The startup HTTPS guard is in place.
- No test credentials or dev headers (
X-Rool-Dev-User) in release code paths. - A production build contains no staging key and does not point at the staging host.