I have tried to use KTor for a KMM (Kotlin Multipl...
# multiplatform
k
I have tried to use KTor for a KMM (Kotlin Multiplatform Mobile) project but the shared module (commonMain) doesn't see the ktor library after I've added it as a dependency ... Anyone had success with using KMM with KTor in the shared module or is it not ready yet?
m
Ktor client works just fine. What dependency did you add and how?
j
there are some good example projects out there, maybe compare your dependency setup with e.g. https://github.com/joreilly/PeopleInSpace
k
My bad, I should have provided more details. I have followed this guide: https://ktor.io/docs/http-client-multiplatform.html My shared gradle build file looks like that:
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

val ktorVersion = "1.5.0"

plugins {
    kotlin("multiplatform")
    id("com.android.library")
}

kotlin {
    android()
    ios {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-core:$ktorVersion")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.google.android.material:material:1.2.1")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13")
            }
        }
        val iosMain by getting
        val iosTest by getting
    }
}

android {
    compileSdkVersion(30)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(30)
    }
    buildToolsVersion = "30.0.3"
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework =
        kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}

tasks.getByName("build").dependsOn(packForXcode)
My expectation was that adding:
Copy code
val commonMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-core:$ktorVersion")
            }
        }
will allow me to import io.ktor.etc in src/commonMain/kotlin/… files. But no such luck
c
What gradle version do you use? Looks like it doesn't pick up gradle metadata for some reason
k
Thanks for looking into it. The gradle version is Gradle 6.8-rc-4