Q: What is Kotlin/Native memory management model?

A: Kotlin/Native provides an automated memory management scheme, similar to what Java or Swift provides. The current implementation includes an automated reference counter with a cycle collector to collect cyclical garbage.

Q: How do I create a shared library?

A: Use the -produce dynamic compiler switch, or binaries.sharedLib() in Gradle, i.e.

It will produce a platform-specific shared object (.so on Linux, .dylib on macOS, and .dll on Windows targets) and a C language header, allowing the use of all public APIs available in your Kotlin/Native program from C/C++ code. See samples/python_extension for an example of using such a shared object to provide a bridge between Python and Kotlin/Native.

A: Use the -produce static compiler switch, or binaries.staticLib() in Gradle, i.e.

  1. targets {
  2. fromPreset(presets.iosArm64, 'mylib') {
  3. binaries.staticLib()
  4. }

It will produce a platform-specific static object (.a library format) and a C language header, allowing you to use all the public APIs available in your Kotlin/Native program from C/C++ code.

Q: How do I run Kotlin/Native behind a corporate proxy?

Q: How do I specify a custom Objective-C prefix/name for my Kotlin framework?

A: Use the -module-name compiler option or matching Gradle DSL statement, i.e.

A: Use the baseName option. This will also set the module name.

  1. targets {
  2. fromPreset(presets.iosArm64, 'myapp') {
  3. framework {
  4. baseName = "TheName"
  5. }
  6. }
  7. }

Q: How do I enable bitcode for my Kotlin framework?

A: By default gradle plugin adds it on iOS target.

  • For debug build it embeds placeholder LLVM IR data as a marker.
  • For release build it embeds bitcode as data.

Or commandline arguments: -Xembed-bitcode (for release) and -Xembed-bitcode-marker (debug)

Setting this in a Gradle DSL:

Q: Why do I see InvalidMutabilityException?

A: It likely happens, because you are trying to mutate a frozen object. An object can transfer to the frozen state either explicitly, as objects reachable from objects on which the kotlin.native.concurrent.freeze is called, or implicitly (i.e. reachable from enum or global singleton object - see the next question).

A: Currently, singleton objects are immutable (i.e. frozen after creation), and it’s generally considered good practise to have the global state immutable. If for some reason you need a mutable state inside such an object, use the @konan.ThreadLocal annotation on the object. Also the kotlin.native.concurrent.AtomicReference class could be used to store different pointers to frozen objects in a frozen object and automatically update them.

Q: How can I compile my project against the Kotlin/Native master?

A: One of the following should be done:

For the CLI, you can compile using gradle as stated in the README (and if you get errors, you can try to do a ./gradlew clean):

  1. ./gradlew dist distPlatformLibs

You can then set the `KONAN_HOME` env variable to the generated `dist` folder in the git repository. For Gradle, you can use Gradle composite builds like this: