android-jetpack-composekotlin-multiplatform

Kotlin multiplatform compose template error after adding sqldelight dependency


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?


Solution

  • can be resolved by doing

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

    answer is referenced from this kmp project