Slide 1

Slide 1 text

Shared Cross-Platform Modules with Kotlin

Slide 2

Slide 2 text

Simone @viteinfinite

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Microsoft Surface

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

1.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

2.

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

❝"A Surface app is like an iPad app"❞

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Until...

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

❝"An iPad app is like a Xoom app"❞

Slide 20

Slide 20 text

Right?

Slide 21

Slide 21 text

WRONG.

Slide 22

Slide 22 text

WRONG.

Slide 23

Slide 23 text

WRONG.

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Cross-Platform

Slide 26

Slide 26 text

Cross-Platform

Slide 27

Slide 27 text

Cross-Platform Cross-Platform Cross-Platform

Slide 28

Slide 28 text

Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform Cross-Platform

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Years Later...

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Konan

Slide 46

Slide 46 text

Konan Kotlin Native Backend

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Server-Side

Slide 49

Slide 49 text

Kotlin for JavaScript Transpilation to JavaScript

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

❝"Kotlin is like Swift"❞

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

How It Works

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

❝A Kotlin/Native compiler is like a Swift compiler❞

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Supported Targets ➀ Android Native ➁ Web Assembly ➂ Linux ➃ Embedded Systems ➄ macOS ➅ iOS

Slide 60

Slide 60 text

Interoperable Can make use of C / C++ / Objective-C libraries

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Can I Write an Entire iOS App in Kotlin?

Slide 63

Slide 63 text

YES

Slide 64

Slide 64 text

https://github.com/JetBrains/kotlinconf-app/tree/master/ios

Slide 65

Slide 65 text

Can I Write a Complete Cross-Platform App in Kotlin?

Slide 66

Slide 66 text

Can I Write a Complete Cross-Platform App in Kotlin?

Slide 67

Slide 67 text

NO.

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

But...

Slide 70

Slide 70 text

iOS Frameworks

Slide 71

Slide 71 text

Share a Cross-Platform Module Between Android and iOS

Slide 72

Slide 72 text

Let's Try Out

Slide 73

Slide 73 text

My Use Case Written in Markdown, presented with Deckset

Slide 74

Slide 74 text

My Goal Create viewer apps, sharing parsing logic: ☞ Shared parsing library: ☞ iOS ➡ .framework (Kotlin/Native) ☞ Android ➡ .aar (Kotlin/JVM) ☞ iOS (view logic in Swift) ☞ Android (view logic in Kotlin)

Slide 75

Slide 75 text

Multiplatform Since Kotlin 1.2, Kotlin/Native 0.6

Slide 76

Slide 76 text

Multiplatform ☞ Sharing code across multiple platforms: ☞ JVM ☞ JS ☞ Native (iOS, macOS, Android Native, ...)

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Kotlin 1.3

Slide 79

Slide 79 text

Kotlin 1.3 New Multiplatform DSL

Slide 80

Slide 80 text

Kotlin 1.3 New Multiplatform DSL Key Concepts

Slide 81

Slide 81 text

Kotlin Gradle Extension > build.gradle kotlin { // ... }

Slide 82

Slide 82 text

4 Key Concepts ➀ SourceSet ➁ Target ➂ Preset ➃ Compilation

Slide 83

Slide 83 text

1. SourceSet ☞ A group of source files ☞ Usually what's inside the src/ folder

Slide 84

Slide 84 text

1. SourceSet ☞ A group of source files ☞ Usually what's inside the src/ folder ├── build.gradle │ └── src │ ├── main // ← SourceSet │ ├── iosMain // ← SourceSet │ └── ... // ← Other SourceSets

Slide 85

Slide 85 text

2. Target ☞ A variant of your project for a given platform (e.g. Android, iOS device, iOS simulator, Linux, etc) kotlin { targets { // ... } }

Slide 86

Slide 86 text

3. Preset ☞ A way to easily create a target ☞ It's composed of pre-defined configuration kotlin { targets { fromPreset(presets.iosX64, 'iosSim') } }

Slide 87

Slide 87 text

4. Compilation ☞ A "trasformation" of your sources for a specific target ☞ Each target can have one or more compilation, for instance "main", "test", etc

Slide 88

Slide 88 text

4. Compilation kotlin { targets { fromPreset(presets.iosX64, 'iosSim') { compilations.main.foo = bar } } }

Slide 89

Slide 89 text

Target+Compilation ☞ Kotlin automatically creates a sourceSet for each possible compilation + target combination

Slide 90

Slide 90 text

Target+Compilation ☞ Kotlin automatically creates a sourceSet for each possible compilation + target combination ├── build.gradle │ └── src │ ├── main // almost useless in MPP │ ├── commonMain // created automatically │ ├── iosMain │ ├── iosTest │ └── ...

Slide 91

Slide 91 text

What if...

Slide 92

Slide 92 text

...you need a platform-specific implementation? class Service { fun log() { // Platform-specific code // NSLog on iOS // Log.d on Android } }

Slide 93

Slide 93 text

2 Solutions

Slide 94

Slide 94 text

1. Interfaces Interfaces get translated conveniently across platforms interface Logger { fun log(string: String) } class Service(val logger: Logger) { fun log(string: String) { logger.log(string) } }

Slide 95

Slide 95 text

2. Actuals: 1/3 Actuals are a functionality introduced in KMPP 1.2 // SourceSet commonMain expect class Logger { fun log(string: String) } class Service(val logger: Logger) { fun log(string: String) { logger.log(string) } }

Slide 96

Slide 96 text

2. Actuals: 2/3 // SourceSet iosMain actual class Logger { actual fun log(string: String) { NSLog(string) } }

Slide 97

Slide 97 text

2. Actuals: 3/3 kotlin { sourceSets { iosMain { dependsOn commonMain } } ...

Slide 98

Slide 98 text

Project Setup

Slide 99

Slide 99 text

Project Setup ├── build.gradle │ └── src │ ├── commonMain │ ├── iosMain │ └── main

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

build.gradle: 1/4 buildscript { ext.kotlin_version = '1.3.0-rc-57' // ... Repositories and dependencies as usual } apply plugin: 'kotlin-multiplatform' // ...

Slide 102

Slide 102 text

build.gradle: 2/4 apply plugin: 'com.android.library' android { compileSdkVersion 26 defaultConfig { minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName '1.0' } }

Slide 103

Slide 103 text

build.gradle: 3/4 kotlin { sourceSets { iosMain { dependsOn commonMain } }

Slide 104

Slide 104 text

build.gradle: 4/4 targets { fromPreset(presets.android, 'androidLibrary') def isSim = findProperty("kotlin.target") == "iosSim" def iosPreset = isSim ? presets.iosX64 : presets.iosArm64 fromPreset(iosPreset, 'iosSim') { compilations.main.outputKinds('FRAMEWORK') } } }

Slide 105

Slide 105 text

Tasks ./gradlew tasks assemble - Assembles all variants of all applications and packages. build - Assembles and tests this project. linkDebugFrameworkIos - Links an Objective-C framework from the 'main' compilation for target 'native'.

Slide 106

Slide 106 text

Build ./gradlew -p . linkDebugFrameworkIos

Slide 107

Slide 107 text

❝"A Kotlin Framework is like a Swift Framework"❞

Slide 108

Slide 108 text

❝"A Kotlin Framework is like a Swift Framework"❞

Slide 109

Slide 109 text

❝"A Framework is a Framework"❞

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

IDE

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

Digging Deeper

Slide 116

Slide 116 text

Testing

Slide 117

Slide 117 text

Testing ☞ Unit tests supported in Kotlin Multiplatform ☞ IDE support

Slide 118

Slide 118 text

import kotlin.test.* class TestFoo { @Test fun testBar() { assertTrue { Foo().bar().startsWith("bar-") } } } ... Kotlin/Native requires additional configuration → please refer to our blogs

Slide 119

Slide 119 text

Digging Deeper Objective-C Headers

Slide 120

Slide 120 text

Objective-C Generated Headers

Slide 121

Slide 121 text

Objective-C Generated Headers └── KotlinSlideParser.framework ├── Headers │ └── KotlinSlideParser.h ‏‏‏ ├── Info.plist ├── KotlinSlideParser └── Modules └── module.modulemap

Slide 122

Slide 122 text

#import @class KSPSupport, KSPMyEnum, KSPStdlibEnum, KSPOtherEnum, KSPSlideEntity, KSPSlideEntityPage, KSPMarkdownEntity, KSPMarkdownEntityItalic, KSPMarkdownEntityBold; @class KSPMarkdownEntityHeader, KSPMarkdownEntityInlineCode, KSPMarkdownEntityCodeBlock, KSPMarkdownEntityLinks; @class KSPMarkdownEntityPlain, KSPMarkdownEntityRefer, KSPMarkdownEntityDelete, KSPSlideParser; @protocol KSPStdlibComparable; NS_ASSUME_NONNULL_BEGIN @interface KotlinBase : NSObject -(instancetype) init __attribute__((unavailable)); +(instancetype) new __attribute__((unavailable)); +(void)initialize __attribute__((objc_requires_super)); @end; @interface KotlinBase (KotlinBaseCopying) @end; __attribute__((objc_runtime_name("KotlinMutableSet"))) @interface KSPMutableSet : NSMutableSet @end; __attribute__((objc_runtime_name("KotlinMutableDictionary"))) @interface KSPMutableDictionary : NSMutableDictionary @end; __attribute__((objc_subclassing_restricted)) @interface KSPSupport : KotlinBase -(instancetype)init NS_SWIFT_NAME(init()) NS_DESIGNATED_INITIALIZER; -(NSNumber* _Nullable)optionalInt NS_SWIFT_NAME(optionalInt()); -(void)somethingThatThrows NS_SWIFT_NAME(somethingThatThrows()); @end; @protocol KSPStdlibComparable @required -(int32_t)compareToOther:(id _Nullable)other NS_SWIFT_NAME(compareTo(other:)); @end; NS_ASSUME_NONNULL_END

Slide 123

Slide 123 text

Kotlin / ObjC / Swift Type Mapping Kotlin Objective-C Swift Boolean BOOL Bool Float / Double float / double Float / Double Int int32_t Int32 String NSString * String List NSArray * [String] Int? Nullable NSNumber * NSNumber?

Slide 124

Slide 124 text

Kotlin Objective-C Swift interface @protocol @protocol class class class data class class class enum class StdlibEnum StdlibEnum Kotlin Objective-C Swift open (subclassable) open public (subclassable) open -→

Slide 125

Slide 125 text

Digging Deeper Debugging

Slide 126

Slide 126 text

Debugging ☞ Debugging is supported ☞ Compiler produces a .dSYM file ☞ "official" LLVM symbolication file for debugging ☞ Allows using Xcode for debugging

Slide 127

Slide 127 text

Debugging (lldb) breakpoint set --func-regex "myFunc" (lldb) b kfun:

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

❝Xcode is like AppCode!❞

Slide 130

Slide 130 text

Features ☞ Breakpoints ✅ ☞ Stack Trace ✅ ☞ Stepping ✅ ☞ Object Value Inspection ⚠

Slide 131

Slide 131 text

Digging Deeper Memory Management

Slide 132

Slide 132 text

Memory Management ☞ ARC-based... ☞ ...with an automatic cycle collector on top ☞ Garbage Collection is performed periodically ☞ weak references supported since 0.7

Slide 133

Slide 133 text

Digging Deeper Exceptions

Slide 134

Slide 134 text

Exceptions ☞ All exceptions are unchecked in Kotlin ☞ @Throws is not supported in Kotlin/Native ☞ Cannot bridge Kotlin Exceptions to Swift Errors

Slide 135

Slide 135 text

Exception (un)Handling fun somethingThatThrows() { throw Exception(message = "Oops.") } ‑ Uncaught Kotlin exception: kotlin.Exception: Oops. at 3 KotlinSlideParser 0x104cb54f3 kfun:kotlin.Exception.(kotlin.String)kotlin.Exception + 115 at 4 KotlinSlideParser 0x104c81dea kfun:fr.xebia.slideparser.Support.somethingThatThrows() + 122 at 5 KotlinSlideParser 0x104c81d10 KotlinSlideParser + 7440 at 6 SlideRehearser 0x104978081 _T014SlideRehearser14ViewControllerC11viewDidLoadyyF + 81 at 7 SlideRehearser 0x104978104 _T014SlideRehearser14ViewControllerC11viewDidLoadyyFTo + 36 at 8 UIKit 0x107e8646c -[UIViewController loadViewIfRequired] + 1235 ...

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

Digging Deeper Concurrency

Slide 138

Slide 138 text

Concurrency Kotlin/Native supports: ☞ Workers (specific to Kotlin/Native) ☞ Coroutines! ☞ kotlinx.coroutines available since September ☞ Only single-threaded code is currently supported

Slide 139

Slide 139 text

In Action!

Slide 140

Slide 140 text

Kotlin public sealed class SlideEntity { data class Page(val contents: List): SlideEntity() } public sealed class MarkdownEntity { data class Header(val contents: List, val level: Int): MarkdownEntity() data class Plain(val contents: String): MarkdownEntity() } public class SlideParser { public fun parsePages(string: String): List? { return this.pageParser().process(string) } }

Slide 141

Slide 141 text

iOS (Swift) import KotlinSlideParser // ... let parser = KSPSlideParser() guard let pages = parser.parsePages(string: myText) else { return } pages.first?.contents.forEach { entity in switch entity { case let header as KSPMarkdownEntityHeader: header.level // Level of the header case let plain as KSPMarkdownEntityPlain: plain.contents // Text of the entity default: break } }

Slide 142

Slide 142 text

Android (Kotlin) import fr.xebia.slideparser.SlideParser // ... val parser = SlideParser() val pages = parser.parsePages(myText) pages?.first()?.contents?.forEach { when(it) { is MarkdownEntity.Header -> it.level // Level of the header is MarkdownEntity.Plain -> it.contents // Text of the entity } }

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

So this thing is perfect! Right?

Slide 146

Slide 146 text

Current Limitations

Slide 147

Slide 147 text

Still a Technology Preview

Slide 148

Slide 148 text

Current Limitations ☞ Compilation time ☞ Binary size ☞ Not 100% compatible with iOS bitcode ☞ Some everyday functions not available in K/N ☞ Lacking documentation

Slide 149

Slide 149 text

Open Questions ☞ GC-like memory model: strength or weakness? ☞ How to overcome platform differences? ☞ e.g. Concurrency / Workers

Slide 150

Slide 150 text

The Future is Now

Slide 151

Slide 151 text

The Future is Now ☞ Feature parity between Kotlin dialects ☞ New common extensions: ☞ coroutines ✅ ☞ Limited IDE support ✅ ☞ Simplified MPP Support ✅ ☞ Cannot inspect objects in the debugger ✅ ☞ Direct Interoperability with Swift ☞ Some new announcements at KotlinConf?

Slide 152

Slide 152 text

Summing Up

Slide 153

Slide 153 text

Kotlin Multiplatform + Kotlin/Native

Slide 154

Slide 154 text

Technology Preview

Slide 155

Slide 155 text

Encourages Modularity

Slide 156

Slide 156 text

Feels Familiar to All Mobile Developers

Slide 157

Slide 157 text

A New Approach to Cross-Platform

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

Thank You!

Slide 163

Slide 163 text

Simone @viteinfinite

Slide 164

Slide 164 text

Full demo: github.com/xebia-france/kotlin-ios- framework

Slide 165

Slide 165 text

Other Resources ☞ Kotlin Blog ☞ Deep Dive into Kotlin/Native by Andrey Breslav ☞ blog.xebia.fr | viteinfinite.com ☞ github.com/JetBrains/kotlin-mpp-example ☞ Kotlin Slack