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

问题描述

在将 ktor 依赖项用于具有 kotlin 多平台的 commonMain 源集时,我遇到了 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 代码中,我有一个类 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 类来执行获取请求。

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。

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

第二张截图是KtorTest2类,与上面的KtorTest完全一样,只是它位于多平台项目中共享文件夹的androidMain文件夹中。

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

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

解决方法

您只需要在 io.ktor:ktor-client-core 中包含 commonMain 并在所需目标中包含实际的 HTTP 引擎实现。如果您想在 android 中使用 CIO 引擎,只需在 io.ktor:ktor-client-cio 中包含 androidMain。 Ktor 会自动为平台选择可用的 HTTP 客户端。您可以像这样更新 KtorTest 类(注意缺少引擎规范):

class KtorTest {
    val client: HttpClient = HttpClient
}
,

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

有关详细信息,请参阅以下答案:https://stackoverflow.com/a/66913665/14635103