I have two separate multi-projects. One is for the backend on the server (Amazon/Tomcat), the other for the frontend. In the backend project, most of the projects are written in Java. One sub-project is written in Kotlin. Part of the code here is used by both the backend and frontend. It should be using JVM for the backend, and export a JavaScript file to be bundled with the frontend project.
When I build the projects using the commandline Gradle wrapper, it builds successfully. When I build the project in IntelliJ (2024.3.2.2), the inspection tool reports compile errors. I've created a minimum example to illustrate one such compile error. I'm using the following java --version
openjdk 21.0.6 2025-01-21 LTS
OpenJDK Runtime Environment Corretto-21.0.6.7.1 (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Corretto-21.0.6.7.1 (build 21.0.6+7-LTS, mixed mode, sharing)
Project structure in this example:
root
|-> JavaProject
| |-> src/javaPackage
| | |-> JavaFile.java
| |-> build.gradle.kts
|-> KotlinProjct
| |-> src/commonMain/kotlin
| | |-> KotlinFile.kt
| |-> build.gradle.kts
|-> build.gradle.kts
|-> settings.gradle.kts
settings.gradle.kts
pluginManagement {
plugins {
`kotlin-dsl`
}
}
include("JavaProject")
include("KotlinProject")
Root gradle.build.kts
import .jetbrains.kotlin.gradle.dsl.JvmTarget
import .jetbrains.kotlin.gradle.dsl.KotlinVersion
plugins {
kotlin("multiplatform") version "2.1.10" apply false
java
}
allprojects {
tasks.withType<JavaCompile> {
sourceCompatibility = "21"
targetCompatibility = "21"
}
tasks.withType<.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
compilerOptions {
languageVersion.set(KotlinVersion.KOTLIN_2_1)
apiVersion.set(KotlinVersion.KOTLIN_2_1)
jvmTarget.set(JvmTarget.JVM_21)
}
}
tasks.withType<Wrapper> {
gradleVersion = "8.12.1"
distributionType = Wrapper.DistributionType.ALL
}
}
KotlinProject/build.gradle.kts
plugins {
kotlin("multiplatform")
}
repositories {
mavenCentral()
}
kotlin {
jvm {
withJava()
}
js(IR) {
browser {
}
binaries.executable()
}
}
JavaProject/build.gradle.kts
plugins {
`java-library`
}
repositories {
mavenCentral()
}
dependencies {
implementation(project(":KotlinProject"))
}
sourceSets {
main {
java {
srcDirs("src")
}
resources {
srcDirs("src")
}
output.setResourcesDir(file("build/classes/java/main"))
java.destinationDirectory.set(file("build/classes/java/main"))
}
}
task<JavaExec>("RunJavaMain") {
mainClass = "javaPackage.JavaFile"
classpath = sourceSets["main"].runtimeClasspath
}
KotlinFile.kt
package kotlinPackage
import kotlin.js.JsName
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic
data class KotlinObject(
@JvmField
val string : String
)
{
companion object
{
@JvmStatic
@JsName("parse")
fun parse(input : String) : KotlinObject
{
return KotlinObject(input)
}
}
}
JavaFile.java
package javaPackage;
import kotlinPackage.KotlinObject;
class JavaFile
{
public static void main(String[] args)
{
KotlinObject item = KotlinObject.parse("Foo");
}
}
When I build this in IntelliJ, I get a rather funny compile error on ("Foo")
:
Required type:
String
Provided:
String
The more detailed message is
'parse([email protected] String)' in 'kotlinPackage.KotlinObject' cannot be applied to '(java.lang.String)'
This message does not appear if I build the project using the commandline Gradle wrapper:
./gradlew build
BUILD SUCCESSFUL in 923ms
30 actionable tasks: 3 executed, 27 up-to-date
./gradlew --version
------------------------------------------------------------
Gradle 8.12.1
------------------------------------------------------------
Build time: 2025-01-24 12:55:12 UTC
Revision: 0b1ee1ff81d1f4a26574ff4a362ac9180852b140
Kotlin: 2.0.21
Groovy: 3.0.22
Ant: Apache Ant(TM) version 1.10.15 compiled on August 25 2024
Launcher JVM: 21.0.6 (Amazon Inc. 21.0.6+7-LTS)
Daemon JVM: /usr/lib/jvm/java-21-amazon-corretto (no JDK specified, using current Java home)
OS: Linux 6.8.0-52-generic amd64
Invalidate caches makes no difference.
If I comment out the following section in KotlinProject/build.gradle.kts
js(IR) {
browser {
}
binaries.executable()
}
the compile error above disappears in IntelliJ for JavaFile.java
, but then problems pop up with KotlinFile.kt
instead:
Unresolved reference 'JsName'.
Regardless, this is not a fix; I need the JavaScript files for the frontend project.
I've ran tests on the real project (not the example above) when its built with the Gradle wrapper, and there appears to be no problems. In other words, this is some sort of false positive that only exists in IntelliJ.
There are a lot more issues than just the one above. I just created an example with one of the simplest, relevant cases that I could come up with.