kotlingradlekotlin-multiplatformgradle-kotlin-dslktor

Gradle Dependency Usage Explanation: Kotlin multiplatform


today I tried to dive a little deeper into Gradle and dependencies (Kotlin).

However, I am now a bit confused about dependencies. I wanted to include Ktor client in a project for practice purposes. So I searched for ktor under google > mavenrepository. https://mvnrepository.com/search?q=ktor&p=1

Based on the results, it seems obvious that I need to include a ktor-client-core in my build.gradle.kts. But now my question arises. Which of the ktor-client-core do I have to include, there are numerous versions of it. ktor-client-core, ktor-client-core-js, ktor-client-core-jvm, ktor-client-core-ioarm64, ... It seems that there is a general version: ktor-client-core and platform specific versions: ktor-client-core-jvm.

  1. does ktor-client-core always have to be included?
  2. when must a platform-specific version be explicitly included (in Intellij under Linux, ktor-client-core had all been done)?
  3. does the platform-specific version replace ktor-client-core?
  4. why is there an iOS, Linux, Windows version if there is a JVM version that must be installed by default on desktop systems to run Java?

Solution

  • Ultimately, you only need the platform specific dependency for the platform you are targeting at that point in time.

    However, the use of a common library like ktor-client-core is very useful if you are writing a Kotlin multiplatform application. It contains:

    So, my Kotlin multiplatform Gradle setup can look something like this:

    plugins {
        kotlin("multiplatform")
    }
    
    kotlin {
        js()
        jvm()
    
        sourceSets {
            commonMain {
                dependencies {
                    implementation("io.ktor:ktor-client-core:2.3.11")
                }
            }
        }
    }
    

    and then Gradle will add the JS version to the JS build and the JVM version to the JVM build.

    You can also add the common dependency to a platform specific dependency configuration and Gradle will be able to find the correct dependency.

    So to cover the specific questions:

    1. does ktor-client-core always have to be included?

    No, but it is easier to use it if you are using Gradle and Kotlin multiplatform. If you are building without Gradle it is almost certainly easier to use the platform dependencies.

    1. when must a platform-specific version be explicitly included (in Intellij under Linux, ktor-client-core had all been done)?

    When you are building the application without a tool that can use the Gradle metadata to access the required platform dependency.

    1. does the platform-specific version replace ktor-client-core?

    Yes, for a given platform.

    1. why is there an iOS, Linux, Windows version if there is a JVM version that must be installed by default on desktop systems to run Java?

    Kotlin, via Kotlin Native, can target these platforms without a JVM. Or, you can run your program on a JVM on those platforms. The choice is yours.