I am writing a cross-platform application using JFace that should run on both Windows and Linux.
Unfortunately, JFace requires different SWT versions depending on platform — it requires org.eclipse.platform:org.eclipse.swt.gtk.linux.x86_64
on 64 bit Linux and org.eclipse.platform:org.eclipse.swt.win32.win32.x86_64
on 64 bit Windows.
I have come up with the following temporary solution:
import org.gradle.internal.os.OperatingSystem
plugins {
// ...
id("com.github.johnrengelman.shadow") version "6.0.0"
}
// ...
val swtVersion = "3.114.100"
val swtName = "org.eclipse.swt." +
(System.getProperty("platform") ?: OperatingSystem.current().let { current ->
when {
current.isLinux -> "gtk.linux"
current.isWindows -> "win32.win32"
current.isMacOsX -> "cocoa.macosx"
else -> throw RuntimeException("Unsupported operating system: $current")
}
} + if (System.getProperty("os.arch").contains("64")) ".x86_64" else ".x86")
configurations.all {
resolutionStrategy.eachDependency {
if (requested.name == "org.eclipse.swt.\${osgi.platform}") {
useTarget("${requested.group}:$swtName:${requested.version}")
}
}
}
dependencies {
// ...
implementation("org.eclipse.platform", "org.eclipse.jface", "3.20.0")
}
application {
mainClassName = "com.example.myapp.AppKt"
}
It allows to build JAR that runs only on host OS with:
gradle shadowJar
and to cross compile for Windows with:
gradle shadowJar -Dplatform=win32.win32.x86_64
When ran on a wrong platform it fails (as expected) with:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons:
no swt-win32-4934r6 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
no swt-win32 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
Can't load library: /home/nawordar/.swt/lib/linux/x86_64/libswt-win32-4934r6.so
Can't load library: /home/nawordar/.swt/lib/linux/x86_64/libswt-win32.so
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:342)
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:256)
at org.eclipse.swt.internal.C.<clinit>(C.java:19)
at org.eclipse.swt.widgets.Display.<clinit>(Display.java:145)
at com.example.myapp.AppKt.main(App.kt:9)
at com.example.myapp.AppKt.main(App.kt)
I am looking for either of two solutions:
- Create tasks that build JARs with different names and dependencies for each platform and a meta-task that builds both JARs at once;
- Create one JAR that runs on both platforms.
As for the first one, I am new to Gradle and I cannot find a way to create a task that produces JAR with name specific to the platform and has platform-specific dependencies. On this website the classifier
property is mentioned, but it is deprecated. archiveClassifier
, that is supposed to replace it, is read-only, so I don't know how I can use it. The second problem I have with this solution is that I cannot find how to declare task specific resolution strategy.
As for the second one, I'm not sure if that is possible, but if it is, it would be a preferable solution.