The JavaFX gradle plugin works well for a JVM-only project, but I can't figure out how to add JavaFX as a dependency to a multiplatform project's source set. I tried to use the plugin globally, without success. I aslo tried declaring the dependency in this way
val jvmMain by getting {
dependencies {
dependsOn(commonMain)
implementation("org.openjfx:javafx:18.0.2")
}
}
but it does not work.
So how can I use JavaFX in a multiplatform source set ? Thank you in advance !
CodePudding user response:
After searching the JavaFX gradle plugin, it turns out that I have to use different JARs depending on the platform, as JavaFX has native components. So I wrote this code that works to compile the library, I will look at the packaging later.
kotlin {
targets {
jvm()
// Other targets...
}
sourceSets {
commonMain {
dependencies {
// Common dependencies
}
}
// Other source sets
val jvmMain by getting {
dependencies {
// As JavaFX have platform-specific dependencies, we need to add them manually
val fxSuffix = when (osdetector.classifier) {
"linux-x86_64" -> "linux"
"linux-aarch_64" -> "linux-aarch64"
"windows-x86_64" -> "win"
"osx-x86_64" -> "mac"
"osx-aarch_64" -> "mac-aarch64"
else -> throw IllegalStateException("Unknown OS: ${osdetector.classifier}")
}
// Replace "compileOnly" with "implementation" for a non-library project
compileOnly("org.openjfx:javafx-base:18.0.2:${fxSuffix}")
compileOnly("org.openjfx:javafx-graphics:18.0.2:${fxSuffix}")
compileOnly("org.openjfx:javafx-controls:18.0.2:${fxSuffix}")
}
}
}
}
This code replaces the plugin entirely.