Kotlin/C++ JNI Library
You can open this sample inside an IDE using the IntelliJ native importer or Eclipse Buildship. |
This sample shows how to build a JNI library with Gradle implemented in Kotlin and C++; however, this applies to other native language as well. The library includes JUnit tests with minimal configuration.
build.gradle
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
id 'dev.nokee.jni-library'
id 'dev.nokee.cpp-language'
}
repositories {
mavenCentral()
}
dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.3.72')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
build.gradle.kts
plugins {
id("org.jetbrains.kotlin.jvm") version "1.3.72"
id("dev.nokee.jni-library")
id("dev.nokee.cpp-language")
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform(kotlin("bom")))
implementation(kotlin("stdlib-jdk8"))
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit"))
}
To build and test the library:
$ ./gradlew build BUILD SUCCESSFUL 7 actionable tasks: 7 executed
The JNI library produced inside ./build/libs
directory:
$ ls ./build/libs/*.jar ./build/libs/kotlin-cpp-jni-library.jar
Since there is only one variant, the native component is included inside the main JAR:
$ jar tf ./build/libs/kotlin-cpp-jni-library.jar META-INF/ META-INF/MANIFEST.MF META-INF/kotlin-cpp-jni-library.kotlin_module com/ com/example/ com/example/greeter/ com/example/greeter/NativeLoader.class com/example/greeter/Greeter.class com/example/greeter/Greeter$Companion.class libkotlin-cpp-jni-library.dylib
For more information, see JNI Library Plugin and C++ Language Plugin reference chapters.