Requirements
- Android 7.0 (API 24) or newer:
minSdk 24. - Kotlin Gradle plugin (
org.jetbrains.kotlin.android) 2.1 or newer. The SDK is built with Kotlin 2.2; an older plugin fails with “Module was compiled with an incompatible version of Kotlin”. - JVM target 11 or newer in your app module. AGP and JDK as your build needs.
kotlinx-coroutinesin the app to observe the SDK status. From Java, useandroidx.lifecycle:lifecycle-livedata-ktxinstead.- An SDK API key from truemetrics, see API keys.
Installation
Add the truemetrics Maven repository, insettings.gradle(.kts) under
dependencyResolutionManagement, or in the root build.gradle(.kts) under allprojects:
Permissions
The SDK’s manifest merges these permissions into your app. Do not remove any of them withtools:node="remove". After an SDK update, check the merged list again in
app/build/outputs/logs/manifest-merger-*-report.txt.
The SDK does not ask for permissions itself. Request them in your app before recording starts:
right after login, and on app start for a user who is already logged in. Reuse your app’s
permission flow if it has one. Order:
- Show a prominent disclosure first: an in-app dialog stating that the app collects location data in the background, also when the app is closed or not in use, and what it is used for. Google Play requires it before the first location request.
ACCESS_FINE_LOCATIONtogether withACCESS_COARSE_LOCATION.ACCESS_BACKGROUND_LOCATION, in a separate request after the foreground location was granted. On Android 11 and newer, Android sends the user to the settings page for “Allow all the time”.ACTIVITY_RECOGNITION,READ_PHONE_STATEandPOST_NOTIFICATIONS.
Status.AskForPermissions(permissions)
(SDK status). Request the listed permissions then. If your app’s flow supports it,
also ask to exclude the app from battery optimization.
Google Play Console. Declare background location access, and the foreground service types
Location and Data sync for the truemetrics recording service. If an older declaration lists
Special use for a truemetrics service, replace it with Data sync.
Recommended lifecycle
- Initialize once per process, and again only after
deinitialize()has returned. - Start recording once per route and stop it once. Do not stop and start per stop, per delivery, per screen, or when the app goes to the background. The one exception is a break that must not be recorded.
- Never stop recording from a crash handler or a lifecycle callback (
onDestroy,onTaskRemoved, app swiped away). A recording that was started and not stopped survives a crash, an OS kill, an app update or a reboot, and resumes on the nextinit(). - Deinitialize on logout only, not at route end: uploading the remaining data stops at
deinitialize()and continues only after the nextinit(). - Do not call
startRecording()on a plain app start; a started recording resumes on its own.
Notification.
init() starts the SDK’s foreground service, and its notification stays visible
until deinitialize(), also while no recording is running. Plan your app’s UX and the Play
declaration for it. On Android 13 and newer without POST_NOTIFICATIONS, the service still runs
and Android does not show the notification in the drawer.Quick start
Initialize in yourApplication while a user is logged in, and again after each login:
init(): onCreate() can run in the background, and the SDK then finishes starting
once your UI is shown (see FOREGROUND_SERVICE_START_REFUSED below).
Start and stop at route start and end:
Initialization
init() returns at once. The SDK then starts its foreground service and fetches its configuration
from truemetrics.
init() after install or
after deinitialize()), the SDK stays Initializing and keeps retrying. Do not add a client-side
timeout or assert.
When the SDK is ready
The SDK is ready from the firstInitialized, RecordingInProgress, RecordingStopped,
DelayedStart, TrafficLimitReached, ReadingsDatabaseFull or AskForPermissions status after
each init() until deinitialize(), also through later errors. There is no direct call for it,
and getDeviceId() is not a readiness signal: it returns an ID while the SDK is still
Initializing, and that ID can change once the configuration arrives.
Calls before the SDK is ready
startRecording()andstopRecording(): safe right afterinit(). The latest request is kept and runs once the service is up, also after a refused start. AstopRecording()beforeinit()is discarded; astartRecording()beforeinit()or afterdeinitialize()reportsError(NOT_INITIALIZED).logMetadata(): dropped, with a log warning. Keep events in your own queue until the SDK is ready; see Metadata.getActiveConfig(),getUploadStatistics(),getSensorStatistics():nulluntil the service is up.
Configuration
Recording start
init() does not start recording; recording runs from startRecording() until stopRecording()
or deinitialize(). This explicit start (SdkConfiguration.EXPLICIT_START, the default) is the
recommended setup. A recording that was started resumes on the next init().
An auto-start is possible but not recommended for courier apps:
Status.DelayedStart; stopRecording() in that window
cancels the start. Any negative value means explicit start.
SDK 1.3.0 to 1.5.x started recording on
init() by default. When you update from one of them, a
device that was recording keeps recording until it is stopped once with stopRecording() or
deinitialize().Custom notification
Replace the SDK’s notification with your own, on the SDK’s channelFOREGROUND_SERVICE:
Metadata
Log one event per delivery or pickup attempt, at the moment the driver finalizes it. The fields, theextra keys and the rules are on Metadata.
logMetadata(Map<String, String>) is deprecated.
Device ID
The SDK generates the device ID. Take it from the firstInitialized or RecordingInProgress
status after each init() and store it in your logs or backend next to the driver or session:
truemetrics support asks for it.
The ID changes after 30 days, at the next init() (unless truemetrics set another period for your
account), and when the app’s data is cleared. getDeviceId() returns the current ID once the
service is up, but it can still change while the SDK is Initializing.
Sensors
setAllSensorsEnabled(enabled) and getAllSensorsEnabled() switch all sensors on or off.
Deinitialize
Calldeinitialize() on logout. It can block for about 6 seconds, so call it off the main thread.
It stops a running recording, ends uploading until the next init(), and removes the notification.
SDK status
observeSdkStatus() (or the sdkStatus StateFlow) reports:
Error codes
Reported by SDK 1.6.3:
Reserved, not reported by SDK 1.6.3:
UPLOAD_ERROR, STORAGE_FULL (a full storage arrives as
ReadingsDatabaseFull), MISSING_NOTIFICATION_PERMISSION, TRAFFIC_USED_UP (arrives as
TrafficLimitReached). Keep an else branch for them.
Statistics
EXCELLENT 95 % or more, GOOD 80–95 %,
POOR 50–80 %, BAD below 50 %, UNKNOWN without data or while not recording.