I'm trying to work with the SQLDelight library in KOtlin Multiplatform for Android and Desktop.Despite the documentation from cashApp or other sources, I can't quite get it right.
I have actually created the databases so far and also applied them in gradle as a plugin. What would I have to do now?
Or does anyone have a start guide/project for SQLDelight KOtlin Multiplatform, preferably directly for Dekstop and Web with the latest versions?
**build.gradle.kts**
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
plugins {
alias(libs.plugins.multiplatform)
alias(libs.plugins.compose)
alias(libs.plugins.android.application)
alias(libs.plugins.libres)
alias(libs.plugins.buildConfig)
alias(libs.plugins.sqlDelight)
}
kotlin {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = "17"
}
}
}
jvm()
sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.material3)
implementation(compose.materialIconsExtended)
implementation(libs.libres)
implementation(libs.voyager.navigator)
implementation(libs.composeImageLoader)
implementation(libs.napier)
implementation(libs.kotlinx.coroutines.core)
}
commonTest.dependencies {
implementation(kotlin("test"))
}
androidMain.dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.activityCompose)
implementation(libs.compose.uitooling)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.sqlDelight.driver.android)
}
jvmMain.dependencies {
implementation(compose.desktop.common)
implementation(compose.desktop.currentOs)
implementation(libs.sqlDelight.driver.sqlite)
}
}
}
android {
namespace = "org.company.app"
compileSdk = 34
defaultConfig {
minSdk = 24
targetSdk = 34
applicationId = "org.company.app.androidApp"
versionCode = 1
versionName = "1.0.0"
}
sourceSets["main"].apply {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
res.srcDirs("src/androidMain/resources")
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.4"
}
}
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "org.company.app.desktopApp"
packageVersion = "1.0.0"
}
}
}
libres {
// https://github.com/Skeptick/libres#setup
}
tasks.getByPath("jvmProcessResources").dependsOn("libresGenerateResources")
tasks.getByPath("jvmSourcesJar").dependsOn("libresGenerateResources")
buildConfig {
// BuildConfig configuration here.
// https://github.com/gmazzo/gradle-buildconfig-plugin#usage-in-kts
}
sqldelight {
databases {
create("AppDatabase") {
// Database configuration here.
// https://cashapp.github.io/sqldelight
packageName.set("org.company.app.db")
//generateAsync.set(true)
}
}
}
Check out this project, Here you will find SqlDelight project and it's setup https://github.com/KaushalVasava/KMP_TaskApp
buid.gradle.kts(shared)
plugins {
kotlin("multiplatform")
id("com.android.library")
id("org.jetbrains.compose")
id("com.squareup.sqldelight")
}
kotlin {
android {
compilations.all {
kotlinOptions {
jvmTarget = "17"
}
}
}
jvm("desktop") {
compilations.all {
kotlinOptions.jvmTarget = "17"
}
}
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
val commonMain by getting {
dependencies {
//put your multiplatform dependencies here
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
api("dev.icerock.moko:mvvm-core:0.16.1")
// only ViewModel, EventsDispatcher, Dispatchers.UI
api("dev.icerock.moko:mvvm-compose:0.16.1")
// api mvvm-core, getViewModel for Compose Multiplatfrom
}
}
val androidMain by getting {
dependencies {
api("androidx.activity:activity-compose:1.7.2")
api("androidx.appcompat:appcompat:1.6.1")
api("androidx.core:core-ktx:1.10.1")
implementation("com.squareup.sqldelight:android-driver:1.5.5")
}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation("com.squareup.sqldelight:native-driver:1.5.5")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val desktopMain by getting {
dependencies {
implementation(compose.desktop.common)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.7.3")
implementation("com.squareup.sqldelight:sqlite-driver:1.5.5")
}
}
}
}
sqldelight {
database("TaskDatabase") {
packageName = "task_database.db"
sourceFolders = listOf("kotlin")
}
}
android {
namespace = "com.kaushalvasava.apps.taskapp"
compileSdk = 33
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].res.srcDirs("src/androidMain/res")
sourceSets["main"].resources.srcDirs("src/commonMain/resources")
defaultConfig {
minSdk = 23
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlin {
jvmToolchain(17)
}
}
To integrate SQLDelight in a Kotlin Multiplatform project for Android and Desktop (using Compose for Desktop), you can follow these steps:
Apply SQLDelight Plugin: In your common build.gradle.kts, apply the SQLDelight plugin. Make sure you have the SQLDelight dependencies specified.
kotlinplugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("com.squareup.sqldelight")
}
sqldelight {
database("MyDatabase") {
packageName = "com.example.db"
sourceFolders = listOf("src/commonMain/sqldelight")
}
}
Specify SQLDelight Dependencies: Ensure that you have the necessary dependencies for SQLDelight in your commonMain source set.
kotlincommonMain.dependencies {
implementation(libs.sqldelight.runtime)
implementation(libs.sqldelight.coroutinesExtensions)
}
Make sure to replace libs.sqldelight.runtime
and libs.sqldelight.coroutinesExtensions
with the appropriate versions.
Generate SQLDelight Code: Run the SQLDelight Gradle task to generate the necessary database-related code.
bash./gradlew generateMyDatabase
Replace "MyDatabase" with the name you provided in the SQLDelight configuration.
Include Generated Code: Include the generated code in your commonMain source set.
kotlincommonMain.dependencies {
// other dependencies
implementation(files("${buildDir}/sqldelight/main/com/example/db/MyDatabase.kt"))
}
Platform-Specific Dependencies: Add platform-specific dependencies for Android and Desktop.
kotlinandroidMain.dependencies {
implementation(libs.sqlDelight.driver.android)
}
jvmMain.dependencies {
implementation(libs.sqlDelight.driver.desktop)
}
Ensure that you have the appropriate SQLDelight drivers for Android and Desktop.
Configure Database Connection:
In your code, configure the database connection. Use the generated MyDatabase
class.
// Example for Android
val driver = AndroidSqliteDriver(MyDatabase.Schema, context, "my_database.db")
// Example for Desktop
val driver = JdbcSqliteDriver("jdbc:sqlite:my_database.db")
Replace "my_database.db"
with the desired database name.
Use Database in Code: Use the generated classes from SQLDelight in your code to interact with the database.
kotlinval database = MyDatabase(driver)
// Example query
val result = database.myTableQueries.selectAll().executeAsList()
Make sure to replace placeholder names and versions with your actual project details. This setup should allow you to work with SQLDelight in a Kotlin Multiplatform project for Android and Desktop using Compose for Desktop.