cashapp / sqldelight

SQLDelight - Generates typesafe Kotlin APIs from SQL
https://cashapp.github.io/sqldelight/
Apache License 2.0
6.12k stars 512 forks source link

Error while importing the generated database #1894

Closed GianfrancoMS closed 4 years ago

GianfrancoMS commented 4 years ago

Build Environment SQLDelight version: 1.4.1 OS: macOS 10.15.6 Gradle version: 6.6 Kotlin version: 1.4.0 IDE: Android Studio 4.0.1 and IntelliJ IDEA 2020.2 AGP Version (if applicable): 4.0.1

Describe the bug After migrating and building my project, I can not import the generated database from the commonMain nor androidMain source sets. However, I can import it from the iosMain source set.

When I try to import the database from the commonMain source set, the IDE says "Add dependency on module 'common_iosMain'". Similarly, on the androidMain source set, it says "Add dependency on on module 'common_iosMain'" and "Import", but the option "Import" does not work.

Common target

Android target

Additionally, after building, the IDE gives a warning:

Duplicate content roots detected: Path [/Users/Gianfranco/Documents/UPC-Mobile/common/build/generated/sqldelight/code/UpcDatabase] of module [common_iosMain] was removed from modules [common_commonMain]

build.gradle

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

android {
    compileSdkVersion(App.CompileSdk)
    buildToolsVersion(App.BuildTools)

    defaultConfig {
        minSdkVersion(App.MinSdk)
        targetSdkVersion(App.TargetSdk)
        versionCode = App.VersionCode
        versionName = App.VersionName
    }

    sourceSets {
        getByName("main") {
            manifest.srcFile(File("src/androidMain/AndroidManifest.xml"))
            java.srcDirs("src/androidMain/kotlin")
            res.srcDirs("src/androidMain/res")
        }

        getByName("test") {
            java.srcDirs("src/androidTest/kotlin")
            res.srcDirs("src/androidTest/res")
        }
    }
}

sqldelight {
    database("UpcDatabase") {
        packageName = "com.upc.common.sqldelight"
    }
}

version = "1.0"

kotlin {
    android()

    val onPhone = System.getenv("SDK_NAME")?.startsWith("iphoneos") ?: false
    if (onPhone) {
        iosArm64("ios")
    } else {
        iosX64("ios")
    }

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.Coroutines}")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.Serialization}")
        implementation("io.ktor:ktor-client-core:${Versions.Ktor}")
        implementation("com.squareup.sqldelight:coroutines-extensions:${Versions.SqlDelight}")
        implementation("com.russhwolf:multiplatform-settings:${Versions.Settings}")
    }

    sourceSets["androidMain"].dependencies {
        implementation("io.ktor:ktor-client-android:${Versions.Ktor}")
        implementation("com.squareup.sqldelight:android-driver:${Versions.SqlDelight}")
    }

    sourceSets["iosMain"].dependencies {
        implementation("io.ktor:ktor-client-ios:${Versions.Ktor}")
        implementation("com.squareup.sqldelight:native-driver:${Versions.SqlDelight}")
    }

    sourceSets["commonTest"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-test-common:${Versions.Kotlin}")
        implementation("org.jetbrains.kotlin:kotlin-test-annotations-common:${Versions.Kotlin}")
    }

    sourceSets["androidTest"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-test:${Versions.Kotlin}")
        implementation("org.jetbrains.kotlin:kotlin-test-junit:${Versions.Kotlin}")
        implementation("com.squareup.sqldelight:sqlite-driver:${Versions.SqlDelight}")
    }

    targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
        binaries.withType<org.jetbrains.kotlin.gradle.plugin.mpp.Framework> {
            isStatic = false
        }
    }

    cocoapods {
        summary = "UPC-Mobile with Kotlin/Native"
        homepage = "https://github.com/GianfrancoMS/UPC-Mobile"
        podfile = project.file("../iosApp/Podfile")
    }
}

tasks.getByName("podspec").enabled = false

gradle.properties

org.gradle.jvmargs=-Xmx1536m
org.gradle.caching=true
android.useAndroidX=true
android.useMinimalKeepRules=true
kotlin.incremental.usePreciseJavaTracking=true
kotlin.code.style=official
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false
xcodeproj=./iosApp
GianfrancoMS commented 4 years ago

Probably related to #1880

GianfrancoMS commented 4 years ago

From kotlinlang.org:

Libraries published with the hierarchical project structure are compatible with all kinds of projects, both with and without the hierarchical project structure. However, libraries published without the hierarchical project structure can’t be used in a shared native source set. So, for example, users with ios() shortcuts in their gradle.build files won’t be able to use your library in their iOS-shared code.

PaulWoitaschek commented 4 years ago

Same here. When I set enableGranularSourceSetsMetadata to false, it however compiles fine but shows warnings in the IDE that the database is not resolved.

softartdev commented 4 years ago

Same problem. I had to temporarily downgrade Kotlin to 1.3.72 and SQLDelight to 1.4.0 and wait for library fixes.

AlecKazakova commented 4 years ago

please try latest snapshot release to see if it fixes

saket commented 4 years ago

I can check

saket commented 4 years ago

I can confirm that I'm no longer seeing the issue in my project. 🎉

benjohnde commented 4 years ago

That's quite some issue for us as well (also cf. #1906). :(

Any change we can see this thingy released without the need to wait for the current milestone 1.4.2 to be finished? 🚀

saket commented 4 years ago

@benjohnde you can use 1.5.0-SNAPSHOT in the meantime.

benjohnde commented 4 years ago

Oweeee that's so perfect :) Thanks @saket ❤️

mathemandy commented 4 years ago

@AlecStrong @saket how do I access 1.5.0-SNAPSHOT, I am currently having the same issue

sigurdurrafn commented 4 years ago

It looks like 1.4.2 has been released, which includes the fix. So no need for snapshot and the snapshot repo

PaulWoitaschek commented 3 years ago

I'm still having this with 1.4.4.

With:

kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false

I have a database' module where the sqldelight database gets generated and it gets consumed by another module. After the very first build, the database fails to resolve: ./gradlew database:clean && ./gradlew consumer:buildthe database does not exist. When I run another./gradlew consumer:build`, the database gets is there.

When I set enableGranularSourceSetsMetadata to false, it works on the first build.