4

I tried to integrate sqldelight in my Multiplatform library project for Android/iOS but I'm having several unresolved dependency errors when syncing gradle.

ERROR: Unable to resolve dependency for ':SharedCode@debug/compileClasspath': Could not resolve com.squareup.sqldelight:runtime:1.1.3.

ERROR: Unable to resolve dependency for ':SharedCode@debug/compileClasspath': Could not resolve com.squareup.sqldelight:runtime-jvm:1.1.3.

ERROR: Unable to resolve dependency for ':SharedCode@release/compileClasspath': Could not resolve com.squareup.sqldelight:runtime:1.1.3. ERROR: Unable to resolve dependency for ':SharedCode@release/compileClasspath': Could not resolve com.squareup.sqldelight:runtime-jvm:1.1.3.**

  • Gradle version 5.1.1

  • Gradle Plugin 3.4.0

  • sqldelight 1.1.3

enableFeaturePreview('GRADLE_METADATA') is present in my settings.gradle

My project gradle file looks like this:

buildscript {
    ext {
        kotlin_version = '1.3.31'
        ktor_version = '1.2.1'
        ktor_json_version = '1.2.1'
        kotlinx_coroutines_version = '1.2.1'
        serialization_version = '0.11.0'
        sqldelight_version = '1.1.3'
        dokka_version = '0.9.16'
    }

    repositories {
        google()
        jcenter()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
        classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:$dokka_version"
        classpath "com.squareup.sqldelight:gradle-plugin:$sqldelight_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url "https://kotlin.bintray.com/kotlinx" }
    }
}

task clean(type: Delete) {
    setDelete rootProject.buildDir
}

my SharedCode lib gradle file:

apply plugin: 'kotlinx-serialization'
apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'com.squareup.sqldelight'

group = 'com.example.multiplatform'
version = '1.0'

android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 15
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    // Specify Kotlin/JVM stdlib dependency.
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7'
    implementation "com.squareup.sqldelight:runtime:1.1.3"

    testImplementation 'junit:junit:4.12'
    testImplementation 'org.jetbrains.kotlin:kotlin-test'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'org.jetbrains.kotlin:kotlin-test'
    androidTestImplementation 'org.jetbrains.kotlin:kotlin-test-junit'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

kotlin {
    targets {
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos")  \
                           ? presets.iosArm64 : presets.iosX64

        fromPreset(iOSTarget, 'ios') {
            binaries {
                framework('SharedCode')
            }
        }

        fromPreset(presets.android, 'androidLib')
    }

    sourceSets {
        commonMain {
            dependencies {
                //HTTP
                implementation "io.ktor:ktor-client-json:$ktor_json_version"
                implementation "io.ktor:ktor-client-serialization:$ktor_version"
                //Coroutines
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlinx_coroutines_version"
                //Kotlinx serialization
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
                //sqldelight
                implementation "com.squareup.sqldelight:runtime:$sqldelight_version"
            }
        }

        androidLibMain {
            dependencies {
                //HTTP
                implementation "io.ktor:ktor-client-json-jvm:$ktor_json_version"
                implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
                //Coroutines
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinx_coroutines_version"
                //Kotlinx serialization
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
                //sqldelight
                implementation "com.squareup.sqldelight:android-driver:$sqldelight_version"
            }
        }

        iosMain {
            dependencies {
                //HTTP
                implementation "io.ktor:ktor-client-ios:$ktor_version"
                implementation "io.ktor:ktor-client-json-native:$ktor_json_version"
                implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
                //Coroutines
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$kotlinx_coroutines_version"
                //kotlinx serialization
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
                //sqldelight
                implementation "com.squareup.sqldelight:ios-driver:$sqldelight_version"
            }
        }
    }
}

sqldelight {
    MyApp {
        packageName = 'com.example.multiplatform'
    }
}

configurations {
    compileClasspath
}

task packForXCode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
    final def framework = kotlin.targets.ios.binaries.getFramework("SharedCode", mode)

    inputs.property "mode", mode
    dependsOn framework.linkTask

    from { framework.outputFile.parentFile }
    into frameworkDir

    doLast {
        new File(frameworkDir, 'gradlew').with {
            text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
            setExecutable(true)
        }
    }
}
tasks.build.dependsOn packForXCode
shkschneider
  • 17,833
  • 13
  • 59
  • 112
Alan
  • 1,264
  • 1
  • 11
  • 21

6 Answers6

3

I was solved this problem. First: Apply sqldelight plugin right to all project:

apply plugin: 'com.squareup.sqldelight'
sqldelight {
    MyDatabase {
        packageName = "ru.trendagent.database"
        sourceFolders = ["sqldelight"]
    }
}

In android section add:

packagingOptions {
        ...
        exclude 'META-INF/*.kotlin_module'
    }

remove all implementations of sqldelight from CommonMain section add implementation to AndroidMain section:

implementation "com.squareup.sqldelight:android-driver:1.1.3" //DataBase

add implementation to iosMain section if need:

implementation "com.squareup.sqldelight:ios-driver:1.1.3"//DataBase

add Metadata settings to gradle to settings.gradle file:

enableFeaturePreview("GRADLE_METADATA") // IMPORTANT!

Fully clean gradle files, reset gradle wrapper and other. Don't use kotlin 1.4.0. It unsupport sqldelight 1.1.3 version Change gradle to latest version. You can download latest gradle manually and set it on IDE. You need gradle 5.3.1 or latest (5.1.1 unsupported)

If error will remain - change IDE to intellij idea comunity edition

SlyBeaver
  • 1,272
  • 12
  • 22
2

This is seems to me as an error occurred due to offline gradle or invalid cache as this error is not specific to com.squareup.sqldelight. To resolve this issue follow the following steps:

  • File
  • Settings
  • Build, Execution, Deployment
  • Gradle
  • Uncheck offline work

and then try to invalidate the cache and restart from file menu. Hope it helps!

Vanshaj Daga
  • 2,145
  • 11
  • 18
1

Change this

 classpath "com.squareup.sqldelight:gradle-plugin:$sqldelight_version"

Following

 classpath "com.squareup.sqldelight:runtime:$sqldelight_version"

you are passing wrong metadata to sqldelight change with my solution it will work

Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
  • Replacing the line, I have a new error `Plugin with id 'com.squareup.sqldelight' not found.` And with the two classpath, the issue remains – Alan Jun 13 '19 at 13:57
  • 1
    you are adding this in wrong gradle file you have to add this in your app folder gradle file with implementation "com.squareup.sqldelight:runtime:$sqldelight_version" – Milan Pansuriya Jun 13 '19 at 16:30
0

The Gradle part of the readme of the library indicates, that gradle plugin is needed. There's no such sqldelight library as runtime. They only have android-driver, ios-driver and sqlite-driver.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0

Using the DroidConKotlin app project as an example, I upgraded gradle from 5.1.1 to 5.3.1 and it resolved all the dependencies issues.

Alan
  • 1,264
  • 1
  • 11
  • 21
  • did you add `enableFeaturePreview('GRADLE_METADATA')` in your settings.gradle ? As advised in the readme of the sqldelight github, I now only import the ios-driver, the android-driver libs and apply the sqldelight plugin. No runtime, runtime-jvm or runtime-metadata lib. – Alan Jun 24 '19 at 15:31
0

You likely are trying to use a dependency on dependant libraries or even applications and not only in commonMain module. Just change:

commonMain {
    dependencies {
        ...
        implementation "com.squareup.sqldelight:runtime:$sqldelight_version"
    }
}

to

commonMain {
    dependencies {
        ...
        api "com.squareup.sqldelight:runtime:$sqldelight_version"
    }
}

Also change it on dependencies section

And do the same with any other dependency uncorrecly resolved

This can happen for example if you define a class that implements an interface provided by the dependency and you use or extend that class on dependant libraries or applications (Just an example of millions that could be).

If this is your case, for further understanding, just take a look to this answer:

Rubén Viguera
  • 3,277
  • 1
  • 17
  • 31