JNI Library Target Machines Configuration
You can open this sample inside an IDE using the IntelliJ native importer or Eclipse Buildship. |
This sample shows how a Java Native Interface (JNI) library can target multiple machines. The library has no dependencies, and the build has a minimal configuration.
In this sample, we are configuring the target machines of a JNI library implemented in Java and C++; however, this applies to other JVM and native languages as well.
build.gradle
plugins {
id 'java'
id 'dev.nokee.jni-library'
id 'dev.nokee.cpp-language'
}
library {
targetMachines = [
machines.windows.x86_64,
machines.macOS.x86_64,
machines.linux.x86_64
]
}
build.gradle.kts
plugins {
id("java")
id("dev.nokee.jni-library")
id("dev.nokee.cpp-language")
}
library {
targetMachines.set(listOf(
machines.windows.x86_64,
machines.macOS.x86_64,
machines.linux.x86_64
))
}
To build the library:
$ ./gradlew assemble BUILD SUCCESSFUL 5 actionable tasks: 5 executed
The JNI library JARs produced inside ./build/libs
directory:
$ ls ./build/libs/*.jar ./build/libs/jni-library-with-target-machines-macos.jar ./build/libs/jni-library-with-target-machines.jar
It’s also important to note that when multiple target platform are configured, the native component will be namespaced inside their own variant directory. Gradle will also produce a separated JAR for each variant:
$ jar tf ./build/libs/jni-library-with-target-machines.jar META-INF/ META-INF/MANIFEST.MF com/ com/example/ com/example/greeter/ com/example/greeter/NativeLoader.class com/example/greeter/Greeter.class
$ jar tf ./build/libs/jni-library-with-target-machines-macos.jar META-INF/ META-INF/MANIFEST.MF macos/ macos/libjni-library-with-target-machines.dylib
For more information, see JNI Library Plugin reference chapter.