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
.oobject files. - Supports linking object files into static libraries (
.a). - Works with all Kotlin Native targets (passed as simple strings).
- Zero runtime dependencies (only requires Gradle API).
- Support for custom compiler arguments.
- Configurable via a Gradle extension.
-
Apply the plugin in your
build.gradle.kts:plugins { id("io.github.lemcoder.konanplugin") version "1.1.0" } -
Ensure that the Kotlin/Native toolchain is installed and the
run_konanutility is accessible.
The plugin provides an extension called konanConfig. Below are the configuration options:
| Property | Type | Description |
|---|---|---|
targets |
List<String> |
List of Kotlin/Native target names to compile for (e.g., "linux_x64", "mingw_x64"). |
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. |
additionalCompilerArgs |
List<String> |
Additional compiler arguments to pass to clang. |
After applying the plugin, configure it in your build script using the konanConfig extension:
konanConfig {
targets.addAll("linux_x64", "mingw_x64")
// Optionally add platform-specific targets
if (System.getProperty("os.name").lowercase().contains("mac")) {
targets.addAll(
"ios_arm64",
"ios_simulator_arm64",
"ios_x64",
"macos_x64",
"macos_arm64"
)
}
sourceDir.set("${rootDir}/native/src")
headerDir.set("${rootDir}/native/include")
outputDir.set("${rootDir}/native/lib")
libName.set("mylib")
konanPath.set(
localKonanDir.listFiles()?.first {
it.name.contains("<LATEST_KOTLIN_VERSION>") // e.g., "2.1.0"
}?.absolutePath
)
// Optional: Add custom compiler arguments
additionalCompilerArgs.addAll("-O2", "-Wall")
}The plugin defines a task, runKonanClang, that:
- Compiles C/C++ source files into
.oobject files. - Links object files into a
.astatic library.
- Place your C/C++ source files in the directory specified by
sourceDir(e.g.,native/src). - Place your
.hheaders 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! 😊