KSafe — Universal Key/Value Persistence for Kotlin Multiplatform and Android
- Encrypted by default. Plain (unencrypted) when needed.
- Persist variables, Compose State, StateFlow, and serializable objects across Android, iOS, macOS, Desktop, and Web
- Easy to use by design — plus key rotation, cross-platform biometrics, and an encryption preflight


What is KSafe?
KSafe is a secure-by-default Kotlin Multiplatform key/value persistence library. Persist ordinary Kotlin variables, Compose MutableState, MutableStateFlow, and @Serializable objects across app restarts with one API on Android, iOS, macOS, JVM/Desktop, WASM, and Kotlin/JS. Encrypted (AES-256-GCM) by default; plain per-entry with mode = KSafeWriteMode.Plain.
var counter by ksafe(0)
counter++ // auto-encrypted (AES-256-GCM), auto-persisted, survives process death
Read and write it like any normal Kotlin variable — no suspend, no runBlocking, no DataStore boilerplate, no explicit encrypt/decrypt. Reads hit a hot in-memory cache; writes encrypt and flush in the background — synchronous, but never blocking. Reach for the suspend API (get / put) only when you want to await the disk flush.
- Easy? ✔ one-line setup, property-delegate API
- Encrypted by default? ✔ AES-256-GCM, hardware-backed where available
- Plain storage? ✔ opt out with one parameter
- Synchronous? ✔ non-blocking hot-cache reads
- Asynchronous? ✔ full suspend API for guaranteed disk flushes
Extras when you encrypt: biometrics (Face ID / Touch ID / Fingerprint — optional standalone ksafe-biometrics module) · root/jailbreak detection (WARN/BLOCK + analytics callback) · memory policy (RAM-exposure modes) · a one-line hardware-isolated DB passphrase for SQLCipher / SQLDelight / Room.
🤖 KSafe Skill for AI agents
KSafe ships an agentskills.io-compatible skill — skills/ksafe/SKILL.md — that teaches any AI agent (Claude Code, Codex, Gemini CLI, Copilot CLI, Junie) KSafe's patterns, anti-patterns and gotchas, so the code it writes for you is the code this README describes.
Claude Code — run both, in this order, once. Restart the session afterwards; skills load at session start:
/plugin marketplace add ioannisa/KSafe # register this repo as a plugin source
/plugin install ksafe@ksafe # install the skill from it
Any other agent — one command, then pick your agents from its prompt:
npx skills add ioannisa/KSafe
Forcing an update, installing without any tooling, and why you should not also hand-copy SKILL.md: docs/AI_AGENTS.md.
Demo & Videos
KSafe in action across many scenarios: KSafeDemo — Compose Multiplatform app.
Setup
1 - Add the Dependency
// commonMain or Android-only build.gradle(.kts)
implementation("eu.anifantakis:ksafe:3.3.2")
implementation("eu.anifantakis:ksafe-compose:3.3.2") // ← Compose state (optional)
implementation("eu.anifantakis:ksafe-biometrics:3.3.2") // ← Biometric auth (optional)
Skip ksafe-compose if you don't use Jetpack Compose or mutableStateOf persistence.
Skip ksafe-biometrics if you don't need Face ID / Touch ID / Fingerprint verification. The biometrics module is fully independent — it has no dependency on :ksafe and can be used on its own to protect any action in your app.
Note: kotlinx-serialization-json comes in transitively — don't add it yourself.