This project demonstrates how to build a Kotlin/Native dynamic library. The example implements an in-memory cookie store, showcasing safe memory management and cross-language data exchange with C and Python.
- C ABI Export: Kotlin functions exposed via
@CName+ manual header (include/cookie_store.h) - Memory Safety: Kotlin-allocated resources must be released using
free_knative_pointer - Protobuf Serialization: Shared
.protoschema for language bindings and data exchange - Example Clients: Ready-to-run demo clients in C and Python
├── src
│ ├── commonMain/kotlin # Common code
│ └── nativeMain/kotlin # Kotlin/Native implementation
│ ├── cookie_store_native.kt # Native API implementation
│ ├── cookie_error_codes.kt # Error codes
│ └── pointer_ext.kt # Pointer helpers
├── include/cookie_store.h # Manually written C header matching Kotlin/Native interface
├── proto/cookie_store.proto # Protobuf schema
├── bindings/c # C demo client
├── bindings/python # Python demo client
├── build.gradle.kts # Gradle build config
└── README.md # Project documentation
⚠️ Note: All API functions that return buffers or error messages allocate memory on the native heap. Always release withfree_knative_pointer.
| Function | Description |
|---|---|
cookie_store_new |
Create a new CookieStore instance. Must be freed with cookie_store_free. |
cookie_store_free |
Free a CookieStore instance created by cookie_store_new. |
cookie_store_set |
Insert or update a cookie from serialized ProtoBuf bytes. |
cookie_store_get_by_domain |
Get cookies by domain. Returns serialized ProtoBuf data. |
cookie_store_remove |
Remove a cookie by name and domain. |
cookie_store_get_all |
Get all cookies in the store. Returns serialized ProtoBuf data. |
cookie_store_clear_all |
Remove all cookies from memory. |
free_knative_pointer |
Free memory allocated by the above functions (buffers or error messages). |
// Kotlin-allocated resources MUST be released via:
free_knative_pointer(ptr);
// Never use system free() directlyApplies to:
- Protobuf data buffers (
unsigned char** outData) - Error message strings (
char** errMsg)
| Code | Meaning |
|---|---|
| COOKIE_SUCCESS | Operation successful |
| COOKIE_NOT_FOUND | No matching cookie found |
| COOKIE_ALLOCATION_FAILED | Memory allocation failed |
| COOKIE_EXCEPTION | Unexpected exception occurred |
- Kotlin/Native + Gradle + JDK 17
- CMake 4.1.0+ (for C demo)
- Python 3.12.8 with
protobuf 3.20.1
⚠️ Note: Protobuf files are already generated. No need to runprotoc.
- Linux (x64):
./gradlew linkReleaseSharedLinuxX64- Linux (ARM64):
./gradlew linkReleaseSharedLinuxArm64- macOS (Intel):
./gradlew linkReleaseSharedMacosX64- macOS (Apple Silicon):
./gradlew linkReleaseSharedMacosArm64- Windows (x64):
./gradlew linkReleaseSharedMingwX64The resulting .so / .dylib / .dll will be under:
build/bin/<target>/releaseShared/
Where <target> ∈ { linuxX64, linuxArm64, macosX64, macosArm64, mingwX64 }.
cd bindings/c
mkdir build && cd build
cmake ..
make
./cookie_store_democd bindings/python
python3 cookie_store_demo.py- Protobuf introduces at least one memory copy between Kotlin and C.
- Direct C structs would be faster but less portable.
- This project is for demonstration only — production use requires additional safety and error handling.