Cannot import dependecies in kotlin multiplatform mobile

Viewed 813

I created a new kotlin multiplatform mobile project. I followed official documentation. Basic project is working, I was able to build it on android without problems.

I wanted to add some api and I found ktor, which I have never used before. I followed docs here: https://kotlinlang.org/docs/mobile/use-ktor-for-networking.html and tutorial here: https://proandroiddev.com/kotlin-multiplatform-very-beginners-guide-part-2-api-d54f7326dc57 and all changes I did are:

I added ktor libraries into build.gradle.kts(:shared):

sourceSets {
        val commonMain by getting {
            dependencies {
                implementation ("io.ktor:ktor-client-core:1.5.0")
            }
        }
        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")
                implementation("io.ktor:ktor-client-android:1.5.0")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13")
            }
        }
        val iosMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:1.5.0")
            }
        }
        val iosTest by getting
    }

And I created Api class where I wanted to create and use the HttpClient:

class Api() {
    private val client = HttpClient()

    suspend fun fetch(): String {
        return ""
    }
}

BUT HttpCLient() is "Unresolved reference" and it cannot be imported. I also tried to manually add import io.ktor.client.HttpClient

but io is "Unresolved reference". Also I tried numerous rebuilds/cleans/syncs. What am I doing wrong? Am I missing something?

1 Answers

I encountered the same problem on windows 7 with Android Studio 4.1.3. It was solved when in the build script of the project, in the dependencies section of the buildscript, it was updated the version of the kotlin-gradle-plugin artifact from 1.4.10 to 1.4.32. Below is a copy paste from project build.gradle.kts:

buildscript {
    repositories {
        gradlePluginPortal()
        jcenter()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
        classpath("com.android.tools.build:gradle:4.1.3")
    }
}
Related