Kotlin(JVM)标准库元素的未解决参考

IntelliJ添加gradle到现有的Kotlin项目之后,我开始引用一些标准库元素的问题。 例如,Kotlin Stringtypes被识别,但mutableMapOf给我

  Error:(11, 60) Kotlin: Unresolved reference: mutableMapOf 

另一个是:

 Error:(9, 78) Kotlin: Unresolved reference: Array 

编译期间。 它们在IDE中也被标记为红色(不是孤立的,只能编译)

IntelliJ中出现的另一个错误是Kotlin not configured ,没有配置Kotlin选项

这是我的gradle构建文件:

 apply plugin: 'kotlin' group = "com.serguei.myproject" version = "1.0" buildscript { ext.kotlin_version = '1.2.10' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" compile 'com.google.code.gson:gson:2.8.2' } compileKotlin { kotlinOptions { jvmTarget = "1.8" } } compileTestKotlin { kotlinOptions { jvmTarget = "1.8" } } 

感谢您对Ice100和Preston Garno的评论,他们帮助我们find了解决方案。

问题是我的项目结构不正确。 我有:

 MyProject -> src -> [Source Files] -> build.gradle 

然而,在更仔细地咨询使用Gradle之后,似乎推荐的项目结构应该是

 MyProject -> src -> main -> kotlin -> [Source Files] -> build.gradle 

一旦项目结构调整,进入File -> Invalidate Caches / Restart ,然后单击Invalidate and Restart 。 一旦索引器重新运行,现在就可以使用Kotlin标准库types,函数等。