Share
Embed
Kotlin Multiplatform MobileのおさらいとABEMAでのマルチプラットフォーム対応 | CA BASE NEXT 2022
https://ca-base-next.cyberagent.co.jp/2022/sessions/kmm-abema/
近年,技術的なトレンドとしてKMM(Kotlin Multiplatform Mobile)やFlutter,React Nativeといったマルチプラットフォーム・クロスプラットフォーム技術が注目を浴びています。
Kotlinは2018年にリリースしたKotlin 1.3から,AndroidとiOSを含む様々なプラットフォーム間でのコードの共通化が可能になり,今年(2022年)にはKMMのベータ版リリースが予定されています。
様々なプラットフォームにサービスを展開しているABEMAでも,このKMMによってプラットフォーム間でのコードの共通化に取り組んでいます。
このセッションでは,特にモバイルプラットフォームにフォーカスを当ててKotlin Multiplatformのこれまでを振り返りつつ,ABEMAにおけるKMMの活用事例や開発している中での様々な知見を紹介します。
Seiya Kokushi
August 04, 2022
Transcript
58 slides
-
1. Kotlin Multiplatform MobileʢKMMʣͱ 2. Kotlin Multiplatformͷ͜Ε·Ͱ 3. Kotlin MultiplatformͷΈ
4. νʔϜͰಋೖɾӡ༻͍ͯ͘͠ʹ 5. KMMͷ՝ͱকདྷੑ 6. ࠷ۙͷτϐοΫε -
Kotlin Multiplatform / KMP / KMM • KotlinʹΑͬͯෳϓϥοτϑΥʔϜͷ։ൃΛߦ͏ٕज़ ΤίγεςϜશମ Kotlin
Multiplatform Kotlin Multiplatform Project (KMP) • Kotlin MultiplatformΛ༻͍ͯ։ൃ͞ΕΔϓϩδΣΫτ -
Kotlin Multiplatform Mobile (KMM)? 🤝 https://youtu.be/PW-jkOLucjM?t=274 ⚙ Kotlin Multiplatform Multiplatform
Gradle DSL Kotlin + Kotlin/JVM + Kotlin/Native + … Android Studio Plugin + CocoaPods integration + … 📱 Mobile Features SDK for cross-platform mobile development OFFICIAL -
• KotlinͷڧྗͳAPIΛ׆༻ͯ͠ιʔείʔυΛڞ௨ԽͰ͖Δ • ֤ϓϥοτϑΥʔϜಛ༗ͷAPI΄ͱΜͲར༻Ͱ͖Δ • Ұ෦ϓϥοτϑΥʔϜΛআ͍ͯɼUI·Ͱڞ௨ԽͰ͖Δ • ରϓϥοτϑΥʔϜଟ͍ • HTTPɼγϦΞϥΠζɼඇಉظॲཧͳͲ։ൃʹ͔ܽͤͳ͍ϥΠϒϥϦΛJetBrains͕ఏڙɾ
ϝϯςφϯε͍ͯ͠Δ • খ͘͞ಋೖΛਐΊΒΕΔ • ϥΠϒϥϦͱͯ͢͠Δ͜ͱͰ͖Δ Kotlin MultiplatformKMMͷϝϦοτ -
FlutterUnityͳͲͱͷҧ͍ʁ • ֤ϓϥοτϑΥʔϜͰΞϓϦέʔγϣϯ͕ϥϯλΠϜͰಈ ࡞͢Δ࣮ߦڥΛඞཁͱ͢Δ • UI·Ͱڞ௨Խ͢Δ͜ͱ͕Մೳ Flutter, Unity… Kotlin Multiplatform
• ಛఆͷ࣮ߦڥΛඞཁͱ͠ͳ͍ • KotlinΛ֤ϓϥοτϑΥʔϜͰಈ࡞͢ΔNativeίʔυʹม ͢Δ • ݱ࣌ͰiOSͷUIڞ௨ԽΛެ͕ࣜαϙʔτ͍ͯ͠ͳ͍ -
Kotlin 1.1 2016/02 Kotlin 1.2 2017/11 Kotlin 1.3 2018/10 Kotlin
1.4 2020/08 Kotlin 1.5 2021/05 Kotlin 1.6 2021/11 Kotlin 1.7 2022/06 KotlinΛJSʹίϯύΠϧ͢ΔػೳʢKotlin/JSʣ͕Stableʹ JVMͱJSڥͷιʔείʔυڞ௨Խ͕Մೳʹ Kotlin/Native Beta (v1.0.1) Kotlin Multiplatform Mobile Alpha Hierarchical project structure Kotlin NativeͷύϑΥʔϚϯεվળ Experimental new memory manager Alpha new memory manager -
Kotlin 1.1 2016/02 Kotlin 1.2 2017/11 Kotlin 1.3 2018/10 Kotlin
1.4 2020/08 Kotlin 1.5 2021/05 Kotlin 1.6 2021/11 Kotlin 1.7 2022/06 KotlinΛJSʹίϯύΠϧ͢Δػೳ(Kotlin/JS)͕Stableʹ JVMͱJSڥͷιʔείʔυڞ௨Խ͕Մೳʹ Kotlin/Native Beta (v1.0.1) Kotlin Multiplatform Mobile Alpha Hierarchical project structure Kotlin NativeͷύϑΥʔϚϯεվળ Experimental new memory manager Alpha new memory manager https://blog.jetbrains.com/kotlin/2017/11/kotlin-1-2-released/ -
Kotlin 1.1 2016/02 Kotlin 1.2 2017/11 Kotlin 1.3 2018/10 Kotlin
1.4 2020/08 Kotlin 1.5 2021/05 Kotlin 1.6 2021/11 Kotlin 1.7 2022/06 KotlinΛJSʹίϯύΠϧ͢Δػೳ(Kotlin/JS)͕Stableʹ JVMͱJSڥͷιʔείʔυڞ௨Խ͕Մೳʹ Kotlin/Native Beta (v1.0.1) Kotlin Multiplatform Mobile Alpha Hierarchical project structure Kotlin NativeͷύϑΥʔϚϯεվળ Experimental new memory manager Alpha new memory manager https://youtu.be/3uVUDsoE_5U?t=220 https://blog.jetbrains.com/kotlin/2020/08/kotlin-multiplatform-mobile-goes-alpha/ -
Kotlin 1.1 2016/02 Kotlin 1.2 2017/11 Kotlin 1.3 2018/10 Kotlin
1.4 2020/08 Kotlin 1.5 2021/05 Kotlin 1.6 2021/11 Kotlin 1.7 2022/06 KotlinΛJSʹίϯύΠϧ͢Δػೳ(Kotlin/JS)͕Stableʹ JVMͱJSڥͷιʔείʔυڞ௨Խ͕Մೳʹ Kotlin/Native Beta (v1.0.1) Kotlin Multiplatform Mobile Alpha Hierarchical project structure Kotlin NativeͷύϑΥʔϚϯεվળ Experimental new memory manager Alpha new memory manager https://blog.jetbrains.com/kotlin/2021/08/try-the-new-kotlin-native-memory-manager-development-preview/ -
https://blog.jetbrains.com/kotlin/2022/05/kotlin-multiplatform-mobile-beta-roadmap-update/ we have postponed Kotlin Multiplatform Mobile Beta until the
memory model becomes the default, which we expect to be the case no later than autumn 2022. -
How Kotlin Multiplatform works • KotlinͷίϯύΠϥʹFrontendͱBackend͕͋ Γɼڞ௨ͷFrontendͰதؒදݱʹมޙɼ֤ϓϥοτ ϑΥʔϜͷBackendΛ௨ͯ͡όΠφϦʹม͞ΕΔ • ঃʑʹҠߦத
🏃 K2 Compiler https://youtu.be/3uVUDsoE_5U?t=303 Kotlin Ecosystem • ϓϥοτϑΥʔϜڞ௨ͷϥΠϒϥϦAPI • + ϓϥοτϑΥʔϜݻ༗ͷAPI • Kotlin/JVM, Kotlin/JS, Kotlin/Wasm, Kotlin/Native -
Code sharing • commonιʔεηοτʹڞ௨ϓϥοτϑΥʔϜͷ࣮Λஔ ͘͜ͱ͕Ͱ͖Δ • ϓϥοτϑΥʔϜಛ༗ͷιʔεηοτ࣋ͯΔ • Hierarchical project
structureʹΑͬͯɼ͍ۙڥͷϓ ϥοτϑΥʔϜΛڞ௨Խͨ͠ιʔεηοτ࡞ΕΔ https://kotlinlang.org/docs/multiplatform.html#how-kotlin-multiplatform-works -
expect/actual • commonιʔεηοτʹϓϥοτϑΥʔϜ͝ͱʹ࣮ͷҟͳ ΔΫϥεɼؔɼϓϩύςΟͳͲΛexpectΛ༻͍ͯఆٛͰ͖ Δ • ֤ϓϥοτϑΥʔϜͷιʔεηοτʹactualΛ༻͍࣮ͯৄࡉ Λهड़͢Δ • commonιʔεηοτ͚ͩͰͳ͘ɼڥͷ͍ۙϓϥοτ
ϑΥʔϜΛ·ͱΊͨιʔεηοτʹexpectΛ༻͍ͨఆ͕ٛՄ ೳ https://kotlinlang.org/docs/multiplatform-connect-to-apis.html ϓϥοτϑΥʔϜݻ༗ͷAPI -
Target platforms • JVM • JS (browser/Node.js) • Android •
Android NDK (ARM32/ARM64/x86/x86_64) • iOS (ARM32/ARM64/iOS simulator) • watchOS (ARM32/ARM64/watchOS simulator) • tvOS (ARM64/tvOS simulator) • macOS (x86_64/Apple Silicon) • Linux (ARM32/ARM64/MIPS/mipsel/x86_64) • Windows (32-bit Windows/64-bit Windows) • WebAssembly https://kotlinlang.org/docs/multiplatform-dsl-reference.html#targets -
Build configuration kotlin { android() listOf( iosX64(), iosArm64(), iosSimulatorArm64() ).forEach
{ it.binaries.framework { baseName = "base" } } sourceSets { val commonMain by getting { dependencies { // … } } val commonTest by getting val androidMain by getting val androidTest by getting val iosX64Main by getting val iosArm64Main by getting val iosSimulatorArm64Main by getting val iosMain by creating { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) } val iosX64Test by getting val iosArm64Test by getting val iosSimulatorArm64Test by getting val iosTest by creating { dependsOn(commonTest) iosX64Test.dependsOn(this) iosArm64Test.dependsOn(this) iosSimulatorArm64Test.dependsOn(this) } } } -
Build configuration kotlin { android() listOf( iosX64(), iosArm64(), iosSimulatorArm64() ).forEach
{ it.binaries.framework { baseName = "base" } } sourceSets { val commonMain by getting { dependencies { // … } } val commonTest by getting val androidMain by getting val androidTest by getting val iosX64Main by getting val iosArm64Main by getting val iosSimulatorArm64Main by getting val iosMain by creating { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) } val iosX64Test by getting val iosArm64Test by getting val iosSimulatorArm64Test by getting val iosTest by creating { dependsOn(commonTest) iosX64Test.dependsOn(this) iosArm64Test.dependsOn(this) iosSimulatorArm64Test.dependsOn(this) } } } λʔήοτϓϥοτϑΥʔϜͷࢦఆ -
Build configuration kotlin { android() listOf( iosX64(), iosArm64(), iosSimulatorArm64() ).forEach
{ it.binaries.framework { baseName = "base" } } sourceSets { val commonMain by getting { dependencies { // implementation(…) } } val commonTest by getting val androidMain by getting val androidTest by getting val iosX64Main by getting val iosArm64Main by getting val iosSimulatorArm64Main by getting val iosMain by creating { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) } val iosX64Test by getting val iosArm64Test by getting val iosSimulatorArm64Test by getting val iosTest by creating { dependsOn(commonTest) iosX64Test.dependsOn(this) iosArm64Test.dependsOn(this) iosSimulatorArm64Test.dependsOn(this) } } } ґଘؔͳͲͷઃఆ ※ ґଘ͢ΔϥΠϒϥϦଆͰɼ λʔήοτͱͳΔϓϥοτϑΥʔ ϜʹରԠ͍ͯ͠ͳ͍ͱ͍͚ͳ͍ -
Build configuration kotlin { android() listOf( iosX64(), iosArm64(), iosSimulatorArm64() ).forEach
{ it.binaries.framework { baseName = "base" } } sourceSets { val commonMain by getting { dependencies { // implementation(…) } } val commonTest by getting val androidMain by getting val androidTest by getting val iosX64Main by getting val iosArm64Main by getting val iosSimulatorArm64Main by getting val iosMain by creating { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) } val iosX64Test by getting val iosArm64Test by getting val iosSimulatorArm64Test by getting val iosTest by creating { dependsOn(commonTest) iosX64Test.dependsOn(this) iosArm64Test.dependsOn(this) iosSimulatorArm64Test.dependsOn(this) } } } ϥΠϒϥϦͷґଘؔ ※ commonͰґଘ͍ͯ͠ΔϥΠϒ ϥϦ͕֤ϓϥοτϑΥʔϜʹର Ԡ͍ͯ͠ΕɼϓϥοτϑΥʔ ϜผʹґଘΛॻ͘ඞཁͳ͍ -
Build configuration kotlin { android() listOf( iosX64(), iosArm64(), iosSimulatorArm64() ).forEach
{ it.binaries.framework { baseName = "base" } } sourceSets { val commonMain by getting { dependencies { // implementation(…) } } val commonTest by getting val androidMain by getting val androidTest by getting val iosX64Main by getting val iosArm64Main by getting val iosSimulatorArm64Main by getting val iosMain by creating { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) iosSimulatorArm64Main.dependsOn(this) } val iosX64Test by getting val iosArm64Test by getting val iosSimulatorArm64Test by getting val iosTest by creating { dependsOn(commonTest) iosX64Test.dependsOn(this) iosArm64Test.dependsOn(this) iosSimulatorArm64Test.dependsOn(this) } } } ιʔεηοτಉ࢜ͷґଘؔ -
Build configuration kotlin { android { compilations.all { kotlinOptions.jvmTarget =
"1.8" } publishAllLibraryVariants() } ios { binaries.framework { baseName = "base" } } // ... } • ෳλʔήοτͷγϣʔτΧοτ༻ҙ͞Ε͍ͯΔ • ios (iosArm64/iosX64) • tvos (tvosArm64/tvosX64) • watchOS (watchosArm32/watchosArm64/watchosX64) • xxxSimulatorArm64ݱঢ়ؚ·Ε͍ͯͳ͍ -
KotlinKotlin MultiplatformΛΔ • ެࣜυΩϡϝϯτ • ެࣜϒϩά • YouTubeνϟϯωϧ • Slack
• Twitter • reddit • ϋϯζΦϯ • αϯϓϧϓϩδΣΫτ • … -
KotlinKotlin MultiplatformΛΔ • ެࣜυΩϡϝϯτ • ެࣜϒϩά • YouTubeνϟϯωϧ • Slack
• Twitter • reddit • ϋϯζΦϯ • αϯϓϧϓϩδΣΫτ • … • ެࣜυΩϡϝϯτ • ެࣜϒϩά • YouTubeνϟϯωϧ • Slack • Twitter • reddit -
KotlinKotlin MultiplatformΛΔ • ެࣜυΩϡϝϯτ • ެࣜϒϩά • YouTubeνϟϯωϧ • Slack
• Twitter • reddit • ϋϯζΦϯ • αϯϓϧϓϩδΣΫτ • … • ϋϯζΦϯ • αϯϓϧϓϩδΣΫτ -
• ͍͔ͭ͘ͷϓϥοτϑΥʔϜͰUI·Ͱڞ௨Խ͕͍͠ • StableͰͳ͍ͷͰϓϩδΣΫτશମͷେ͖ͳมߋ͕ඞཁͱͳΔ߹͕͋Δ • SwiftͱͷޓੑͰਏ͍͜ͱ͕… • Kotlin/NativeͷϏϧυ͕͍࣌ؒ • ίϛϡχςΟ͕·ͩൺֱతখ͍͔͞ʁ
• ͍͔ͭ͘ͷϓϥοτϑΥʔϜͰUI·Ͱڞ௨Խ͕͍͠ FUTURE? KMMͷ՝ͱকདྷੑ -
• ͍͔ͭ͘ͷϓϥοτϑΥʔϜͰUI·Ͱڞ௨Խ͕͍͠ • StableͰͳ͍ͷͰϓϩδΣΫτશମͷେ͖ͳมߋ͕ඞཁͱͳΔ߹͕͋Δ • SwiftͱͷޓੑͰਏ͍͜ͱ͕… • Kotlin/NativeͷϏϧυ͕͍࣌ؒ • ίϛϡχςΟ͕·ͩൺֱతখ͍͔͞ʁ
FUTURE? • SwiftͱͷޓੑͰਏ͍͜ͱ͕… https://youtrack.jetbrains.com/issue/KT-49521 KMMͷ՝ͱকདྷੑ -
• ͍͔ͭ͘ͷϓϥοτϑΥʔϜͰUI·Ͱڞ௨Խ͕͍͠ • StableͰͳ͍ͷͰϓϩδΣΫτશମͷେ͖ͳมߋ͕ඞཁͱͳΔ߹͕͋Δ • SwiftͱͷޓੑͰਏ͍͜ͱ͕… • Kotlin/NativeͷϏϧυ͕͍࣌ؒ • ίϛϡχςΟ͕·ͩൺֱతখ͍͔͞ʁ
KEEP GOING https://youtrack.jetbrains.com/issue/KT-49521 • Kotlin/NativeͷϏϧυ͕͍࣌ؒ KMMͷ՝ͱকདྷੑ -
• ͍͔ͭ͘ͷϓϥοτϑΥʔϜͰUI·Ͱڞ௨Խ͕͍͠ • StableͰͳ͍ͷͰϓϩδΣΫτશମͷେ͖ͳมߋ͕ඞཁͱͳΔ߹͕͋Δ • SwiftͱͷޓੑͰਏ͍͜ͱ͕… • Kotlin/NativeͷϏϧυ͕͍࣌ؒ • ίϛϡχςΟ͕·ͩൺֱతখ͍͔͞ʁ
KEEP GOING https://github.com/dropbox/AffectedModuleDetector • Kotlin/NativeͷϏϧυ͕͍࣌ؒ KMMͷ՝ͱকདྷੑ ABEMAͰɼdropbox/AffectedModuleDetectorΛ Kotlin MultiplatformͰར༻Ͱ͖ΔΑ͏ʹͯ͠ɼมߋӨڹ ͷ͋ΔϞδϡʔϧͷΈΛϏϧυ͍ͯ͠Δ -
Hierarchical Project Structure • ϓϥοτϑΥʔϜͷιʔεηοτΛ·ͱΊΔ͜ͱ͕Մೳʹ • Kotlin 1.4.0͔ΒಋೖՄೳ • Kotlin
1.6.20͔Β৽نϓϩδΣΫτ࡞ͷࡍʹσϑΥϧτͰઃఆ͞ΕΔΑ͏ʹ commonMain androidMain iosArm64Main iosX64Main commonMain androidMain iosArm64Main iosX64Main iosMain No Hierarchical Hierarchical https://kotlinlang.org/docs/multiplatform-hierarchy.html -
Hierarchical project structure • gradle.propertiesͰઃఆ • ґଘ͍ͯ͠ΔϥΠϒϥϦ͕ରԠ͍ͯ͠ͳ͍߹ɼiosMain ͱ͍ͬͨڞ௨ιʔεηοτͰϥΠϒϥϦΛ༻Ͱ͖ͳ͍ ࢀߟจݙɿhttps://kotlinlang.org/docs/multiplatform.html#how-kotlin-multiplatform-works ରԠ͢Δʹʁ
kotlin.mpp.enableGranularSourceSetsMetadata=true kotlin.native.enableDependencyPropagation=false https://kotlinlang.org/docs/multiplatform-hierarchy.html -
Kotlin/Native Memory Management • ݻ༗ͷϧʔϧ • ϧʔϧ1ɿMutable State == 1
thread • ϧʔϧ2ɿImmutable State == many thread • Immutable State != val • ϥϯλΠϜͰෆมͳঢ়ଶʢfrozen stateʣͰ͋Δ͜ͱ͕ٻΊΒΕΔ • freeze()Ͱ͋ΒΏΔΠϯελϯεΛfreezeͰ͖Δ https://kotlinlang.org/docs/multiplatform-mobile-concurrency-overview.html Kotlin/NativeͷϚϧνεϨουϓϩάϥϛϯάͷ՝ -
Kotlin/Native Memory Management • ղܾࡦ • AtomicXxxΛ༻͢Δ • Thread-isolate stateΛ༻͢Δ
ImmutableͰͳ͍ͱෳεϨουͰѻ͑ͳ͍ʁ https://kotlinlang.org/docs/multiplatform-mobile-concurrent-mutability.html -
New Kotlin/Native Memory Management • Reference-counting GC → Tracing GC
※ ࠓޙΞϧΰϦζϜ͕มΘΔՄೳੑ͋Δ • ฒߦͰͷGC͕Մೳʹ • freeze()͕ෆཁʹͳΔ • AtomicReferenceҾ͖ଓ͖ඞཁ • AtomicReferenceΛؚΜͩ॥ࢀরʹΑΔϝϞϦϦʔΫͷ৺ແ͘ͳͬͨ • Kotlin 1.6.20͔ΒexperimentalͳNew Memory Manager͕༻Մೳ • GCͷύϑΥʔϚϯεʹ·ͩ͋Γ • Kotlin 1.7.0Ͱalphaʹ New Memory ManagementͰͲ͏ͳΔ͔ https://blog.jetbrains.com/kotlin/2021/05/kotlin-native-memory-management-update/ -
New Kotlin/Native Memory Management • ϚΠάϨʔγϣϯΨΠυ • https://github.com/JetBrains/kotlin/blob/master/kotlin-native/NEW_MM.md • ϥΠϒϥϦ
• kotlinx.coroutines • 1.6.0Ҏ߱ɼnative-mtόʔδϣϯΛ༻͠ͳͯ͘ྑ͘ͳΔ • ktor • 2.0.0Ҏ߱New memory managementʹରԠ͢Δඞཁ͕͋Δ New Memory ManagementΛࢼ͢