Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Multiplatform Project入門/Introduction-Kot...

AAkira
August 24, 2019

Kotlin Multiplatform Project入門/Introduction-Kotlin-MPP

Introduction of Kotlin Multiplatform Project.

This is talked in Kotlin Fest 2019.
(https://kotlin.connpass.com/event/129860/)

Example code: https://github.com/mpp-example

AAkira

August 24, 2019
Tweet

More Decks by AAkira

Other Decks in Technology

Transcript

  1. Kotlin Multiplatform Projectと私 • Kotlin/Nativeチュートリアル Android, iOS編
 http://aakira.app/blog/2018/10/kotlin-native • Kotlin

    Multiplatform構想 ~今やる理由編~
 https://aakira.app/blog/2018/12/kotlin-mpp-reason • Kotlin Multiplatform構想 ~設計編~
 https://aakira.app/blog/2018/12/kotlin-mpp-architecture • Kotlin Multiplatform環境でKotlin Serializationと
 Android ExtensionsのParcelize Annotationを使う
 http://aakira.app/blog/2018/12/kotlin-mpp-android-parcelable • NapierというKotlin Multiplatform用のログライブラリを作った
 https://aakira.app/blog/2019/02/napier
  2. Agenda 1. Kotlin Multiplatform Projectとは 1.1. Kotlin Multipratform Projectについて 1.2.

    Kotlin/Native 1.3. Kotlin/JS 1.4. Kotlin Multiplatform Projectの仕組み 1.5. メリット、デメリット 2. 実践Kotlin Multiplatform Project 2.1. ライブラリ選定 2.2. 設計を考える 2.3. サンプルアプリを作る 3. まとめ
  3. UI含む全てのコードを共通化(独自UI) • PhoneGap(Cordova)
 - HTML & JS based
 - Adobe,

    Apache • Flutter
 - Dart
 - Google
 - OpenGLを使って低レイヤにUIを描画
  4. ロジックのみ • Java2ObjC
 - Java
 - Google
 - Kotlin/Nativeに置き換えられそう •

    Xamarin(Native)
 - C#等の.NET言語
 - Microsoft •Kotlin Multiplatform Project
  5. プラットフォーム 対応アーキテクチャ iOS arm32, arm64, simulator x86/64 MacOS x86_64 Android

    NDK arm32, arm64 Windows mingw x86_64, x86 Linux x86_64, arm32, arm64, MIPS,
 MIPS little endian, Raspberry Pi WebAssembly wasm32 Kotlin/Native https://kotlinlang.org/docs/reference/native-overview.html#target-platforms
  6. iOS arm32, arm64, simulator x86/64 MacOS x86_64 ndroid NDK arm32,

    arm64 Windows mingw x86_64, x86 Linux x86_64, arm32, arm64, MIPS,
 MIPS little endian, Raspberry Pi ebAssembly wasm32 Kotlin/Native
  7. Kotlin/Native - 歴史 Version 日付 主な変更 Kotlin Version 0.1 2017/04

    Early Previewリリース 1.1.x 0.2 2017/05 Coroutineのサポート 1.1.x 0.3 2017/06 Android, Windowsのサポート 1.1.x 0.4 2017/10 iOS, macOSのサポート 1.2 0.5 2017/12 Objective-C, SwiftからのKotlin呼び出し 1.2 0.6 2018/02 Kotlin Multiplatform対応 1.2.20 0.7 2018/02 Objective-C, Swiftの相互運用性、multithreadまわりの強化 1.2.x 0.8 2018/07 stdlibがKotlin/JVMとKotlin/JSに, ios Arm32サポート 1.2.x 0.9 2018/09 安定版Coroutine等 主にKotlin1.3のアップデートに追従 1.3.M2 Beta 2018/09 Kotlin1.3リリースに伴いBeta版に 1.3 1.1.0 2018/12 パフォーマンス改善, Contracts対応 1.3 1.2.0(1) 2019/04 Windows32bitサポート, WindowsとMacからLinuxへのクロスコンパイル 1.3.30 1.3.0 2019/06 Linux Arm64サポート, メモリ管理の大幅改善 1.3.40
  8. Kotlin/Native - 互換性 ,PUMJO 4XJGU 0CKFDUJWF$ class class @interface interface

    protocol @protocol @Throws throws error:(NSError**)error null nil nil Unit return type Void void String String NSString String NSMutableString NSMutableString List Array NSArray MutableList NSMutableArray NSMutableArray Set Set NSSet MutableSet NSMutableSet NSMutableSet Map Dictionary NSDictionary MutableMap NSMutableDictionary NSMutableDictionary https://kotlinlang.org/docs/reference/native/objc_interop.html
  9. Kotlin/Native - 互換性なし • suspend関数(Coroutine) • inlineクラス • Kotlinのコレクションインタフェースを実装した独自クラス •

    Objective-Cのクラスを継承したKotlinのクラス IUUQTHJUIVCDPN+FU#SBJOTLPUMJOOBUJWFCMPCNBTUFS0#+$@*/5&301NEVOTVQQPSUFE
  10. Kotlin/Native - Freeze Actual.kt ViewController.swift class Hoge {} fun foo(block:

    (Hoge) -> Unit) { val hoge = Hoge() block(hoge) } ActualKt.foo { hoge in DispatchQueue.global(qos: .background).async { print(hoge) } }
  11. Kotlin/Native - Freeze Actual.kt ViewController.swift class Hoge {} fun foo(block:

    (Hoge) -> Unit) { val hoge = Hoge() block(hoge) } ActualKt.foo { hoge in DispatchQueue.global(qos: .background).async { print(hoge) } }
  12. class Hoge {} fun foo(block: (Hoge) -> Unit) { val

    hoge = Hoge() block(hoge) } Kotlin/Native - Freeze Actual.kt ViewController.swift ActualKt.foo { hoge in DispatchQueue.global(qos: .background).async { print(hoge) } } kotlin.native.IncorrectDereferenceException
  13. Kotlin/Native - Freeze Actual.kt ViewController.swift ActualKt.foo { hoge in DispatchQueue.global(qos:

    .background).async { print(hoge) } } class Hoge {} fun foo(block: (Hoge) -> Unit) { val hoge = Hoge() block(hoge) } Immutableを保証
  14. Kotlin/Native - Freeze Actual.kt ViewController.swift ActualKt.foo { hoge in DispatchQueue.global(qos:

    .background).async { print(hoge) } } class Hoge {} fun foo(block: (Hoge) -> Unit) { val hoge = Hoge().freeze() block(hoge) } Immutableを保証
  15. fun bar(block: (Int) -> Unit) { val hoge = 46

    block(hoge) } Kotlin/Native - Freeze Actual.kt ViewController.swift ActualKt.bar { hoge in DispatchQueue.global(qos: .background).async { print(hoge) } }
  16. fun bar(block: (Int) -> Unit) { val hoge = 46

    block(hoge) } Kotlin/Native - Freeze Actual.kt ViewController.swift ActualKt.bar { hoge in DispatchQueue.global(qos: .background).async { print(hoge) } } Primitive型は問題ない
  17. Kotlin/Native - 成果物 成果物 詳細 EXECUTABLE 実行ファイル KLIBRARY Kotlin/Native library(*.klib)

    FRAMEWORK Objective-Cのフレームワーク(*.framework) DYNAMIC 動的リンクライブラリ STATIC 静的リンクライブラリ https://kotlinlang.org/docs/reference/native-overview.html
  18. Kotlin/JS - DOM /web/build/mpp-web.js (function (root, factory) { if (typeof

    define === 'function' && define.amd) define(['exports', 'kotlin'], factory); else if (typeof exports === 'object') factory(module.exports, require('kotlin')); else { if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'mpp-web'. Its dependency 'kotlin' was not found. Please,
 check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} : this['mpp-web'], kotlin); } }(this, function (_, Kotlin) { 'use strict'; function main() { var tmp$; (tmp$ = document.body) != null ? (tmp$.textContent = 'Hello world!') : null; } var package$com = _.com || (_.com = {}); var package$aakira = package$com.aakira || (package$com.aakira = {}); var package$mpp = package$aakira.mpp || (package$aakira.mpp = {}); var package$web = package$mpp.web || (package$mpp.web = {}); package$web.main = main; main(); return _; }));
  19. Kotlin/JS - DOM (function (root, factory) { if (typeof define

    === 'function' && define.amd) define(['exports', 'kotlin'], factory); else if (typeof exports === 'object') factory(module.exports, require('kotlin')); else { if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'mpp-web'. Its dependency 'kotlin' was not found. Please,
 check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} : this['mpp-web'], kotlin); } }(this, function (_, Kotlin) { 'use strict'; function main() { var tmp$; (tmp$ = document.body) != null ? (tmp$.textContent = 'Hello world!') : null; } var package$com = _.com || (_.com = {}); var package$aakira = package$com.aakira || (package$com.aakira = {}); var package$mpp = package$aakira.mpp || (package$aakira.mpp = {}); var package$web = package$mpp.web || (package$mpp.web = {}); package$web.main = main; main(); return _; })); /web/build/mpp-web.js
  20. throw new Error("Error loading module 'mpp-web'. Its dependency 'kotlin' was

    not found. Please, check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} : this['mpp-web'], kotlin); } }(this, function (_, Kotlin) { 'use strict'; function main() { var tmp$; (tmp$ = document.body) != null ? (tmp$.textContent = 'Hello world!') : null; } var package$com = _.com || (_.com = {}); Kotlin/JS - DOM /web/build/mpp-web.js
  21. throw new Error("Error loading module 'mpp-web'. Its dependency 'kotlin' was

    not found. Please, check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} : this['mpp-web'], kotlin); } }(this, function (_, Kotlin) { 'use strict'; function main() { var tmp$; (tmp$ = document.body) != null ? (tmp$.textContent = 'Hello world!') : null; } var package$com = _.com || (_.com = {}); Kotlin/JS - DOM /web/build/mpp-web.js
  22. Kotlin/JS - DOM fun main() { document.body?.textContent = "Hello world!"

    } function main() { var tmp$; (tmp$ = document.body) != null ? (tmp$.textContent = 'Hello world!') : null; }
  23. Kotlin/JS - external external class Logger { companion object {

    fun log(log: String) } } fun main() { Logger.log("Hello logger world!") } /web/Main.kt
  24. Kotlin/JS - external external class Logger { companion object {

    fun log(log: String) } } fun main() { Logger.log("Hello logger world!") } JavaScriptのライブラリ /web/Main.kt
  25. Kotlin/JS - external external class Logger { companion object {

    fun log(log: String) } } fun main() { Logger.log("Hello logger world!") } external修飾子 /web/Main.kt
  26. Kotlin/JS - external external class Logger { companion object {

    fun log(log: String) } } fun main() { Logger.log("Hello logger world!") } 生成されるJavaScriptはどうなるか /web/Main.kt
  27. Kotlin/JS - external (function (root, factory) { if (typeof define

    === 'function' && define.amd) define(['exports', 'kotlin'], factory); else if (typeof exports === 'object') factory(module.exports, require('kotlin')); else { if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'mpp-web'. Its dependency 'kotlin' was not found. Please,
 check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} : this['mpp-web'], kotlin); } }(this, function (_, Kotlin) { 'use strict'; function main() { Logger.log('Hello logger world!'); } var package$com = _.com || (_.com = {}); var package$aakira = package$com.aakira || (package$com.aakira = {}); var package$mpp = package$aakira.mpp || (package$aakira.mpp = {}); var package$web = package$mpp.web || (package$mpp.web = {}); package$web.main = main; main(); return _; })); /web/build/mpp-web.js
  28. (function (root, factory) { if (typeof define === 'function' &&

    define.amd) define(['exports', 'kotlin'], factory); else if (typeof exports === 'object') factory(module.exports, require('kotlin')); else { if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'mpp-web'. Its dependency 'kotlin' was not found. Please,
 check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} : this['mpp-web'], kotlin); } }(this, function (_, Kotlin) { 'use strict'; function main() { Logger.log('Hello logger world!'); } var package$com = _.com || (_.com = {}); var package$aakira = package$com.aakira || (package$com.aakira = {}); var package$mpp = package$aakira.mpp || (package$aakira.mpp = {}); var package$web = package$mpp.web || (package$mpp.web = {}); package$web.main = main; main(); return _; })); Kotlin/JS - external /web/build/mpp-web.js
  29. if (typeof kotlin === 'undefined') { throw new Error("Error loading

    module 'mpp-web'. Its dependency 'ko check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} } }(this, function (_, Kotlin) { 'use strict'; function main() { Logger.log('Hello logger world!'); } var package$com = _.com || (_.com = {}); var package$aakira = package$com.aakira || (package$com.aakira = {}); var package$mpp = package$aakira.mpp || (package$aakira.mpp = {}); var package$web = package$mpp.web || (package$mpp.web = {}); package$web.main = main; main(); return _; Kotlin/JS - external /web/build/mpp-web.js
  30. if (typeof kotlin === 'undefined') { throw new Error("Error loading

    module 'mpp-web'. Its dependency 'ko check whether 'kotlin' is loaded prior to 'mpp-web'."); } root['mpp-web'] = factory(typeof this['mpp-web'] === 'undefined' ? {} } }(this, function (_, Kotlin) { 'use strict'; function main() { Logger.log('Hello logger world!'); } var package$com = _.com || (_.com = {}); var package$aakira = package$com.aakira || (package$com.aakira = {}); var package$mpp = package$aakira.mpp || (package$aakira.mpp = {}); var package$web = package$mpp.web || (package$mpp.web = {}); package$web.main = main; main(); return _; Kotlin/JS - external /web/build/mpp-web.js ラップしたLoggerは生成されていない
  31. Kotlin/JS - dynamic /web/Main.kt external class Logger { val hoge:

    dynamic fun log(log: String) } fun main() { val logger = Logger() val hoge = logger.hoge as String logger.log(hoge) }
  32. Kotlin/JS - dynamic /web/Main.kt external class Logger { val hoge:

    dynamic fun log(log: String) } fun main() { val logger = Logger() val hoge = logger.hoge as String logger.log(hoge) }
  33. Kotlin/JS - dynamic /web/Main.kt external class Logger { val hoge:

    dynamic fun log(log: String) } fun main() { val logger = Logger() val hoge = logger.hoge as String logger.log(hoge) }
  34. Kotlin/JS - dynamic /web/Main.kt external class Logger { val hoge:

    dynamic fun log(log: String) } fun main() { val logger = Logger() val hoge = logger.hoge as String logger.log(hoge) }
  35. Kotlin/JS - dynamic /web/Main.kt external class Logger { val hoge:

    dynamic fun log(log: String) } fun main() { val logger = Logger() val hoge = logger.hoge as String logger.log(hoge) } var hoge = typeof (tmp$ = logger.hoge) === 'string' ? tmp$ : throwCCE(); /web/build/mpp-web.js
  36. Kotlin/JS - dynamic /web/Main.kt external class Logger { val hoge:

    dynamic fun log(log: String) } fun main() { val logger = Logger() val hoge = logger.hoge as String logger.log(hoge) } var hoge = typeof (tmp$ = logger.hoge) === 'string' ? tmp$ : throwCCE(); /web/build/mpp-web.js
  37. Kotlin/JS - 成果物 成果物 詳細 plain グローバルスコープに定義される
 デフォルトはPlainになっている amd 主にクライアントサイドで使われる


    非同期にロードしやすい commonjs Node.jsなどサーバサイドで使われる事が多い umd AMDとCommonJSの両方をサポートしている https://kotlinlang.org/docs/reference/js-modules.html
  38. MPP(クロスプラットフォーム)のメリット • 認証系を共通化 • ログ送信基盤を共通化 • 広義の意味でのUtilityを共通化 ロジックの共通化が可能 PM <

    Dimension(Key)の値が
 register-userとregistered-user
 の2つあるのですが...? iOSer, Webmen < registered-userやろ!
 Androider < あっ...ほんま...
 ごめんて...
  39. MPP(クロスプラットフォーム)のメリット • 認証系を共通化 • ログ送信基盤を共通化 • 広義の意味でのUtilityを共通化 ロジックの共通化が可能 QA <

    Androidは "残り1分"
 iOSは "残り60秒"
 と表示されるのですが...? iOSer < どっちも正しい!!!
 Androider < どっちも正しい!!!
  40. MPPのメリット • Android, サーバで広く使われているKotlinを使用できる • Andorid, iOSのコードだけでなく、
 Web(JavaScript, wasm), サーバ

    のコードまでも共有することが可能 • 最初にGradle等の設定さえすれば、新しくフレームワークの記法を
 覚える必要がない • 他のクロスプラットフォームツールではAndroidの方がバグが多いが、
 Kotlin/NativeではAndroid側が今までと変わらず開発出来る
  41. MPPの仕組み • Android, Server
 JVM言語でGradle使っていれば、通常の外部ライブラリと同じ • iOS
 .frameworkを作成してXcodeで読み込む • WEB


    生成されたJavaScriptファイルを読み込む 共通モジュールで生成された成果物を 各プラットフォームから参照する
  42. ExpectとActual /common/src/androidMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Android!" /common/src/iosMain/kotlin/com/github/mpp/common/Actual.kt actual

    fun platformString() = "Hello iOS!" /common/src/jsMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Web!" /common/src/jvmMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Server!"
  43. ExpectとActual /common/src/androidMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Android!" /common/src/iosMain/kotlin/com/github/mpp/common/Actual.kt actual

    fun platformString() = "Hello iOS!" /common/src/jsMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Web!" /common/src/jvmMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Server!"
  44. ExpectとActual /common/src/androidMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Android!" /common/src/iosMain/kotlin/com/github/mpp/common/Actual.kt actual

    fun platformString() = "Hello iOS!" /common/src/jsMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Web!" /common/src/jvmMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Server!"
  45. ExpectとActual /common/src/androidMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Android!" /common/src/iosMain/kotlin/com/github/mpp/common/Actual.kt actual

    fun platformString() = "Hello iOS!" /common/src/jsMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Web!" /common/src/jvmMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Server!"
  46. ExpectとActual /common/src/androidMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Android!" /common/src/iosMain/kotlin/com/github/mpp/common/Actual.kt actual

    fun platformString() = "Hello iOS!" /common/src/jsMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Web!" /common/src/jvmMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Server!"
  47. ExpectとActual /common/src/androidMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Android!" /common/src/iosMain/kotlin/com/github/mpp/common/Actual.kt actual

    fun platformString() = "Hello iOS!" /common/src/jsMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Web!" /common/src/jvmMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Server!"
  48. ExpectとActual /common/src/androidMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Android!" /common/src/iosMain/kotlin/com/github/mpp/common/Actual.kt actual

    fun platformString() = "Hello iOS!" /common/src/jsMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Web!" /common/src/jvmMain/kotlin/com/github/mpp/common/Actual.kt actual fun platformString() = "Hello Server!" それぞれのディレクトリに定義が必要
  49. ライブラリ選定 ジャンル ライブラリ URL )551 LUPS DMJFOU IUUQTHJUIVCDPNLUPSJPLUPS 4FSJBMJ[FS LPUMJOTFSJBMJ[BUJPO

    IUUQTHJUIVCDPNLPUMJOLPUMJOYTFSJBMJ[BUJPO 3%# 42-%FMJHIU IUUQTHJUIVCDPNTRVBSFTRMEFMJHIU ,74 NVMUJQMBUGPSNTFUUJOHT IUUQTHJUIVCDPNSVTTIXPMGNVMUJQMBUGPSNTFUUJOHT %* ,PEFJO IUUQTHJUIVCDPN,PEFJO'SBNFXPSL,PEFJO%* *0 ,PUMJO*0 IUUQTHJUIVCDPN,PUMJOLPUMJOYJP %BUF ,MPDL IUUQTHJUIVCDPNLPSMJCTLMPDL -PHHFS /BQJFS IUUQTHJUIVCDPN""LJSB/BQJFS
  50. Kotlin Multiplatform 7JFX 7JFX.PEFM 3FQPTJUPSZ %# "1*$MJFOU
 H31$FUDʜ 3FQPTJUPSZ -PDBM

    $BDIF "1*$MJFOU
 )551FUDʜ 4FSWJDF 4FSWJDF Platform Reactive Stream Coroutine
  51. 設計を考える - Server • 現状MPPで全て作るならKtor一択 • Domain Objectだけを共有するなら
 Spring Bootとかでも良いかも

    • マイクロサービス構成なら
 BFF(Backends For Frontends)サーバを作るのがオススメ
  52. ライブラリ ジャンル ライブラリ URL 4FSWFS 'SBNFXPSL ,UPSTFSWFS IUUQTHJUIVCDPNLUPSJPLUPS )551$MJFOU ,UPSDMJFOU

    IUUQTHJUIVCDPNLUPSJPLUPS 4FSJBMJ[FS LPUMJOTFSJBMJ[BUJPO IUUQTHJUIVCDPNLPUMJOLPUMJOYTFSJBMJ[BUJPO "TZOD $PSPVUJOF IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT
  53. MPP - パッケージ構成 ├── android │ ├── build.gradle │ └──

    src │ └── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── jsMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle ├── gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── ios ├── js │ └── build.gradle ├── server │ └── build.gradle └── settings.gradle
  54. MPP - パッケージ構成 ├── android │ ├── build.gradle │ └──

    src │ └── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── jsMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle ├── gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── ios ├── js │ └── build.gradle ├── server │ └── build.gradle └── settings.gradle
  55. ├── android │ ├── build.gradle │ └── src │ └──

    main │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle MPP - パッケージ構成
  56. │ │ └── Actual.kt │ └── jvmMain │ └── kotlin

    │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle ├── gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── ios ├── js │ └── build.gradle ├── server │ └── build.gradle └── settings.gradle MPP - パッケージ構成
  57. │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp

    │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── jsMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle MPP - パッケージ構成
  58. │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp

    │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── jsMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle MPP - パッケージ構成
  59. │ │ └── MainActivity.kt │ └── res ├── build.gradle ├──

    common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common MPP - パッケージ構成
  60. │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├──

    jsMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle ├── gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── ios ├── js │ └── build.gradle ├── server │ └── build.gradle MPP - パッケージ構成
  61. │ │ └── MainActivity.kt │ └── res ├── build.gradle ├──

    common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin DD MPP - パッケージ構成
  62. │ │ └── MainActivity.kt │ └── res ├── build.gradle ├──

    common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin MPP - パッケージ構成
  63. │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │

    └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── jsMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle ├── gradle ├── gradle.properties ├── gradlew ├── gradlew.bat MPP - パッケージ構成
  64. │ │ └── MainActivity.kt │ └── res ├── build.gradle ├──

    common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin MPP - パッケージ構成
  65. MPP - Gradle設定 apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin

    { android() iosArm64('ios') { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs implementation rootProject.ext.serializationJs implementation rootProject.ext.ktorClientJs implementation rootProject.ext.ktorClientJsonJs } } jvmMain { dependencies { implementation rootProject.ext.kotlinJvm implementation rootProject.ext.coroutine implementation rootProject.ext.serialization implementation rootProject.ext.ktorClientJvm implementation rootProject.ext.ktorClientJsonJvm } } } } /common/build.gradle
  66. MPP - Gradle設定 /common/build.gradle apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle'

    kotlin { android() iosArm64('ios') { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon
  67. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 MPP用プラグインをApply /common/build.gradle
  68. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 Kotlin1.3.0より前 • kotlin-platform-common • kotlin-platform-android • org.jetbrains.kotlin.platform.native • kotlin-platform-js /common/build.gradle
  69. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 Tips: Android用のgradleは別で定義 /common/build.gradle
  70. apply plugin: 'kotlin-multiplatform' apply plugin: 'com.android.library' android { compileSdkVersion 28

    buildToolsVersion "28.0.3" defaultConfig { minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0.0" } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } kotlin { android() MPP - Gradle設定 /common/build.gradle Android用の定義を書かなければならない
  71. apply plugin: 'kotlin-multiplatform' apply plugin: 'com.android.library' android { compileSdkVersion 28

    buildToolsVersion "28.0.3" defaultConfig { minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0.0" } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } kotlin { android() MPP - Gradle設定 /common/build.gradle Android用の定義を書かなければならない
  72. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs implementation rootProject.ext.serializationJs implementation rootProject.ext.ktorClientJs implementation rootProject.ext.ktorClientJsonJs } } jvmMain { dependencies { implementation rootProject.ext.kotlinJvm implementation rootProject.ext.coroutine implementation rootProject.ext.serialization implementation rootProject.ext.ktorClientJvm implementation rootProject.ext.ktorClientJsonJvm } } } } apply plugin: 'com.android.library' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 buildToolsVersion "28.0.3" defaultConfig { minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0.0" } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), ɹɹɹɹɹɹɹɹɹ'proguard-rules.pro' } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" } MPP - Gradle設定 /common/android.gradle /common/build.gradle
  73. apply plugin: 'kotlin-multiplatform' apply plugin: 'com.android.library' android { compileSdkVersion 28

    buildToolsVersion "28.0.3" defaultConfig { minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0.0" } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } kotlin { android() MPP - Gradle設定 /common/build.gradle
  74. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 /common/build.gradle
  75. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP用Platform Pluginを読み込み MPP - Gradle設定 /common/build.gradle
  76. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 /common/build.gradle
  77. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() if

    (project.findProperty("device")?.toBoolean() ?: false) { iosArm64('ios') { binaries { framework() } } } else { iosX64('ios') { binaries { framework() } } } MPP - Gradle設定 /common/build.gradle
  78. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() if

    (project.findProperty("device")?.toBoolean() ?: false) { iosArm64('ios') { binaries { framework() } } } else { iosX64('ios') { binaries { framework() } } } MPP - Gradle設定 /common/build.gradle
  79. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() if

    (project.findProperty("device")?.toBoolean() ?: false) { iosArm64('ios') { binaries { framework() } } } else { iosX64('ios') { binaries { framework() } } } MPP - Gradle設定 /common/build.gradle
  80. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 /common/build.gradle
  81. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 /common/build.gradle
  82. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() { browser() } sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon MPP - Gradle設定 /common/build.gradle
  83. apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android() iosArm64('ios')

    { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定 /common/build.gradle
  84. } } jvm() js() sourceSets { commonMain { dependencies {

    implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } MPP - Gradle設定 /common/build.gradle 依存関係の定義
  85. } } jvm() js() sourceSets { commonMain { dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.2.2" implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.1" implementation "io.ktor:ktor-client-core:1.2.2" implementation "io.ktor:ktor-client-gson:1.2.2" } } androidMain.dependencies { } iosMain { dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.2.2" implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.11.1" implementation "io.ktor:ktor-client-ios:1.2.2" implementation "io.ktor:ktor-client-json-native:1.2.2" } MPP - Gradle設定 /common/build.gradle ルートにまとめて定義
  86. } } jvm() js() sourceSets { commonMain { dependencies {

    implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } 共通モジュールの依存定義 MPP - Gradle設定 /common/build.gradle
  87. /common/build.gradle commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation

    rootProject.ext.serializationCommon implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs MPP - Gradle設定
  88. commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon

    implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs /common/build.gradle MPP - Gradle設定
  89. /common/build.gradle apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin { android()

    iosArm64('ios') { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon 名前を指定している MPP - Gradle設定
  90. /common/build.gradle commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation

    rootProject.ext.serializationCommon implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs 同じ名前 MPP - Gradle設定
  91. /common/build.gradle もし何も指定しない場合 apply plugin: 'kotlin-multiplatform' apply from: 'android.gradle' kotlin {

    android() iosArm64() { binaries { framework() } } jvm() js() sourceSets { commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon MPP - Gradle設定
  92. commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation rootProject.ext.serializationCommon

    implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosArm64Main { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs /common/build.gradle フルネームを書く必要がある MPP - Gradle設定
  93. /common/build.gradle commonMain { dependencies { implementation rootProject.ext.kotlinCommon implementation rootProject.ext.coroutineCommon implementation

    rootProject.ext.serializationCommon implementation rootProject.ext.ktorClient implementation rootProject.ext.ktorClientJson } } androidMain.dependencies { } iosMain { dependencies { implementation rootProject.ext.coroutineNative implementation rootProject.ext.serializationNative implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs MPP - Gradle設定
  94. /common/build.gradle implementation rootProject.ext.ktorClientIos implementation rootProject.ext.ktorClientJsonIos } } jsMain { dependencies

    { implementation rootProject.ext.kotlinJs implementation rootProject.ext.coroutineJs implementation rootProject.ext.serializationJs implementation rootProject.ext.ktorClientJs implementation rootProject.ext.ktorClientJsonJs } } jvmMain { dependencies { implementation rootProject.ext.kotlinJvm implementation rootProject.ext.coroutine implementation rootProject.ext.serialization implementation rootProject.ext.ktorClientJvm implementation rootProject.ext.ktorClientJsonJvm } } } MPP - Gradle設定
  95. │ │ └── MainActivity.kt │ └── res ├── build.gradle ├──

    common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common MPP - Gradle設定
  96. │ │ └── MainActivity.kt │ └── res ├── build.gradle ├──

    common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ ├── commonMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common │ │ ├── ApiClient.kt │ │ └── model │ │ └── Greeting.kt │ ├── iosMain │ │ └── kotlin │ │ └── com.github.aakira.mpp.common MPP - Gradle設定
  97. ├── android │ ├── build.gradle │ └── src │ └──

    main │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml MPP - Gradle設定
  98. ├── android │ ├── build.gradle │ └── src │ └──

    main │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml MPP - Gradle設定
  99. versionCode 1 versionName "1.0.0" } buildTypes { release { minifyEnabled

    true proguardFiles getDefaultProguardFile('proguard-an } } } dependencies { implementation project(":common") implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$ } /android/build.gradle 共通モジュールの読み込み
  100. ├── android │ ├── build.gradle │ └── src │ └──

    main │ ├── AndroidManifest.xml │ ├── java │ │ └── com.github.aakira.mpp │ │ └── MainActivity.kt │ └── res ├── build.gradle ├── common │ ├── android.gradle │ ├── build.gradle │ └── src │ ├── androidMain │ │ ├── AndroidManifest.xml 共通モジュールの読み込み
  101. │ │ └── Actual.kt │ ├── jsMain │ │ └──

    kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle ├── gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── ios ├── js │ └── build.gradle ├── server │ └── build.gradle └── settings.gradle 共通モジュールの読み込み
  102. 共通モジュールの読み込み plugins { id 'org.jetbrains.kotlin.js' id 'kotlin-dce-js' } kotlin {

    } [compileKotlinJs, compileTestKotlinJs].each { config -> config.kotlinOptions { moduleKind = 'umd' sourceMap = true metaInfo = true } } dependencies { implementation project(':common') implementation rootProject.ext.kotlinJs } /web/build.gradle
  103. } [compileKotlinJs, compileTestKotlinJs].each { config -> config.kotlinOptions { moduleKind =

    'umd' sourceMap = true metaInfo = true } } dependencies { implementation project(':common') implementation rootProject.ext.kotlinJs } /web/build.gradle 共通モジュールの読み込み
  104. │ │ └── Actual.kt │ ├── jsMain │ │ └──

    kotlin │ │ └── com.github.aakira.mpp.common │ │ └── Actual.kt │ └── jvmMain │ └── kotlin │ └── com.github.aakira.mpp.common │ └── Actual.kt ├── dependencies.gradle ├── gradle ├── gradle.properties ├── gradlew ├── gradlew.bat ├── ios ├── js │ └── build.gradle ├── server │ └── build.gradle └── settings.gradle 共通モジュールの読み込み
  105. plugins { id 'kotlin' id 'application' } group 'com.github.aakira.mpp' version

    '0.0.1' mainClassName = "io.ktor.server.netty.EngineMain" sourceSets { main.kotlin.srcDirs = main.java.srcDirs = ['src'] main.resources.srcDirs = ['resources'] } dependencies { implementation project(':common') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" def ktor_server_version = "1.2.2" implementation "io.ktor:ktor-server-netty:$ktor_server_version" implementation "io.ktor:ktor-gson:$ktor_server_version" implementation "ch.qos.logback:logback-classic:1.2.3" } /server/build.gradle 共通モジュールの読み込み
  106. group 'com.github.aakira.mpp' version '0.0.1' mainClassName = "io.ktor.server.netty.EngineMain" sourceSets { main.kotlin.srcDirs

    = main.java.srcDirs = ['src'] main.resources.srcDirs = ['resources'] } dependencies { implementation project(':common') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" def ktor_server_version = "1.2.2" implementation "io.ktor:ktor-server-netty:$ktor_server_version" implementation "io.ktor:ktor-gson:$ktor_server_version" implementation "ch.qos.logback:logback-classic:1.2.3" } /server/build.gradle 共通モジュールの読み込み
  107. internal expect val hostName: String internal expect val coroutineDispatcher: CoroutineDispatcher

    class ApiClient { private val httpClient = HttpClient() fun getGreeting(successCallback: (Greeting) -> Unit, errorCallback: (Exception) -> Unit) { GlobalScope.apply { launch(coroutineDispatcher) { try { val result = httpClient.get<String> { url { protocol = URLProtocol.HTTP host = hostName // expect value port = 8080 } } val greeting = Json.parse(Greeting.serializer(), result) successCallback(greeting) } catch (e: Exception) { errorCallback(e) } } } } } 共通モジュール(expect) /common/ApiClient.kt
  108. internal expect val hostName: String internal expect val coroutineDispatcher: CoroutineDispatcher

    class ApiClient { private val httpClient = HttpClient() fun getGreeting(successCallback: (Greeting) -> Unit, errorCallback: (Exception) -> Unit) { GlobalScope.apply { launch(coroutineDispatcher) { try { val result = httpClient.get<String> { url { protocol = URLProtocol.HTTP host = hostName // expect value port = 8080 } } val greeting = Json.parse(Greeting.serializer(), result) successCallback(greeting) } catch (e: Exception) { errorCallback(e) } } } } } 共通モジュール(expect) /common/ApiClient.kt
  109. getGreeting(successCallback: (Greeting) -> Unit, errorCallback: ion) -> Unit) { GlobalScope.apply

    { launch(coroutineDispatcher) { try { val result = httpClient.get<String> { url { protocol = URLProtocol.HTTP host = hostName // expect value port = 8080 } } val greeting = Json.parse(Greeting.serializer(), result) successCallback(greeting) } catch (e: Exception) { errorCallback(e) } } } /common/ApiClient.kt 共通モジュール(expect)
  110. /common/ApiClient.kt 共通モジュール(expect) internal expect val hostName: String internal expect val

    coroutineDispatcher: CoroutineDispatcher class ApiClient { private val httpClient = HttpClient() fun getGreeting(successCallback: (Greeting) -> Unit, errorCallback (Exception) -> Unit) { GlobalScope.apply { launch(coroutineDispatcher) { try { val result = httpClient.get<String> { url { protocol = URLProtocol.HTTP host = hostName // expect value
  111. /common/androidMain/Actual.kt internal actual val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO internal actual

    val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default internal actual val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default /common/jsMain/Actual.kt /common/jvmMain/Actual.kt 共通モジュール(actual)
  112. /common/androidMain/Actual.kt internal actual val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO internal actual

    val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default internal actual val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default /common/jsMain/Actual.kt /common/jvmMain/Actual.kt 共通モジュール(actual)
  113. internal actual val coroutineDispatcher: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue()) internal class NsQueueDispatcher(private

    val dispatchQueue: dispatch_queue_t) : CoroutineDispatcher() { override fun dispatch(context: CoroutineContext, block: Runnable) { dispatch_async(dispatchQueue) { block.run() } } } /common/iosMain/Actual.kt 共通モジュール(actual)
  114. internal actual val coroutineDispatcher: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue()) internal class NsQueueDispatcher(private

    val dispatchQueue: dispatch_queue_t) : CoroutineDispatcher() { override fun dispatch(context: CoroutineContext, block: Runnable) { dispatch_async(dispatchQueue) { block.run() } } } /common/iosMain/Actual.kt 共通モジュール(actual)
  115. internal actual val coroutineDispatcher: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue()) internal class NsQueueDispatcher(private

    val dispatchQueue: dispatch_queue_t) : CoroutineDispatcher() { override fun dispatch(context: CoroutineContext, block: Runnable) { dispatch_async(dispatchQueue) { block.run() } } } /common/iosMain/Actual.kt 共通モジュール(actual)
  116. /android/MainActivity.kt private val handler = Handler(Looper.getMainLooper()) override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ApiClient().getGreeting( successCallback = { handler.post { helloText.text = it.hello } }, errorCallback = { handler.post { helloText.text = it.toString() } }) } Client(Android)
  117. private val handler = Handler(Looper.getMainLooper()) override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ApiClient().getGreeting( successCallback = { handler.post { helloText.text = it.hello } }, errorCallback = { handler.post { helloText.text = it.toString() }) } Client(Android) /android/MainActivity.kt
  118. /ios/ViewController.swift override func viewDidLoad() { super.viewDidLoad() let label = UILabel(frame:

    CGRect(x: 0, y: 0, width: view.frame.size.width,
 height: view.frame.size.height) ) label.textAlignment = .center label.font = label.font.withSize(26) self.view.addSubview(label) ApiClient().getGreeting( successCallback: { response in label.text = response.hello }, errorCallback: { error in print(error) }) } Client(iOS)
  119. ) label.textAlignment = .center label.font = label.font.withSize(26) self.view.addSubview(label) ApiClient().getGreeting( successCallback:

    { response in label.text = response.hello }, errorCallback: { error in print(error) }) } /ios/ViewController.swift Client(iOS)
  120. /web/Main.kt fun main() { ApiClient().getGreeting( successCallback = { document.body?.textContent =

    it.hello }, errorCallback = { console.log(it.toString()) } ) } Client(WEB)
  121. fun main() { ApiClient().getGreeting( successCallback = { document.body?.textContent = it.hello

    }, errorCallback = { console.log(it.toString()) } ) } /web/Main.kt Client(WEB)
  122. /web/resources/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Mpp Sample</title>

    <script type="text/javascript" language="JavaScript" src="/build/kotlin.js"></script> ...(ུ) <script type="text/javascript" language="JavaScript" src="/build/mpp-common.js"></script> </head> <body> <script type="text/javascript" language="JavaScript" src="/build/mpp-web.js"></script> </body> </html> Client(WEB)
  123. <head> <meta charset="UTF-8"> <title>Mpp Sample</title> <script type="text/javascript" language="JavaScript" src="/build/kotlin.js"></script> ...(ུ)

    <script type="text/javascript" language="JavaScript" src="/build/mpp-common.js"></script> </head> <body> <script type="text/javascript" language="JavaScript" src="/build/mpp-web.js"></script> </body> </html> /web/resources/index.html Client(WEB)
  124. 宣伝 - 本を書きました 技術評論社から今秋発売予定 タイトル 未定 Android, Server, Test, Coroutine,

    MPP を現場のエンジニアが解説 著者: 愛澤、荒谷、木原、仙波、前川