无法在 Android Studio 中将 io.ktor 导入到 KMM 的公共模块

问题描述

总的来说,我是 Kotlin Multiplatform Mobile 和移动开发的新手。我正在尝试按照此处的教程 on KMM tutorials 在我的项目中使用 Ktor。

添加依赖后,如图build.gradle.kts 下面(commonMain、androidMain 和 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)

我试图 import io.ktor.client.* 到公共模块中的一个类,但它无法解析。但是,当我尝试为 Android 模块中的一个类做同样的事情时,它就可以工作了。请看下面的截图:

import in the Common module

import in the Android module

所以我的问题是:我哪里出错了?或者,它应该是这样的?从文档中,我相信我尝试做的网络应该在公共模块中完成,而不是在特定于平台的模块中完成。

请帮忙,我一直在环顾四周以了解问题所在,但没有运气。谢谢!

编辑: 我一直在 Android Studio 中收到此通知

Issue

我用谷歌搜索了一下,似乎错误通知 is incorrect?

解决方法

在 root org.jetbrains.kotlin:kotlin-gradle-plugin 中将 build.gradle.kts 从 1.4.10 更新到 1.4.31 为我解决了这个问题。

这是我的 build.gradle.kts 文件的样子:

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()
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...