JNI Library with macOS Framework Dependencies
You can open this sample inside an IDE using the IntelliJ native importer or Eclipse Buildship. |
This sample shows how a JNI library implemented in Java and Objective-C can be built with Gradle. The library has macOS framework dependencies, and the build has a minimal configuration.
build.gradle
plugins {
id 'java'
id 'dev.nokee.jni-library'
id 'dev.nokee.objective-c-language'
}
library {
targetMachines = [machines.macOS]
dependencies {
nativeImplementation 'dev.nokee.framework:Cocoa:latest.release' (1)
nativeImplementation 'dev.nokee.framework:JavaNativeFoundation:latest.release' (2)
/* When using Xcode 12.1 and lower, use the following instead:
nativeImplementation 'dev.nokee.framework:JavaVM:latest.release'
nativeImplementation('dev.nokee.framework:JavaVM:latest.release') {
capabilities {
requireCapability 'JavaVM:JavaNativeFoundation:latest.release'
}
}
*/
}
}
library.variants.configureEach {
sharedLibrary {
linkTask.configure {
linkerArgs.add('-lobjc')
}
}
}
build.gradle.kts
plugins {
id("java")
id("dev.nokee.jni-library")
id("dev.nokee.objective-c-language")
}
library {
targetMachines.set(listOf(machines.macOS))
dependencies {
nativeImplementation("dev.nokee.framework:Cocoa:latest.release") (1)
nativeImplementation("dev.nokee.framework:JavaNativeFoundation:latest.release") (2)
/* When using Xcode 12.1 and lower, use the following instead:
nativeImplementation("dev.nokee.framework:JavaVM:latest.release")
nativeImplementation("dev.nokee.framework:JavaVM:latest.release") {
capabilities {
requireCapability("JavaVM:JavaNativeFoundation:latest.release")
}
}
*/
}
}
library.variants.configureEach {
sharedLibrary {
linkTask.configure {
linkerArgs.add("-lobjc")
}
}
}
1 | Adding dependencies to macOS framework bundle is done by using the dev.nokee.framework group, the framework name (without the .framework ) and the version is currently infer from the framework |
2 | Adding dependencies to subframework is selected by using Gradle’s capabilities |
To build the library:
$ ./gradlew assemble BUILD SUCCESSFUL 4 actionable tasks: 4 executed
The JNI library produced inside ./build/libs
directory:
$ ls ./build/libs/*.jar ./build/libs/jni-library-with-framework-dependencies.jar
Since there is only one variant, the native component is included inside the main JAR:
$ jar tf ./build/libs/jni-library-with-framework-dependencies.jar META-INF/ META-INF/MANIFEST.MF com/ com/example/ com/example/cocoa/ com/example/cocoa/NativeLoader.class com/example/cocoa/NSSavePanel.class libjni-library-with-framework-dependencies.dylib
For more information, see JNI Library Plugin and Objective-C Language Plugin reference chapters.