LoginSignup
12
10

More than 5 years have passed since last update.

[iOS] SwiftからKotlin/Nativeを使う

Posted at

Kotlin/Native v0.5がreleaseされて、C, Objective-C, SwiftからKotlin/Nativeが使えるようになり、UIをSwiftで書き、logicをKotlinで書くということができるようになったので、サンプルをお試し。

ドキュメント

Kotlin Blog - Kotlin/Native v0.5 released: calling Kotlin from Swift and C, LLVM 5 and more
https://blog.jetbrains.com/kotlin/2017/12/kotlinnative-v0-5-released-calling-kotlin-from-swift-and-c-llvm-5-and-more/

GitHub - Kotlin/Native
https://github.com/JetBrains/kotlin-native

Programming Guide - Kotlin/Native
https://kotlinlang.org/docs/reference/native-overview.html

Kotlin/Nativeとは?

  • Kotlin/NativeはKotlinのソースコードからネイティブコードを生成するコンパイラ、ランタイムである。
  • LLVMのバックエンドを使っているので、Kotlin => bitcode => ネイティブコードへ変換される。(Java VMで動くバイトコードが生成されるわけでは無い。)

環境

  • JDK 1.8
  • Xcode 9.2

手順

基本的には、GitHubのREADMEに書かれている手順です。

Kotlin/Nativeのソースを持ってくる。

git clone https://github.com/JetBrains/kotlin-native.git

依存関係のあるものをdownloadする。

./gradlew dependencies:update

コンパイラとライブラリのビルド

コンパイラとライブラリをビルドするには、以下を実行する。
※時間がかなりかかる(README.mdには、MacBook Proで約1時間と書かれている)ので、暫く待つ。

./gradlew bundle

ホストコンパイラとライブラリのみをビルドして時間短縮したい場合には、以下を実行する。

./gradlew dist distPlatformLibs

サンプルを動かす場合には、クロスコンパイラもビルドする必要があるので、以下を実行する。

./gradlew cross_dist

iOS calculator sampleのビルド

  • samples/calculator/calculator.xcodeprojをXcodeで開く。

  • XcodeのメニューからProduct -> Runで実行する。

    • "Run Script" build phaseでgredlewが実行されて、samples/calculator/src/main/kotlin/org/konan/arithmeticparser/Parser.ktがコンパイルされてKotlinArithmeticParser.frameworkが生成される。
12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10