我发现 了这个 关于 Android 的类似问题,但我使用纯 Java 和 Maven 作为构建工具。我认为最好发布一个新问题。
我创建了一个 Kotlin 类,我试图从 Java 类中将其引用为 MyKotlinClass.class
。 Maven 构建失败,而 IntelliJ Idea 中的编译工作正常。我已经将 Kotlin 插件添加到 Maven 中:
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
但是,这无济于事:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project app: Compilation failure
[ERROR] MyClassLinkingKotlin.java:[100,40] error: cannot find symbol
该行/列恰好是指符号 MyKotlinClass.class
。即使像这样使用它也会失败:
System.err.println(MyKotlinClass.class)
原文由 Vojtěch 发布,翻译遵循 CC BY-SA 4.0 许可协议
您的 Maven 配置添加了 Kotlin 编译器插件,但没有调整 Java 编译器插件的执行,以便 Java 编译器在 Kotlin 编译器之后运行。因此,Java 编译器在 Kotlin 之前运行,并且看不到 Kotlin 编译的类。
这是一个片段,显示了混合语言项目的正确配置(取自 文档):