You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KMM项目commonMain中无法访问io.ktor.client.HttpClient类求助

KMM项目commonMain无法访问HttpClient类的问题

问题描述

在KMM项目的commonMain目录下尝试访问包含HttpClient实例的类时,触发错误提示:

Cannot access class 'io.ktor.client.HttpClient'. Check your module classpath for missing or conflicting dependencies。

测试环境

已在两个Android Studio版本中复现该问题:

Android Studio Flamingo | 2022.2.1 Patch 2

  • Build编号:AI-222.4459.24.2221.10121639,构建于2023年5月12日
  • Runtime版本:17.0.6+0-17.0.6b802.4-9586694 aarch64
  • VM:OpenJDK 64-Bit Server VM by JetBrains s.r.o.
  • 系统:macOS 13.4.1
  • GC:G1 Young Generation, G1 Old Generation
  • 内存:2048M
  • 核心数:10
  • Metal Rendering:已开启
  • 注册表配置:
    • external.system.auto.import.disabled=true
    • ide.text.editor.with.preview.show.floating.toolbar=false
    • ide.instant.shutdown=false
    • gradle.version.catalogs.dynamic.support=true
  • 非捆绑插件:com.jetbrains.kmm (0.6.1(222)-14)

Android Studio Giraffe | 2022.3.1 RC 1

  • Build编号:AI-223.8836.35.2231.10320515,构建于2023年6月14日
  • Runtime版本:17.0.6+0-17.0.6b829.9-10027231 aarch64
  • VM:OpenJDK 64-Bit Server VM by JetBrains s.r.o.
  • 系统:macOS 13.4.1
  • GC:G1 Young Generation, G1 Old Generation
  • 内存:2048M
  • 核心数:10
  • Metal Rendering:已开启
  • 注册表配置:
    • external.system.auto.import.disabled=true
    • ide.text.editor.with.preview.show.floating.toolbar=false
    • ide.instant.shutdown=false
  • 非捆绑插件:com.jetbrains.kmm (0.6.1(223)-18)

当前依赖配置

val coroutinesVersion = "1.7.2"
val ktorVersion = "2.3.2"

sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("io.ktor:ktor-client-core:$ktorVersion")
            implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
            implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
        }
    }
    val androidMain by getting {
        dependencies {
            implementation("io.ktor:ktor-client-android:$ktorVersion")
        }
    }
    val iosMain by getting {
        dependencies {
            implementation("io.ktor:ktor-client-darwin:$ktorVersion")
        }
    }
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test"))
        }
    }
}

解决方案

1. 同步Gradle项目

执行File -> Sync Project with Gradle Files,确保依赖包已正确下载并同步。若同步失败,查看Gradle控制台的错误日志,排查网络或仓库配置问题。

2. 清理重建项目

先执行Build -> Clean Project清除旧构建文件,再执行Build -> Rebuild Project重新构建,解决缓存导致的依赖加载异常。

3. 确认KMM插件应用

检查项目根目录的build.gradle.kts(或build.gradle),确保已正确应用KMM和Kotlin多平台插件:

plugins {
    id("com.android.application") version "7.4.2" // 适配你的Android Studio版本
    id("org.jetbrains.kotlin.multiplatform") version "1.8.20" // 版本需与Ktor兼容
    id("com.jetbrains.kmm") version "0.6.1" // 与安装的插件版本一致
}

4. 验证Kotlin与Ktor版本兼容性

Ktor 2.3.2要求Kotlin版本至少为1.8.20,确认项目中的Kotlin版本达标。可在根build.gradle.kts中查看并调整Kotlin版本。

5. 排查依赖冲突

在终端执行以下命令分析依赖树:

./gradlew dependencies

查看是否存在多个版本的Ktor客户端相关依赖,若有冲突,使用exclude排除冗余依赖,或统一所有Ktor相关依赖的版本号。

6. 清除IDE缓存

若上述步骤无效,执行File -> Invalidate Caches...,勾选"Clear file system cache and local history"后点击Invalidate and Restart,清除IDE缓存后重新打开项目。


内容的提问来源于stack exchange,提问作者georgij

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 08:39:51