Android studio Android Studio多平台项目commonMain中的ktor依赖项在IDE中未解决,但代码运行
在使用kotlin multiplatform的commonMain源集的ktor依赖项时,我对Android Studio IDE有一个问题。问题是IDE无法识别这种依赖关系,但程序编译并运行良好。此外,在androidMain源集中,可以识别依赖关系。我看到过关于类似问题的其他问题,但我没有看到有人在程序编译和运行时遇到过这个问题 梯度依赖 以下内容位于项目共享文件夹的build.gradle.kts中Android studio Android Studio多平台项目commonMain中的ktor依赖项在IDE中未解决,但代码运行,android-studio,kotlin-multiplatform,ktor,gradle-dependencies,source-sets,Android Studio,Kotlin Multiplatform,Ktor,Gradle Dependencies,Source Sets,在使用kotlin multiplatform的commonMain源集的ktor依赖项时,我对Android Studio IDE有一个问题。问题是IDE无法识别这种依赖关系,但程序编译并运行良好。此外,在androidMain源集中,可以识别依赖关系。我看到过关于类似问题的其他问题,但我没有看到有人在程序编译和运行时遇到过这个问题 梯度依赖 以下内容位于项目共享文件夹的build.gradle.kts中 kotlin { android() ios { bin
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:1.5.1")
implementation("io.ktor:ktor-client-cio:1.5.1")
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
implementation("io.ktor:ktor-client-android:1.5.1")
}
}
...
}
}
其中点表示其他源集的依赖关系,例如iosMain,它是空的
在commonMain代码中,我有一个类KtorTest:
package com.example.myapplication222.shared
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
class KtorTest {
val client: HttpClient = HttpClient(CIO)
suspend fun get(): String {
val res: String = client.get("http://www.7timer.info/bin/api.pl?lon=113.17&lat=23.09&product=astro&output=json")
return res
}
}
主要活动
在主活动中,我导入并使用KtorTest类执行get请求
package com.example.myapplication222.androidApp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.myapplication222.shared.KtorTest
import kotlinx.coroutines.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var response = ""
val c = GlobalScope.launch {
response = get()
}
c.invokeOnCompletion {
println("***RESPONSE***");
println(response) }
}
suspend fun get(): String {
val a = KtorTest()
return a.get()
}
}
结果
程序构建、运行并打印出以下内容
I/System.out: ***RESPONSE***
{
"product" : "astro" ,
"init" : "2021021700" ,
"dataseries" : [
{
"timepoint" : 3,
"cloudcover" : 4,
I/System.out: "seeing" : 6,
"transparency" : 2,
"lifted_index" : 15,
"rh2m" : 5,
"wind10m" : {
"direction" : "NE",
"speed" : 3
},
"temp2m" : 20,
"prec_type" : "none"
},
...
}
where the response is cut short for brevity
Android Studio的屏幕截图:
第一个屏幕截图是上面展示的KtorTest
第二个屏幕截图是KtorTest2类,它与上面的KtorTest2完全相同,只是它位于多平台项目中共享文件夹的androidMain文件夹中
在这些图像中,您可以看到IDE抱怨commonMain中的ktor,而不是androidMain中的ktor。您只需要在
commonMain
中包含io.ktor:ktor客户端核心
,并在所需目标中包含实际的HTTP引擎实现。如果您想在android中使用CIO引擎,只需在androidMain
中包含io.ktor:ktor客户端CIO
。Ktor将自动选择平台可用的HTTP客户端。您可以像这样更新KtorTest
类(请注意,没有发动机规格):
在kotlin多平台项目的build.gradle.kts中将kotlin gradle插件更新为1.4.31修复了此问题
有关更多详细信息,请参见以下答案:我明白了,但这无助于语法突出显示。我认为我的渐变操作有问题,使缓存无效/重新启动ide。使缓存无效并重新启动没有帮助。有趣的是,当我在androidstudio中打开以下示例代码的客户机mpp项目时,这不是一个问题
class KtorTest {
val client: HttpClient = HttpClient
}