Kotlin SDK
Add embedded PostgreSQL to an Android app using coroutine APIs.
Use dev.oliphaunt:oliphaunt-android in Android apps with API level 24 or later. The SDK provides suspend functions and supports arm64-v8a and x86_64 native targets.
Install
Ensure mavenCentral() is available in both plugin and dependency repositories in settings.gradle.kts. Apply the Oliphaunt plugin alongside your app's existing Android/Kotlin plugins:
plugins {
id("com.android.application")
id("dev.oliphaunt.android") version "0.3.0"
}
dependencies {
implementation("dev.oliphaunt:oliphaunt-android:0.3.0")
}Select the initialization resources for new Android databases:
oliphaunt {
seedProfile.set("standard")
}Use "icu" instead if your database needs ICU collations. The plugin packages the selected seed and runtime resources. Build and run a native Android application after changing this configuration.
Run your first query
Call this function from a coroutine. It opens disposable storage, binds a value, reads a typed result, and closes the database.
import android.content.Context
import dev.oliphaunt.*
suspend fun firstQuery(context: Context) {
val db = Oliphaunt.open(context.applicationContext)
try {
val result = db.query(
"SELECT $1::int4 AS answer",
parameters = listOf(QueryParam.int(42)),
)
println(result.rows[0].value("answer", PostgresDecoders.int)) // 42
} finally {
db.close()
}
}The SDK serializes work on one native PostgreSQL session. Keep the database in application state when multiple coroutines need it.
Keep data between launches
Use this storage configuration instead of the disposable example above. Direct mode stays bound to its first root for the lifetime of the process; closing does not let the same process switch roots.
Use an app-private File as the persistent root:
val db = Oliphaunt.open(
context = context.applicationContext,
config = OliphauntConfig(
storage = DatabaseStorage.Directory(context.filesDir.resolve("main.oliphaunt")),
),
)Directory accepts java.io.File, not a path string. Reopen the same directory to access saved data; the default TemporaryDirectory is disposable.
Continue with the Kotlin guide, mobile lifecycle guide, or API reference.