Skip to main content
The truemetrics SDK records sensor data on the courier’s phone and uploads it. Your app initializes the SDK, starts and stops recording, and logs one metadata event per delivery attempt. To let a coding agent do the integration, use the agent prompt.
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-coroutines in the app to observe the SDK status. From Java, use androidx.lifecycle:lifecycle-livedata-ktx instead.
  • An SDK API key from truemetrics, see API keys.

Installation

Add the truemetrics Maven repository, in settings.gradle(.kts) under dependencyResolutionManagement, or in the root build.gradle(.kts) under allprojects:
Then add the dependency to your app module:

Permissions

The SDK’s manifest merges these permissions into your app. Do not remove any of them with tools: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:
  1. 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.
  2. ACCESS_FINE_LOCATION together with ACCESS_COARSE_LOCATION.
  3. 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”.
  4. ACTIVITY_RECOGNITION, READ_PHONE_STATE and POST_NOTIFICATIONS.
When a permission is still missing, the SDK reports 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.
Timeline of a courier's day: init after login, the SDK goes from Initializing to Initialized, startRecording at route start, RecordingInProgress, stopRecording at route end, deinitialize at logout. The SDK's notification is visible from init to deinitialize.
  • 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 next init().
  • Deinitialize on logout only, not at route end: uploading the remaining data stops at deinitialize() and continues only after the next init().
  • 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 your Application while a user is logged in, and again after each login:
Keep the API key in your build config or secrets, never in source control. Do not add a foreground check around 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.
There is no timeout. Offline and without a cached configuration (first 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 first Initialized, 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() and stopRecording(): safe right after init(). The latest request is kept and runs once the service is up, also after a refused start. A stopRecording() before init() is discarded; a startRecording() before init() or after deinitialize() reports Error(NOT_INITIALIZED).
  • logMetadata(): dropped, with a log warning. Keep events in your own queue until the SDK is ready; see Metadata.
  • getActiveConfig(), getUploadStatistics(), getSensorStatistics(): null until the service is up.
If your app has no UI (headless, or driven by an MDM), it never comes to the foreground, so the SDK gets no chance to retry a refused start. Call deinitialize() and then init() again from a context Android allows, such as a receiver it exempts, and then startRecording() again: deinitialize() drops a pending request.

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:
While a delay is pending, the SDK reports 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 channel FOREGROUND_SERVICE:

Metadata

Log one event per delivery or pickup attempt, at the moment the driver finalizes it. The fields, the extra keys and the rules are on Metadata.
The map-based logMetadata(Map<String, String>) is deprecated.

Device ID

The SDK generates the device ID. Take it from the first Initialized 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

Call deinitialize() 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

Quality is the measured rate against the configured one: EXCELLENT 95 % or more, GOOD 80–95 %, POOR 50–80 %, BAD below 50 %, UNKNOWN without data or while not recording.

API reference

TruemetricsSdk

Flows

Data classes