0

I am trying to use SQLDelight in a Kotlin Multiplatform project for android and iOS. I have followed the documentation found here. The project builds successfully, but I can't access com.squareup.* anywhere inside the shared/iosMain folder. Below are my code files.

Project/build.gradle.kts:

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
        classpath("com.android.tools.build:gradle:7.0.3")
        classpath("com.squareup.sqldelight:gradle-plugin:1.5.2")

    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

Project/shared/build.gradle.kts:

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("com.squareup.sqldelight")

}

version = "1.0"

kotlin {
    android()

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget = when {
        System.getenv("SDK_NAME")?.startsWith("iphoneos") == true -> ::iosArm64
        System.getenv("NATIVE_ARCH")?.startsWith("arm") == true -> ::iosSimulatorArm64
        else -> ::iosX64
    }

    iosTarget("ios") {}

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "14.1"
        frameworkName = "shared"
        podfile = project.file("../iosApp/Podfile")
    }
    
    sourceSets {
        val commonMain by getting {
            dependencies {
                // SQLDelight
                implementation("com.squareup.sqldelight:runtime:1.5.2")
                implementation ("com.squareup.sqldelight:coroutines-extensions:1.5.0")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.squareup.sqldelight:android-driver:1.5.0")

            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        val iosMain by getting {
            dependencies {
                implementation("com.squareup.sqldelight:native-driver:1.5.2")
            }
        }
        val iosTest by getting
    }
}

sqldelight {
    database("AppDatabase") {
        packageName = "com.example.sam.data.db"
        dialect = "sqlite:3.25"
    }
}

android {
    compileSdkVersion(31)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(21)
        targetSdkVersion(31)
    }
}

Project/shared/src/commonMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt

package com.example.sam.data.db

import com.squareup.sqldelight.db.SqlDriver

expect class DatabaseDriverFactory {
    fun createDriver(): SqlDriver
}

Project/shared/src/androidMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt

package com.example.sam.data.db

import android.content.Context
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver

actual class DatabaseDriverFactory(
    private val ctx: Context
) {

    actual fun createDriver(): SqlDriver {
        return AndroidSqliteDriver(AppDatabase.Schema, ctx, "app.db")
    }

}

And the file that I am having issues with - Project/shared/iosMain/kotlin/com/example/sam/data/db/DatabaseDriverFactory.kt

package com.example.sam.data.db

import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver


actual class DatabaseDriverFactory {

    actual fun createDriver(): SqlDriver {
        return NativeSqliteDriver(AppDatabase.Schema, "app.db")
    }

}

It can't find SqlDriver, NativeSqliteDriver or even com.squareup.*.

I have scoured the web, but didn't find anything that helped.

Olu Udeh
  • 1,031
  • 1
  • 10
  • 20
  • The first question is if it's just an IDE issue or if the build also doesn't work. Next, what version of Kotlin are you on? Are you using `enableDependencyPropagation`? Assuming the gradle build works but the IDE isn't recognizing the dependency, you generally just need to use the latest stable versions of Kotlin, libraries, and IDE. – Kevin Galligan Nov 06 '21 at 11:39
  • Hi @KevinGalligan, I don't know if it is an IDE issue, but I am using the latest version of android studio; also the project builds successfully, but doesn't run. I am not using enableDependencyPropagation and I am using the latest stable version of kotlin. Thank you. – Olu Udeh Nov 06 '21 at 21:30
  • I don't know what you mean by it builds but doesn't run. We should separate Android Studio issues from Kotlin issues, as studio config is still brittle (the Kotlin team and Android teams aren't always in perfect sync). I'd open the project in Xcode and attempt to actually run it from there, not Android Studio. – Kevin Galligan Nov 07 '21 at 15:45
  • What happens if you run `./gradlew :shared:clean :shared:build` in the project's root directory? Does it compile? If yes it's an IDE issue, if not, well then it's something else. From my point of view the code you posted looks correct. – Emanuel Moecklin Nov 09 '21 at 19:36

1 Answers1

0

Comments won't let me post code, but I'd like to try some things.

Try this config and see if the IDE can understand it better. You'll still need to add back some form of the more complex one, but I'd like to reduce the variables as much as possible.

kotlin {
    android()

    iosX64("ios")

    cocoapods {
    //etc...
Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62
  • Hi @KevinGalligan the issue I am having is same as in [this question](https://stackoverflow.com/questions/62916210/unresolved-reference-nativesqlitedriver-for-sqldelight-added-to-ios-source-se), but your answer there didn't work for me. – Olu Udeh Jan 15 '22 at 13:29
  • OK. You'll need a lot more details. I don't have any other guesses. – Kevin Galligan Jan 16 '22 at 19:20
  • Please, let me know what other details will be helpful, I have posted my gradle file and the sample codes in the question. I am really stuck. – Olu Udeh Jan 16 '22 at 19:53