Search code examples
androidkotlin-multiplatform

Cannot access class 'io.ktor.client.HttpClient'. Check your module classpath for missing or conflicting dependencies


I get this error if I try to access a class with a HttpClient instance in commonMain in a KMM project.

Tried it on: Android Studio Flamingo | 2022.2.1 Patch 2 Build #AI-222.4459.24.2221.10121639, built on May 12, 2023 Runtime version: 17.0.6+0-17.0.6b802.4-9586694 aarch64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 13.4.1 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 10 Metal Rendering is ON Registry: external.system.auto.import.disabled=true ide.text.editor.with.preview.show.floating.toolbar=false ide.instant.shutdown=false gradle.version.catalogs.dynamic.support=true

Non-Bundled Plugins: com.jetbrains.kmm (0.6.1(222)-14)

and

Android Studio Giraffe | 2022.3.1 RC 1 Build #AI-223.8836.35.2231.10320515, built on June 14, 2023 Runtime version: 17.0.6+0-17.0.6b829.9-10027231 aarch64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 13.4.1 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 10 Metal Rendering is ON Registry: external.system.auto.import.disabled=true ide.text.editor.with.preview.show.floating.toolbar=false ide.instant.shutdown=false

Non-Bundled Plugins: com.jetbrains.kmm (0.6.1(223)-18)

These are the dependencies:

val coroutinesVersion = "1.7.2"
val ktorVersion = "2.3.2"

sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("io.ktor:ktor-client-core:$ktorVersion")
            implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
            implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
        }
    }
    val androidMain by getting {
        dependencies {
            implementation("io.ktor:ktor-client-android:$ktorVersion")
        }
    }
    val iosMain by getting {
        dependencies {
            implementation("io.ktor:ktor-client-darwin:$ktorVersion")
        }
    }
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test"))
        }
    }
}

Solution

  • The problem turned out to be a constructor like this:

    class SomeService(private val client: HttpClient = createDefaultService()) { ...
    

    It can be called without parameter from a module which does not import ktor but the compiler does not like this at all.