A: Define a top level function fun main(args: Array<String>)
or just fun main()
if you are not interested
in passed arguments, please ensure it's not in a package.
Also compiler switch -entry
could be used to make any function taking Array<String>
or no arguments
and return Unit
as an entry point.
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.
A: Use the -produce dynamic
compiler switch, or compilations.main.outputKinds 'DYNAMIC'
in Gradle, i.e.
targets {
fromPreset(presets.iosArm64, 'mylib') {
compilations.main.outputKinds 'DYNAMIC'
}
}
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 compilations.main.outputKinds 'STATIC'
in Gradle, i.e.
targets {
fromPreset(presets.iosArm64, 'mylib') {
compilations.main.outputKinds 'STATIC'
}
}
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.
A: As Kotlin/Native needs to download a platform specific toolchain, you need to specify
-Dhttp.proxyHost=xxx -Dhttp.proxyPort=xxx
as the compiler's or gradlew
arguments,
or set it via the JAVA_OPTS
environment variable.
A: Use the -module_name
compiler option or matching Gradle DSL statement, i.e.
targets {
fromPreset(presets.iosArm64, 'myapp') {
compilations.main.outputKinds 'FRAMEWORK'
compilations.main.extraOpts '-module_name', 'TheName'
}
}
A: Use either -Xembed-bitcode
or -Xembed-bitcode-marker
compiler option
or matching Gradle DSL statement, i.e.
targets {
fromPreset(presets.iosArm64, 'myapp') {
compilations.main.outputKinds 'FRAMEWORK'
compilations.main.extraOpts '-Xembed-bitcode' // for release binaries
// or '-Xembed-bitcode-marker' for debug binaries
}
These options have nearly the same effect as clang's -fembed-bitcode
/-fembed-bitcode-marker
and swiftc's -embed-bitcode
/-embed-bitcode-marker
.
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.
A: We release dev builds frequently, usually at least once a week. You can check the list of available versions. But if we recently fixed an issue and you want to check it before a release is done, you can do:
./gradlew clean
):./gradlew dependencies:update
./gradlew dist distPlatformLibs
You can then set the KONAN_HOME
env variable to the generated dist
folder in the git repository.
# Set with the path of your kotlin-native clone
export KONAN_REPO=$PWD/../kotlin-native
# Run this once since it is costly, you can remove the `clean` task if not big changes were made from the last time you did this
pushd $KONAN_REPO && git pull && ./gradlew clean dependencies:update dist distPlatformLibs && popd
# In your project, you set have to the konan.home property, and include as composite the shared and gradle-plugin builds
./gradlew check -Pkonan.home=$KONAN_REPO/dist --include-build $KONAN_REPO/shared --include-build $KONAN_REPO/tools/kotlin-native-gradle-plugin