Set up JNI development in Gradle project
Aqua supports JNI development in Gradle projects.
This tutorial uses on the Java 1.8 and Gradle 7.4 versions. The tutorial uses the software model.
If you want to use the replacement plugins, you can check the JNI Sample project.
To add JNI support
Open the build.gradle file.
Add the following code to the build.gradle:
import org.gradle.internal.jvm.Jvm plugins { id 'java' id 'application' id 'c' } mainClassName = 'HelloWorld' repositories { mavenCentral() } dependencies { testImplementation 'junit:junit:4.12' } sourceCompatibility = 1.8 targetCompatibility = 1.8 application { applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath] } model { platforms { x64 { architecture "x86_64" } } components { hello(NativeLibrarySpec) { targetPlatform "x64" binaries.all { def jvmHome = Jvm.current().javaHome if (targetPlatform.operatingSystem.macOsX) { cCompiler.args '-I', "${jvmHome}/include" cCompiler.args '-I', "${jvmHome}/include/darwin" cCompiler.args '-mmacosx-version-min=10.4' linker.args '-mmacosx-version-min=10.4' } else if (targetPlatform.operatingSystem.linux) { cCompiler.args '-I', "${jvmHome}/include" cCompiler.args '-I', "${jvmHome}/include/linux" cCompiler.args '-D_FILE_OFFSET_BITS=64' } else if (targetPlatform.operatingSystem.windows) { cCompiler.args "-I${jvmHome}/include" cCompiler.args "-I${jvmHome}/include/win32" } else if (targetPlatform.operatingSystem.freeBSD) { cCompiler.args '-I', "${jvmHome}/include" cCompiler.args '-I', "${jvmHome}/include/freebsd" } } } } } classes.dependsOn 'helloSharedLibrary'import org.gradle.internal.jvm.Jvm plugins { id 'java' id 'application' id 'c' } mainClassName = 'HelloWorld' repositories { mavenCentral() } dependencies { testImplementation 'junit:junit:4.12' } sourceCompatibility = 1.8 targetCompatibility = 1.8 application { applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath] } model { platforms { x64 { architecture "x86_64" } } components { hello(NativeLibrarySpec) { targetPlatform "x64" binaries.all { def jvmHome = Jvm.current().javaHome if (targetPlatform.operatingSystem.macOsX) { cCompiler.args '-I', "${jvmHome}/include" cCompiler.args '-I', "${jvmHome}/include/darwin" cCompiler.args '-mmacosx-version-min=10.9' linker.args '-mmacosx-version-min=10.9' linker.args '-stdlib=libc++' } else if (targetPlatform.operatingSystem.linux) { cCompiler.args '-I', "${jvmHome}/include" cCompiler.args '-I', "${jvmHome}/include/linux" cCompiler.args '-D_FILE_OFFSET_BITS=64' } else if (targetPlatform.operatingSystem.windows) { cCompiler.args "-I${jvmHome}/include" cCompiler.args "-I${jvmHome}/include/win32" } else if (targetPlatform.operatingSystem.freeBSD) { cCompiler.args '-I', "${jvmHome}/include" cCompiler.args '-I', "${jvmHome}/include/freebsd" } } } } } classes.dependsOn 'helloSharedLibrary'To make your setup work smoothly, keep in mind the following details :
Aqua uses the version of Gradle JVM located in the Gradle settings.
Depending on your OS, compiler parameters may vary, keep that in mind when you change
cCompiler.args
andlinker.args
in your build.gradle.When the application is run with the following existing system property:
application { applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath] }and this application depends on
classes.dependsOn 'helloSharedLibrary'
, Aqua generates the binary libhello.dylib file for the shared library in the Project tool window.
In the Project tool window, select the src | java directory.
Right-click the java directory and select to create a Java class
HelloWorld
that will useC
code.Open the created
HelloWorld
class in the editor and enter the following code:class HelloWorld { public native void print(); static { System.loadLibrary("hello"); } public static void main(String[] args) { new HelloWorld().print(); } }Code specified in the file will load the generated system library.
In the Project tool window, in the src directory, create the hello/c/hello.c file, which is a file for your
C
programs.Open the hello.c file in the editor and specify the following code:
#include <jni.h> #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) { printf("Hello From C++ World!\n"); return; }
At this point you can start developing your application further using native codes as needed.
Run your application
Open your run configuration dialog.
If you use macOS as your operating system, then add the following code to the VM options field.
The VM options field can be added from the Modify options list.
Click OK to save the changes.
Click on the main toolbar.