Android Studio 多平台项目的 commonMain 中的 ktor 依赖项在 IDE 中未解决但代码运行

ktor dependency in commonMain of Android Studio multiplatform project unresolved in IDE but code runs

在使用 kotlin 多平台的 commonMain 源集的 ktor 依赖项时,Android Studio IDE 出现问题。问题是 IDE 无法识别这种依赖关系,但程序编译并运行良好。此外,在 androidMain 源集中识别依赖关系。我看过其他类似问题的问题,但是我没有看到任何人在程序编译和运行的地方出现这个问题。

Gradle 依赖关系

下面是项目共享文件夹中的build.gradle.kts

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代码中,我有一个class 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
    }
}

主要Activity

在主要的 activity 中,我导入并使用 KtorTest class 来执行获取请求。

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 工作室的屏幕截图:

第一个屏幕截图是上面显示的 KtorTest。

KtorTest in commonMain of shared code in Android Studio kotlin multiplatform project

第二张截图是classKtorTest2,和上面的KtorTest一模一样,只不过是在多平台项目的共享文件夹的androidMain文件夹下。

KtorTest2 in androidMain of shared code in Android Studio kotlin multiplatform project

在这些图像中,您可以看到 IDE 在 commonMain 中抱怨 ktor,但在 androidMain 中没有。

您只需在 commonMain 中包含 io.ktor:ktor-client-core 并在所需目标中包含实际的 HTTP 引擎实现。如果你想在android中使用CIO引擎,只需在androidMain中包含io.ktor:ktor-client-cio。 Ktor 将自动 select 可用的 HTTP 客户端供平台使用。您可以像这样更新 KtorTest class(注意没有引擎规范):

class KtorTest {
    val client: HttpClient = HttpClient
}

在 kotlin 多平台项目的 build.gradle.kts 中将 kotlin-gradle-plugin 更新到 1.4.31 修复了这个问题

有关详细信息,请参阅以下答案: