@jendrik
Indeed, it’s working! Thank you very much.
However, I’m not sure we talked about the same problem (well, maybe I don’t understand everything haha):
We don’t declare the natives for different platforms in our dependencies (otherwise our code would simply crash). The task works the first time we launch it, but when we run it again, Gradle try to reload the dependencies, so the task fail: “Library is already loaded in another ClassLoader”.
A temporary solution was to:
gradle --stop
- disable the gradle daemon mode
For the log (could help somebody else): I replaced this code:
project.ext.lwjglVersion = "3.2.3"
switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
project.ext.lwjglNatives = "natives-linux"
break
case OperatingSystem.MAC_OS:
project.ext.lwjglNatives = "natives-macos"
break
case OperatingSystem.WINDOWS:
project.ext.lwjglNatives = "natives-windows"
break
}
dependencies {
implementation "org.lwjgl:lwjgl:$lwjglVersion"
implementation "org.lwjgl:lwjgl-shaderc:$lwjglVersion"
runtimeOnly "org.lwjgl:lwjgl:$lwjglVersion:$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-shaderc:$lwjglVersion:$lwjglNatives"
}
By this one:
project.ext.lwjglVersion = "3.2.3"
def osArch = System.getProperty("os.arch")
project.ext.arch = osArch.startsWith("armv8") || osArch.contains("64") ? "x86-64" : "x86"
switch ( OperatingSystem.current() ) {
case OperatingSystem.WINDOWS:
project.ext.OS = "windows"
break
case OperatingSystem.LINUX:
project.ext.OS = "linux"
break
case OperatingSystem.MAC_OS:
project.ext.OS = "macos"
break
}
class LwjglRule implements ComponentMetadataRule { //val os: String, val arch: String, val classifier: String)
private def nativeVariants = [
[os: OperatingSystemFamily.LINUX, arch: "arm32", classifier: "natives-linux-arm32"],
[os: OperatingSystemFamily.LINUX, arch: "arm64", classifier: "natives-linux-arm64"],
[os: OperatingSystemFamily.LINUX, arch: "x86-64", classifier: "natives-linux"],
[os: OperatingSystemFamily.WINDOWS, arch: "x86", classifier: "natives-windows-x86"],
[os: OperatingSystemFamily.WINDOWS, arch: "x86-64", classifier: "natives-windows"],
[os: OperatingSystemFamily.MACOS, arch: "x86-64", classifier: "natives-macos"]
]
@javax.inject.Inject
ObjectFactory getObjects() { }
void execute(ComponentMetadataContext context) {
context.details.withVariant("runtime") {
attributes {
attributes.attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, objects.named(OperatingSystemFamily, "none"))
attributes.attribute(MachineArchitecture.ARCHITECTURE_ATTRIBUTE, objects.named(MachineArchitecture, "none"))
}
}
nativeVariants.each { variantDefinition ->
context.details.addVariant("${variantDefinition.classifier}-runtime", "runtime") {
attributes {
attributes.attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, objects.named(OperatingSystemFamily, variantDefinition.os))
attributes.attribute(MachineArchitecture.ARCHITECTURE_ATTRIBUTE, objects.named(MachineArchitecture, variantDefinition.arch))
}
withFiles {
addFile("${context.details.id.name}-${context.details.id.version}-${variantDefinition.classifier}.jar")
}
}
}
}
}
configurations["runtimeClasspath"].attributes {
attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, objects.named(OperatingSystemFamily, "$project.ext.OS"))
attribute(MachineArchitecture.ARCHITECTURE_ATTRIBUTE, objects.named(MachineArchitecture, "$project.ext.arch"))
}
dependencies {
components {
withModule("org.lwjgl:lwjgl", LwjglRule)
withModule("org.lwjgl:lwjgl-shaderc", LwjglRule)
}
implementation "org.lwjgl:lwjgl:$lwjglVersion"
implementation "org.lwjgl:lwjgl-shaderc:$lwjglVersion"
}