Help - cannot use Ktor client

Hi All,
I am very new to Kotlin Native and I’m trying to build a very simple HTTP client as a PoC. The idea is that I create a native executable with the same source that can be run on the Mac and Linux, and maybe in the future JVM / Node or other platforms.

I was able to make it work as a simple “hello world” and built targets successfully.

Then I tried adding Ktor as an HTTP client and there the fun began.

import io.ktor.client.HttpClient

fun main() {
    val c = HttpClient()
    println( "HTTP client created" )
}

Idea marks the import as correct (no squiggly red line), but when I build I get;

.../Main.kt: (3, 8): Unresolved reference: io
.../Main.kt: (11, 13): Unresolved reference: HttpClient

Like the library does not exist.

I guess my issues come from build.gradle, where the “kotlin” stanza is:

kotlin {

    macosX64("mac") {
      binaries {
            executable {
                entryPoint = 'knativedemo.main'
                runTask?.args('')
            }
        }
    }
    
    linuxX64("linux") {
      binaries {
            executable {
                entryPoint = 'knativedemo.main'
                runTask?.args('')
            }
        }
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation "io.ktor:ktor-client-core:$ktor_version"
            }
        }

        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }

        macMain {
            dependencies {
                implementation "io.ktor:ktor-client-core-macosx64:$ktor_version"
                implementation "io.ktor:ktor-client-curl:$ktor_version"
            }
        }

        linuxMain {
            dependencies {
                implementation "io.ktor:ktor-client-core-linuxx64:$ktor_version"
                implementation "io.ktor:ktor-client-curl:$ktor_version"
            }
        }
    }

But what is wrong with it?
Any help appreciated.

Full project sources at: GitHub - l3nz/kotlin-native-example: A small example of a Kotlin Native project

Hello, @l3nz!
Thank you for a very detailed issue. It has helped a lot when I started to work on it. There are several things that you missed in this project:

  1. One got to enable Gradle metadata to add dependencies on Kotlin/Native libraries(this is also mentioned here).
  2. The entry point that you declare in the build.gradle is unreachable, as far as the package name is not knativedemo but l3nz. You should choose just one of them.

Please, try to change the project like that, and let me know if it won’t help.

You might try importing the ktor-client-apache like so:

implementation(“io.ktor:ktor-client-apache:$ktor_version”)

And then in the code: val client = HttpClient(Apache)