The Konan Plugin is a custom Gradle plugin that facilitates compiling C/C++ sources using the run_konan
utility. It simplifies multi-platform native compilation for Kotlin Native projects.
This plugin is based on awesome work by aSemy 🚀
- Compiles C/C++ source files to
.o
object files. - Supports linking object files into static libraries (
.a
). - Works with all Kotlin Native targets
- Configurable via a Gradle extension.
-
Apply the plugin in your
build.gradle.kts
:plugins { id("io.github.lemcoder.konanplugin") version "1.0.0" }
-
Ensure that the Kotlin/Native toolchain is installed and the
run_konan
utility is accessible.
The plugin provides an extension called KonanPluginExtension
. Below are the configuration options:
Property | Type | Description |
---|---|---|
targets |
List<KonanTarget> |
List of Kotlin/Native targets to compile for. |
sourceDir |
String |
Directory containing C/C++ source files. |
headerDir |
String |
Directory containing .h header files. |
libName |
String |
Name of the output static library. |
outputDir |
String |
Directory for output object files and libraries. |
konanPath |
String |
Path to the run_konan utility. |
After applying the plugin, configure it in your build script using the provided extension:
configure<KonanPluginExtension> {
targets = buildList {
add(KonanTarget.LINUX_X64)
add(KonanTarget.MINGW_X64)
if (System.getProperty("os.name").lowercase().contains("mac")) {
add(KonanTarget.IOS_ARM64)
add(KonanTarget.IOS_SIMULATOR_ARM64)
add(KonanTarget.IOS_X64)
add(KonanTarget.MACOS_X64)
add(KonanTarget.MACOS_ARM64)
}
}
sourceDir = "${rootDir}/native/src"
headerDir = "${rootDir}/native/include"
outputDir = "${rootDir}/native/lib"
libName = "mylib"
konanPath = localKonanDir.listFiles()?.first {
it.name.contains("<LATEST_KOTLIN_VERSION>") // e.g., "2.1.0"
}?.absolutePath
}
The plugin defines a task, runKonanClang
, that:
- Compiles C/C++ source files into
.o
object files. - Links object files into a
.a
static library.
- Place your C/C++ source files in the directory specified by
sourceDir
(e.g.,native/src
). - Place your
.h
headers in the directory specified byheaderDir
(e.g.,native/include
). - Run the build:
./gradlew runKonanClang
The compiled static libraries will be output to outputDir
.
project-root/
├── build.gradle.kts
├── native/
│ ├── src/
│ │ ├── file1.c
│ ├── include/
│ │ ├── file1.h
│ └── lib/ (generated output)
└── settings.gradle.kts
After running the task:
native/lib/
├── linux_x64/
│ └── lib.a
├── mingw_x64/
│ └── lib.a
This project is licensed under the Apache 2.0 License. For more details, see the LICENSE
file.
Happy coding! 😊