Kotlin Native 笔记

Posted by API Caller on July 17, 2020, last modified on July 19, 2020

今天的云, 比你好看

拖延了好久, 了解一下 Kotlin Native, 感觉比其它方案更简单直接一些.

Summary

1
2
3
4
5
6
7
8
9
10
Kotlin/Native supports the following platforms:

  iOS (arm32, arm64, simulator x86_64)
  macOS (x86_64)
  watchOS (arm32, arm64, x86)
  tvOS (arm64, x86_64)
  Android (arm32, arm64, x86, x86_64)
  Windows (mingw x86_64, x86)
  Linux (x86_64, arm32, arm64, MIPS, MIPS little endian)
  WebAssembly (wasm32)
1
2
3
4
5
6
7
8
9
10
Kotlin/Native supports two-way interoperability with the Native world. On the one hand, the compiler creates:

  an executable for many platforms
  a static library or dynamic library with C headers for C/C++ projects
  an Apple framework for Swift and Objective-C projects

On the other hand, Kotlin/Native supports interoperability to use existing libraries directly from Kotlin/Native:

  static or dynamic C Libraries
  C, Swift, and Objective-C frameworks
  • 有活力, 有商业公司支持, 平台支持全
  • 支持 Java 8, 所以在直接复用代码方面或许有优势

Helloworld

Command Line

target

1
2
3
4
5
6
7
8
9
10
11
12
konanc -list-targets 

# linux_x64:                    (default) linux
# linux_arm32_hfp:                        raspberrypi
# linux_arm64:
# linux_mips32:
# linux_mipsel32:
# android_x86:
# android_x64:
# android_arm32:
# android_arm64:
# wasm32:
fun main() {
    println("Hello Kotlin/Native!")
}
1
2
kotlinc-native -o hello hello.kt
kotlinc-native -produce program -target linux -o hello hello.kt
package example

object Object {
    val field = "A"
}

class Clazz {
    fun memberFunction(p: Int): ULong = 42UL
}

fun forIntegers(b: Byte, s: Short, i: UInt, l: Long) { }
fun forFloats(f: Float, d: Double) { }

@CName("strings")
fun strings(str: String) : String? {
    return "That is '$str' from C"
}

val globalString = "A global String"
1
kotlinc-native -produce dynamic -target linux -o dynamic dynamic.kt

Gradle

Ref