Kotlin Health Connect sync with the Android SDK
Use HC Sync’s native Android SDK to read permitted Health Connect records and upload them for backend REST reads and signed webhooks. Install version 0.3.0 from the HC Sync Maven repository and mint a scoped user token on your authenticated backend.
Last updated: 2026-11-04. API source: HC Sync documentation.
How do you install from the Maven repository?
// settings.gradle.kts
dependencyResolutionManagement {
repositories { google(); mavenCentral(); maven("https://hcsync.dev/maven") }
}
// app/build.gradle.kts
implementation("dev.hcsync:hcsync:0.3.0")The repository is published from public/maven. Follow the Android manifest, permission rationale and backup exclusions before running the SDK. Exclude hcsync.xml shared preferences from cloud backup and device transfer because they contain the user token.
How do you connect a user?
# On your authenticated backend; derive user_id from the signed-in session.
curl -X POST https://hcsync.dev/v1/user-tokens \
-H "authorization: Bearer $HCSYNC_SECRET" -d '{"user_id":"maya"}' val connect = registerForActivityResult(HcSync.connectContract()) { r ->
when (r) {
is ConnectResult.Connected -> toast("Synced ${r.uploaded} records")
is ConnectResult.Failed -> log(r.kind)
ConnectResult.Cancelled -> {}
}
}
connect.launch(ConnectOptions(appName = "My Coach", userToken = token,
types = setOf("steps", "sleep", "heart_rate"), privacyUrl = "https://myapp.com/privacy"))Register the contract with your Activity lifecycle and launch it from a user action. Replace the sample app name and privacy URL with your own. The Connect screen checks availability, requests selected permissions and runs the first sync.
How does ongoing sync work?
The SDK backfills 30 days, then uploads changes hourly in the background with background access. Without it, call syncNow while your app is open.
// After configuration and consent: HcSync.syncOnAppStart(application) // In a coroutine while the app is open: val result = HcSync.syncNow(context) // On logout: HcSync.logout(context)
syncNow returns SyncState. Observe HcSync.state for uploaded counts and errors, and handle revoked permissions. See the SDK changelog.
How does the backend read the data?
curl 'https://hcsync.dev/v1/records?user_id=maya&type=steps' \ -H "authorization: Bearer $HCSYNC_READ_KEY"
Before upload, collect explicit consent explaining your purpose, selected types, US storage through HC Sync and withdrawal. Declare only needed Android permissions and request them at runtime. Follow the consent and publishing checklist. Keep the secret and read keys on your backend; a device user token can only write one user. Stop sync and clear credentials on logout or withdrawal.
Frequently asked questions
Where is the Kotlin SDK hosted?
Add maven("https://hcsync.dev/maven") and implementation("dev.hcsync:hcsync:0.3.0") to your Android build.
What if background access is denied?
Call HcSync.syncNow(context) while the app is open; it returns the resulting SyncState.