JNI with JDK 10+

I’m experimenting with JNI.

I found that the JDK includes tools to generate the JNI header files. In JDK 9 and earlier, javah looks great. It work with bytecode, so we can compile Kotlin source into bytecode, then generate the JNI headers.

The Kotlin docs say (emphasis mine):

To declare a function that is implemented in native (C or C++) code, you need to mark it with the external modifier.

external fun foo(x: Int): Double

The rest of the procedure works in exactly the same way as in Java.

However, javah was removed from JDK 10+, in favor of javac -h. javac only works on Java source code, so there seems to be no simple way to generate JNI headers from Kotlin code in the same way as we would do it in Java.

Is there any built-in support for generating JNI headers from Kotlin code, or any plans to add support for this? It seems like right now it’s necessary to decompile bytecode to Java in order to run javac -h, or otherwise use the javah tool from JDK 9 and earlier.

1 Like

I found a YouTrack issue was raised for this three months ago, but it doesn’t seem to have been prioritized or have a target version.

@adamu - i’ve been using this with great success as part of my build process: https://github.com/Glavo/gjavah/pull/3