-
Create kotlin source files: Main.kt and NativeSample.kt. See files in
src
folder. -
Notice in NativeSample.kt, I import "NativeSample" library. This is the name of my dll/so file. It should be changed to match your project.
-
Create native header file by running the following command:
javah -jni -cp ".;path/to/kotlin-runtime.jar" sam.NativeSample
- Make sure javah is in your system path. More on javah
- sam.NativeSample is the class name that contains native calls.
-
Once the header file is generated (see
jni/sam_NativeSample.h
), you can create the implementation file for your header (seejni/sam_NativeSampleImp
). -
That is it. Follow the build/run instructions below.
Build and compile kotlin with Kotlin CLI. If you use IntelliJ, you can also use Kotlin plugin for compilation.
Build and compile C/C++ source to SO/DLL library with Clang, GCC or Visual Studio.
One way to run the program is:
- navigating to build folder
out/production/kolin-jni-example
- copy your native dll file to this directory.
- run
java -cp ".;path/to/kotlin-runtime.jar" sam.SamPackage