IntelliJ IDEA 2018.2 Help

Setting up JNI development in Gradle project

IntelliJ IDEA supports JNI development in Gradle projects.

To add JNI support

  1. Create a new or open an existing Gradle project.

  2. Open the build.gradle file.

  3. Specify the C plugin and define a native library:

    apply plugin: 'c' model { components { hello(NativeLibrarySpec) } }
    If you want to see the whole project, refer to the project's build.gradle file.
    When you specified the native library, the shared and static library binaries are added to the Gradle projects tool window (build directory).
    shared static libraries jni

  4. In the Project tool window, in the src | java directory create a Java class (New | Java Class) that will use C code.

  5. Open the created class in the editor and enter your code.

    class HelloWorld { public native void print(); static { System.loadLibrary("hello"); } }

  6. In the Project tool window, in the src directory, create the hello directory and the c subdirectory.

  7. In the c subdirectory, create the hello.c file which is a file for your C programs.

    gradle jni hello c

  8. 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 World!\n"); return; }

At this point you can start developing your application further using native codes as needed.

Last modified: 20 November 2018