Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine. It is an LLVM based backend for the Kotlin compiler and native implementation of the Kotlin standard library.
Kotlin/Native目前虽然是Beta版,但是已经比较稳定,是目前最靠谱的跨平台技术之一。
KN的安装过程非常简单,本文将基于Linux/MacOS环境安装KN的过程做可一个简单的整理,详细步骤如下:
安装JVM
Kotlin的编译离不开JVM,所以安装JVM是前提条件。JVM的安装想必大家都很清楚,这里就不具体介绍了。
安装Kotlin/Native
https://github.com/JetBrains/kotlin/releases/latest
从官方下载KN最新版本,下载需要使用wget
mac
$ brew install wget
linux(ubuntu)
$ sudo apt install wget
然后使用wget下载KN
mac
$ wget https://github.com/JetBrains/kotlin/releases/download/v1.3.61/kotlin-native-macos-1.3.61.tar.gz
linux(ubuntu)
$ wget https://github.com/JetBrains/kotlin/releases/download/v1.3.61/kotlin-native-linux-1.3.61.tar.gz
下载完成后,解压并移动到谁当位置,我们选择路径/usr/local/kotlin-native
$ tar xzvf kotlin-native-macos-1.3.61.tar.gz
$ sudo mkdir -p /usr/local/kotlin-native
$ sudo mv kotlin-native-macos-1.3.61/* /usr/local/kotlin-native
配置环境变量,~/.bash_profile
中添加如下
$ export PATH=$PATH:/usr/local/kotlin-native/bin/
安装 IntelliJ IDEA
虽然也可以使用别的IDE进行开发,但是同属Jetbrains家族的产品,对KN的支持自然最到位。天然集成gradle也会为我们在开发中对于各种依赖库的管理提供方便。
请到IntelliJ IDEA官网自行下载安装即可,过程非常简单
Hello world
打开IDEA,写下第一行KN代码
fun main(args: Array<String>) {
println("Hello, World!")
}
编译
初次运行时需要下载和安装LLVM所依赖的各个package,会稍微花点时间
$ kotlinc-native hello.kt -o hello
运行
$ ./hello.kexe
Hello, World!
程序顺利执行!
编译Library
通过-p
参数,可以编译成任意形式的Library
$ kotlinc-native hello.kt -p dynamic
-p 参数 | lib类型 |
---|---|
program | 可执行二进制 |
static | .a文件 |
dynamic | Linux下生成.so, macOS下生成.dylib |
framework | iOS/macOS所需的framework |
library | klib(Kotlin库) |
bitcode | bc文件(LLVM的Bitcodebc) |
可以通过-h
查询KN更多参数的使用方法
$ kotlinc-native -h
Usage: kotlinc-native <options> <source files>
where possible options include:
-g Enable emitting debug information
-enable-assertions (-ea) Enable runtime assertions in generated code
-friend-modules <path> Paths to friend modules
-generate-no-exit-test-runner (-trn)
Produce a runner for unit tests not forcing exit
-generate-test-runner (-tr) Produce a runner for unit tests
-generate-worker-test-runner (-trw)
Produce a worker runner for unit tests
-include-binary (-ib) <path> Pack external binary within the klib
-library (-l) <path> Link with the library
-library-version (-lv) <version>
Set library version
-linker-options <arg> Pass arguments to linker
-list-targets List available hardware targets
-entry (-e) <name> Qualified entry point name
-manifest <path> Provide a maniferst addend file
-memory-model <model> Memory model to use, 'strict' and 'relaxed' are currently supported
-module-name <name> Specify a name for the compilation module
-native-library (-nl) <path> Include the native bitcode library
-no-default-libs Don't link the libraries from dist/klib automatically
-no-endorsed-libs Don't link the endorsed libraries from dist automatically
-nomain Assume 'main' entry point to be provided by external libraries
-nopack Don't pack the library into a klib file
-nostdlib Don't link with stdlib
-opt Enable optimizations during compilation
-output (-o) <name> Output name
-produce (-p) {program|static|dynamic|framework|library|bitcode}
Specify output file kind
-repo (-r) <path> Library search path
-linker-option <arg> Pass argument to linker
-target <target> Set hardware target
-Werror Report an error if there are any warnings
-api-version <version> Allow to use declarations only from the specified version of bundled libraries
-X Print a synopsis of advanced options
-help (-h) Print a synopsis of standard options
-kotlin-home <path> Path to Kotlin compiler home directory, used for runtime libraries discovery
-language-version <version> Provide source compatibility with specified language version
-P plugin:<pluginId>:<optionName>=<value>
Pass an option to a plugin
-progressive Enable progressive compiler mode.
In this mode, deprecations and bug fixes for unstable code take effect immediately,
instead of going through a graceful migration cycle.
Code written in the progressive mode is backward compatible; however, code written in
non-progressive mode may cause compilation errors in the progressive mode.
-nowarn Generate no warnings
-verbose Enable verbose logging output
-version Display compiler version
@<argfile> Read compiler arguments and file paths from the given file
最后
编译过程感觉有些慢,据说Kotlin1.4对KN的编译有很多优化,值得期待。