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

Kotlin Multiplatform新项目build.gradle未解析引用错误求助

KMP项目中kotlin块报未解析引用但项目可正常构建的解决方法

问题描述

Kotlin Multiplatform项目中,IDE提示kotlin标签存在未解析引用错误,错误信息如下:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:

public fun DependencyHandler.kotlin(module: String, version: String? = ...): Any defined in org.gradle.kotlin.dsl

public fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec defined in org.gradle.kotlin.dsl

项目可正常构建和运行,但错误提示持续存在,新建KMP项目后问题依然出现。

解决方案

1. 清理IDE缓存并重新同步Gradle(优先尝试)

这是最常见的原因——IDE缓存未正确加载KMP插件的扩展配置:

  • 打开Android Studio,点击顶部菜单栏的File -> Invalidate Caches...
  • 在弹出的窗口中勾选Invalidate and Restart,等待IDE重启
  • 重启后点击右上角的Sync Project with Gradle Files按钮,重新同步项目

2. 检查Gradle与AGP、Kotlin版本兼容性

Kotlin 2.0.0对Gradle和AGP版本有最低要求:

  • 修改libs.versions.toml中的AGP版本为兼容版本(建议8.8.0及以上):
    [versions]
    agp = "8.8.0"
    kotlin = "2.0.0"
    # ... 其他版本保持不变
    
  • 确保项目使用的Gradle wrapper版本在8.5及以上,可查看gradle/wrapper/gradle-wrapper.properties中的distributionUrl:
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
    

3. 验证插件配置正确性

确认shared模块的build.gradle.kts中插件引用正确,无拼写错误:

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
}

无需额外导入其他Kotlin DSL扩展,上述插件会自动提供kotlin块的扩展支持。

补充说明

如果以上方法都无效,可以尝试:

  • 删除项目根目录下的.gradle和.idea文件夹,重新导入项目
  • 确保Android Studio的Kotlin插件版本与项目中使用的Kotlin版本一致(2.0.0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:33:15