Android Quickstart (OpenMedKit)¶
OpenMedKit Android is the on-device counterpart to the Swift Package (OpenMedKit). It loads a de-identification model from the on-device model catalog and redacts clinical text entirely on the handset — no network access and no cloud fallback for PHI.
This page walks through the first-run path: add the Gradle dependency, load a model from the catalog, and redact a synthetic clinical note.
Requirements¶
| Requirement | Value |
|---|---|
| Minimum Android API | 26 (Android 8.0) |
| Compile SDK | 35 |
| Demo target SDK | 35 |
| Build JDK | 17 |
| Android Gradle Plugin | 8.7.3 (8.5.1 or newer for 16 KB zip alignment) |
| Consumer Kotlin | 1.9 or newer |
| Language | Kotlin (coroutines) |
The library performs inference on device via ONNX Runtime Mobile. See Android ONNX Export for producing a compatible model artifact and Android Span Parity for the cross-platform span guarantees. See Android QNN and NNAPI Acceleration for execution-provider selection, operator coverage, and deterministic CPU fallback.
Install¶
OpenMedKit Android is published from immutable OpenMed release tags through JitPack. Add the scoped repository in the consumer application's settings.gradle.kts:
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven {
url = uri("https://jitpack.io")
content { includeGroup("com.github.maziyarpanahi") }
}
}
}
After v4.0.0 is published, add this release coordinate in the module build.gradle.kts. The candidate's coordinate is not yet a published artifact:
After tag publication, JitPack resolves the immutable v4.0.0 tag and builds the openmedkit Android release component as an AAR. Public consumers do not need GitHub credentials.
Release Builds (R8 and APK Size)¶
The AAR packages the R8 keep rules OpenMedKit needs, so a release build with isMinifyEnabled = true (R8 full mode, the AGP 8 default) needs no OpenMedKit-specific rules. The repository verifies this with an R8-minified consumer app on every Android CI run; see the Android build notes.
DJL's tokenizers and JNA jars ship desktop JNI binaries as Java resources that Android never loads. Exclude them, or every APK grows by about 9.6 MB:
android {
packaging {
resources {
excludes += listOf(
"native/lib/**",
"com/sun/jna/*/*jnidispatch*",
)
}
}
}
With those excludes, OpenMedKit adds about 37 MB to an arm64-v8a APK, 26 MB to armeabi-v7a and 44 MB to x86 or x86_64, almost all of it native libraries (ONNX Runtime, the DJL tokenizer and the C++ runtime it links against). Ship ABI splits or an Android App Bundle so each device downloads one ABI.
Apps that already ship libc++_shared.so¶
The AAR packages the NDK r28c libc++_shared.so for every ABI, because the DJL tokenizer library links against it and no other dependency ships it. If another dependency also ships libc++_shared.so (React Native does), the build fails in mergeNativeLibs with 2 files found with path 'lib/<abi>/libc++_shared.so'. Keep one copy:
Keep the copy from the newest NDK that built any native library in the app; the DJL tokenizer needs one from NDK r27c or newer. See Shared C++ runtime for which copy AGP keeps and the license notice your app needs.
The core artifact has no OCR engine and requests no network permission. Add the optional ML Kit OCR adapter only if you need it; see Optional ML Kit OCR module for the license and network permissions it adds.
Quick Start: Kotlin¶
The snippet below discovers a model in the on-device catalog, resolves its cached directory, constructs an OpenMedKit facade, and de-identifies a synthetic, non-PHI clinical note. deidentify is a suspend function, so call it from a coroutine.
import android.content.Context
import com.openmed.openmedkit.OpenMedBackend
import com.openmed.openmedkit.OpenMedKit
import com.openmed.openmedkit.cache.ModelCache
import com.openmed.openmedkit.catalog.ModelCatalog
import java.io.File
suspend fun redactSyntheticNote(context: Context): String {
// 1. Load the bundled on-device model catalog and pick an entry.
val catalog = ModelCatalog.load(context)
val entry = catalog.entries.first()
// 2. Resolve the model's on-device directory from the local cache.
val cache = ModelCache(rootDirectory = File(context.filesDir, "openmed-models"))
require(cache.isAvailable(entry.repoId)) {
"Model ${entry.repoId} is not present on device yet."
}
val modelDirectory = cache.modelDirectory(entry.repoId)
// 3. Build the local-first backend and facade.
val backend = OpenMedBackend(modelDirectory = modelDirectory)
// 4. De-identify a synthetic (fictional, non-PHI) clinical note.
val syntheticNote =
"Patient: Jane Roe (synthetic). MRN 000-00-0000. " +
"Seen on 2024-01-02 for routine follow-up; contact 555-0100."
return OpenMedKit(backend).use { kit ->
val result = kit.deidentify(syntheticNote)
result.redactedText
}
}
By default deidentify applies the bundled hipaa_safe_harbor policy profile; pass policy = "..." to select another bundled profile. The returned PolicyDeidentificationResult exposes redactedText, the applied policyName, and the per-span actions.
Public API¶
| Symbol | Purpose |
|---|---|
OpenMedBackend | Local-first configuration pointing at an on-device model directory and tokenizer assets. Performs no network access. |
OpenMedKit | Closeable facade. deidentify(text, policy, …) redacts under a bundled policy; extractPii(text, …) and extractPiiChunked(text, …) return detected EntityPrediction spans. |
ModelCatalog | Reads the bundled openmed_model_catalog.jsonl asset into ModelCatalogEntry rows (repoId, formats, languages, family, task, canonicalLabels, …). |
ModelCache | Resolves and manages on-device model directories keyed by repoId. |
Local-First Guarantees¶
OpenMedKit Android is deliberately local-first:
- On-device only. All tokenization, inference, and de-identification run on the handset.
OpenMedBackendreads model and tokenizer assets from a local directory and performs no network access. - No cloud fallback for PHI. There is no remote inference path; clinical text is never transmitted off the device for processing.
- No PHI in logs. The library does not log input text, detected spans, or redacted output. Do not add application logging that echoes clinical text.
- Not a clinical decision. Output is an assistive redaction aid, not medical advice or a clinical decision. De-identification is best-effort and must be reviewed before any downstream use; it does not guarantee removal of every identifier.
Demo Apps¶
Two runnable Android demos exercise the on-device path end to end:
android/OpenMedDemo— Compose de-identification demo over a synthetic note.android/OpenMedScanDemo— on-device capture-and-redact demo.
See Also¶
- Swift Package (OpenMedKit) — the Apple-platform counterpart.
- Swift-Kotlin API Parity — cross-platform API alignment.
- Android ONNX Export — producing a compatible on-device model.
- Android QNN and NNAPI Acceleration — configuring delegate selection and CPU fallback.
- Model Manifest — the model catalog and reproducibility metadata.