3

I've cloned the compose multiplatform template (https://github.com/JetBrains/compose-multiplatform-template) and I've been trying to add sqldelight to the project following the instructions here: https://cashapp.github.io/sqldelight/2.0.0/multiplatform_sqlite/ given that my build.gradle.kts in shared directory now looks like this

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("org.jetbrains.compose")
    id("app.cash.sqldelight") version "2.0.0"
}

kotlin {
    androidTarget()

    jvm("desktop")

    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        version = "1.0.0"
        summary = "Project shared module"
        homepage = ""
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "shared"
            isStatic = true
        }
        extraSpecAttributes["resources"] = "['src/commonMain/resources/**', 'src/iosMain/resources/**']"
    }

    val sqlDelightVersion = "2.0.0"

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material)
                @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
                implementation(compose.components.resources)
                implementation("app.cash.sqldelight:runtime:$sqlDelightVersion")
            }
        }
        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("app.cash.sqldelight:android-driver:$sqlDelightVersion")
            }
        }
        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("app.cash.sqldelight:native-driver:$sqlDelightVersion")
            }
        }
        val desktopMain by getting {
            dependencies {
                implementation(compose.desktop.common)
                implementation("app.cash.sqldelight:native-driver:$sqlDelightVersion")
            }
        }
    }
}

android {
    compileSdk = (findProperty("android.compileSdk") as String).toInt()
    namespace = "com.project"

    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    sourceSets["main"].res.srcDirs("src/androidMain/res")
    sourceSets["main"].resources.srcDirs("src/commonMain/resources")

    defaultConfig {
        minSdk = (findProperty("android.minSdk") as String).toInt()
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    kotlin {
        jvmToolchain(11)
    }
}

sqldelight {
    databases {
        create("TestDatabase") {
            packageName.set("com.project.shared.db")
        }
    }
}

I've added sqldelight related configuration and in shared module I've added a Database.sq file in the directory src/commonMain/com/project/shared/db (I've even tried setting srcDirs explicitly to this directory) but I've been getting the following error while trying to build the project

e: D:\Users\mlovrekov\AndroidStudioProjects\GymBro\shared\build.gradle.kts:10:5: Unresolved reference: androidTarget
SQLDelight Gradle plugin was applied but there are no databases set up.

The Kotlin source set commonTest was configured but not added to any Kotlin compilation. You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.
See https://kotl.in/connecting-source-sets

All of a sudden androidTarget is no longer a valid function? And what about SQLDelight Gradle plugin was applied but there are no databases set up.? I have done all the required steps so it should start building the database.

This error only shows up once I add id("app.cash.sqldelight") version "2.0.0" to plugins. If I remove it, the project builds but without a database

Can anyone tell me what I'm doing wrong here?

7
  • shouldn't it be android instead of androidTarget? I'm also not sure about native driver for desktop, since it runs on jvm, and native driver doesn't support jvm Commented Aug 1, 2023 at 14:20
  • @PhilDukhov if I try using android it is crossed over and set as deprecated with the following link: kotlinlang.org/docs/… As for the driver, I wasn't sure what is the proper one for desktop. I will change it Commented Aug 1, 2023 at 14:29
  • Have you solved it? I'm stuck with the same error. Commented Aug 4, 2023 at 20:21
  • 1
    @mhenryk not quite. I switched to com.squareup.sqldelight and the issue was gone. Seems like cashapp sqldelight plugin doesn't know about androidTarget Commented Aug 5, 2023 at 7:50
  • what do you mean you switched to com.squareup.sqldelight ? Did you stopped using the plugin? Commented Sep 1, 2023 at 15:41

1 Answer 1

1

can be resolved by doing

kotlin {
  @Suppress("OPT_IN_USAGE")
  targetHierarchy.default()
  androidTarget()
  ...

}

answer is referenced from this kmp project

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.