Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

ktor - Cannot import dependecies in kotlin multiplatform mobile

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?

question from:https://stackoverflow.com/questions/65847008/cannot-import-dependecies-in-kotlin-multiplatform-mobile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using IntelliJ IDEA Ultimate 2020.3 I've created a KMM project from File->New...->Project. Then, I added ktor as dependencies exactly as you did in the code included in your question. When creating an ApiClient class just like yours, it worked for me both in the androidMain as well as the commonMain source set, with IDEA automatically importing HttpClient.

So since I cannot reproduce this, I fear that without knowing the exact code you are running I'll have a tough time helping.

However, just in case it might be of some help, I've put my minimal project on Github (here), maybe you can find a hint there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...