我创建了一个新的kotlin多平台移动项目。我遵循官方文件。基本项目正在运行,我能够在Android上毫无问题地构建它。
我想添加一些api,发现了我以前从未使用过的ktor
。我在这里看到:https://kotlinlang.org/docs/mobile/use-ktor-for-networking.html和教程:https://proandroiddev.com/kotlin-multiplatform-very-beginners-guide-part-2-api-d54f7326dc57我所做的所有改变是:
我添加了ktor
库到build.gradle.kts(:共享)
:
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
}
我创建了Api类,我想在其中创建和使用HttpClient:
class Api() {
private val client = HttpClient()
suspend fun fetch(): String {
return ""
}
}
但是HttpCLient()
是“未解析引用”,无法导入。我还尝试手动添加导入io。克托尔。客户HttpClient
但是io
是“未解析的引用”。我还尝试了多次重建/清理/同步。我做错了什么?我错过什么了吗?
我在Windows7和Android Studio 4.1上遇到了同样的问题。3.当在项目的构建脚本中,在构建脚本的依赖项部分,它从1.4更新了kotlin gradle插件工件的版本时,问题就解决了。10比1.4。32.下面是project build的复制粘贴。格拉德尔。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")
}
}
使用IntelliJ IDEA Ultimate 2020.3,我从文件创建了一个KMM项目-
因此,由于我无法再现这一点,我担心如果不知道您正在运行的确切代码,我将很难帮助您。
然而,为了以防万一它可能有一些帮助,我把我的最小项目放在了Github上(这里),也许你可以在那里找到一个提示。