惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
B
Blog
博客园 - Franky
I
InfoQ
A
About on SuperTechFans
博客园_首页
L
LangChain Blog
量子位
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
美团技术团队
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
G
Google Developers Blog
Last Week in AI
Last Week in AI

Inside Nutrient

A guide to the invisible work behind documents Introducing Nutrient Documents for Salesforce: Native document generation and signing Document AI vs. traditional OCR: Choosing between OCR, AI, and hybrid pipelines PDF SDK compliance and security evaluation checklist for enterprise teams (2026) Invariant Corp replaces paper processes with Nutrient Workflow and scales without limits What is process mapping? A complete guide Nutrient vs. Conga Composer for Salesforce document generation (2026) Document routing: How to automate document distribution The CTO’s AI playbook: Why accountability architecture beats orchestration Compliance workflow automation: Why built-in compliance is table stakes Workflow diagrams: Examples, symbols, and how to build one that actually runs Digital forms: Replace paper forms with automated workflows Approval workflow software: How to automate approvals Why document-centric automation is different The CEO’s AI playbook: Why decision architecture beats model selection Nutrient SDK product updates for Q1 2026 PDF redaction verification: How to prove sensitive data is permanently removed What is a VPAT? The complete guide to accessibility conformance reports What is PDF/UA? The accessible PDF standard explained Salesforce eSignatures: Generate, sign, and track documents in one flow Online document viewer: Options, tradeoffs, and how to embed one Document viewer for web apps: React, Vue, Angular (2026) Best document viewers in 2026: A buyer’s guide How to edit a PDF in Python: Add text, images, and annotations Nutrient advances Workflow platform with agentic AI for enterprise-grade speed and consistency in document-heavy operations How to create a Salesforce quote template from opportunity data The business case for accessibility: Five ways it drives enterprise value Python PDF library comparison (2026): 7 libraries for developers Why your AI agent hallucinates PDF table data PDF.js limitations: When to upgrade to a commercial PDF SDK
Room database for Kotlin Multiplatform projects
Akshay Sharma · 2024-07-10 · via Inside Nutrient

Table of contents

    Room database for Kotlin Multiplatform projects

    Room(opens in a new tab) is a database library that uses SQLite(opens in a new tab) under the hood. In the past, it worked with Android only, but since adding support for all platforms, it works with Kotlin Multiplatform(opens in a new tab) (KMP) to store data in local databases on any platform supported by KMP.

    This blog post will explore the process of integrating the Room database library into your Kotlin Multiplatform projects with ease and efficiency.

    Setting up the Kotlin Multiplatform project

    Navigate to the official Kotlin Multiplatform Wizard(opens in a new tab) and configure your platforms as per your needs.

    For this demo, select the checkboxes for Android, iOS, and Desktop.

    After selecting the platforms, select DOWNLOAD to download the ZIP file and extract the files.

    You can open this project in either Android Studio(opens in a new tab) or IntelliJ IDEA(opens in a new tab)

    Adding dependencies

    Room version 2.7.0-alpha01 and higher includes support for Kotlin Multiplatform. This post uses Kotlin version 2.0.0 with Android Gradle plugin 8.2.2.

    To add Room to your multiplatform project, include the dependencies below in libs.version.toml:

    [versions]

    room = "2.7.0-alpha03"

    ksp = "2.0.0-1.0.21"

    sqlite = "2.5.0-SNAPSHOT"

    [libraries]

    room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }

    room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }

    sqlite-bundled = { module= "androidx.sqlite:sqlite-bundled", version.ref = "sqlite" }

    [plugins]

    ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }

    room = { id = "androidx.room", version.ref = "room" }

    Now, you’ll add these to your build.gradle.kts file.

    Append the plugins below in the plugins block and the dependencies below in the commonMain.dependencies block:

    plugins {

    alias(libs.plugins.ksp)

    alias(libs.plugins.room)

    ...

    commonMain.dependencies {

    implementation(libs.room.runtime)

    implementation(libs.sqlite.bundled)

    ...

    Add the Room database schema path, along with KSP support for the Room compiler, at the end of the build.gradle.kts file:

    room {

    schemaDirectory("$projectDir/schemas")

    }

    dependencies {

    // KSP support for Room Compiler.

    kspCommonMainMetadata(libs.room.compiler)

    }

    // Solves implicit dependency issue and IDEs source code detection.

    kotlin.sourceSets.commonMain { tasks.withType<KspTaskMetadata> { kotlin.srcDir(destinationDirectory) } }

    You’ve now added all the required dependencies for Room to your KMP project.

    Adding database classes

    Next, create the Database class with DAOs and Entities inside the CommonMain package:

    @Database(entities = [FruitEntity::class], version = 1)

    abstract class AppDatabase : RoomDatabase() {

    abstract fun getFruitDao(): FruitDao

    }

    @Dao

    interface FruitDao {

    @Upsert // Insert and update entities.

    suspend fun upsert(fruit: FruitEntity)

    @Delete

    suspend fun delete(fruit: FruitEntity)

    @Query("SELECT * FROM FruitEntity")

    fun getAllAsFlow(): Flow<List<FruitEntity>>

    }

    @Entity

    data class FruitEntity(

    @PrimaryKey(autoGenerate = true) val id: Long = 0L,

    val name: String,

    val description: String,

    val count: Int = 0

    )

    Next, add platform-specific code to create your database builder.

    Android

    fun getDatabaseBuilder(context: Context) = with(context) {

    val dbFile = applicationContext.getDatabasePath("room_android.db")

    Room.databaseBuilder<AppDatabase>(

    context = applicationContext,

    name = dbFile.absolutePath

    )

    }

    iOS

    fun getDatabaseBuilder() = Room.databaseBuilder<AppDatabase>(

    name = NSHomeDirectory() + "/room_ios.db",

    factory = { AppDatabase::class.instantiateImpl() }

    )

    Desktop

    fun getDatabaseBuilder() = Room.databaseBuilder<AppDatabase>(

    name = File(System.getProperty("java.io.tmpdir"), "room_desktop.db").absolutePath

    )

    Here’s the database builder you use to initialize your AppDatabase:

    fun getRoomDatabase(builder: RoomDatabase.Builder<AppDatabase>): AppDatabase {

    return builder

    .addMigrations(MIGRATIONS)

    .fallbackToDestructiveMigrationOnDowngrade()

    .setDriver(BundledSQLiteDriver())

    .setQueryCoroutineContext(Dispatchers.IO)

    .build()

    }

    At last, the Room database setup is complete! Now, with the getRoomDatabase method, you can easily retrieve the database instance.

    Conclusion

    This post demonstrated how only a few lines of code can grant the power of databases in multiple platforms in a single codebase. With the instance of AppDatabase, it’s possible to perform all database transactions — like searching, inserting, and deleting data — regardless of which platform you’re on. Magic!

    For a working example project of Room database implementation in KMP, refer to the following GitHub repository(opens in a new tab).

    FAQ

    Room simplifies database management with a clean API and provides compile-time checks for SQL queries, enhancing code reliability across platforms.

    Room manages migrations through defined migration paths, allowing developers to upgrade the database schema seamlessly without losing existing data.

    While Room is primarily designed for local database storage using SQLite, it can be integrated with other libraries to facilitate remote data synchronization.

    Kotlin Multiplatform enables code sharing across different platforms (like Android and iOS), reducing development time and maintaining consistency in codebases.

    Yes, Room is efficient for managing large datasets, but performance can depend on how well the database is structured and the complexity of the queries.

    Explore related topics

    Try for free Ready to get started?

    Related SDK articles

    Explore more