Kotlin KMM 'commonMain' 依赖项:运行正常,AndroidStudio 显示错误

问题描述

我确实按照此处所述设置了 KMM 项目:https://kotlinlang.org/docs/mobile/getting-started.html

我想在 commonMain 模块中放置一些共享类。这些类需要一些依赖项。所以我添加了这里描述的依赖项:https://kotlinlang.org/docs/mobile/add-dependencies.html#dependency-on-a-library-used-in-specific-source-sets

shared/build.gradle.kts(缩短)

....
kotlin {
    android()
    ios {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-reflect:1.4.31")
            }
        }
...
        val androidMain by getting {
            dependencies {
            }
        }
....
        val iosMain by getting
        val iosTest by getting
    }
}
....

所以我创建了这个 InjectManager Helper 类,它使用了 kotlin-reflect 库。

问题:

AndroidStudio (4.1.3) 在类放在 commonMain 中时无法找到依赖项,只有放在 androidMain 中时才能找到。当我运行项目时 - 没有错误 - 一切正常。这正是 Android Studio 的看法。 InjectManagerInjectManager2 具有相同的代码。为了能够使用自动完成等,我目前的工作流程是这样的

  1. 将班级移至androidMain
  2. 修改代码
  3. 拖回到commonMain

InjectManager2 仅用于在屏幕截图中显示

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)