微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。