无法将io.ktor导入到Android Studio中的KMM的通用模块 [英] Cannot import io.ktor to common module of KMM in Android Studio
问题描述
因此,我通常是Kotlin Multiplatform Mobile和移动开发的新手.我正在尝试在KMM教程中
所以我的问题是:我哪里出错了?还是应该是这样?从文档中,我相信我要尝试的联网应该在通用模块中完成,而不是在平台特定的模块中完成.
请帮助,我一直在四处寻找问题所在,但没有运气.谢谢!
我一直在Android Studio中收到此通知
我用Google搜索了它,似乎错误通知不正确?
在root build中将
为我解决了这个问题. org.jetbrains.kotlin:kotlin-gradle-plugin
从1.4.10更新到1.4.31.gradle.kts
这是我的 build.gradle.kts
文件的外观:
buildscript {储存库{gradlePluginPortal()jcenter()谷歌()mavenCentral()}依赖项{classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")classpath("com.android.tools.build:gradle:4.2.0-beta06")classpath("com.squareup.sqldelight:gradle-plugin:1.4.4")}}所有项目{储存库{谷歌()jcenter()mavenCentral()}}
So I am a newbie to Kotlin Multiplatform Mobile and mobile development in general. I am trying to follow this tutorial here on KMM tutorials to use Ktor in my project.
After adding dependencies, as shown in the build.gradle.kts below (dependencies for commonMain, androidMain, and iosMain):
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
}
}
}
val ktorVersion = "1.5.2"
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktorVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation ("io.ktor:ktor-client-android:$ktorVersion")
}
}
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:$ktorVersion")
}
}
val iosTest by getting
}
}
android {
compileSdkVersion(29)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(29)
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-frameworks")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
I tried to import io.ktor.client.*
to a class in the common module, but it won't resolve. However, when I tried to do the same thing for a class in the Android module then it works. Please see the screenshots below:
So my question is: Where did I go wrong? Or, is it supposed to be like this? From the documentation, I believe that the networking I am trying to do is supposed to be done in the common module, instead of in the platform specific module.
Please help, I have been looking around to understand what the problem is but no luck. Thank you!
Edit: I have been having this notification in Android Studio
I googled it and it seems that the error notification is incorrect?
Updating org.jetbrains.kotlin:kotlin-gradle-plugin
from 1.4.10 to 1.4.31 in root build.gradle.kts
fixed the issue for me.
This is how my build.gradle.kts
file looks like:
buildscript {
repositories {
gradlePluginPortal()
jcenter()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
classpath("com.android.tools.build:gradle:4.2.0-beta06")
classpath("com.squareup.sqldelight:gradle-plugin:1.4.4")
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
这篇关于无法将io.ktor导入到Android Studio中的KMM的通用模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!