Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Shared Cross-Platform Modules with Kotlin/Native

Slide 3

Slide 3 text

Simone @viteinfinite

Slide 4

Slide 4 text

No content

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

Shareholders

Slide 9

Slide 9 text

You

Slide 10

Slide 10 text

Solution Cross-Platform

Slide 11

Slide 11 text

Main platforms: • Xamarin • PhoneGap / Cordova • AppCelerator Titanium • React Native • RubyMotion • Flash • ...

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

JVM

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Object-Oriented with bits of Functional Programming

Slide 23

Slide 23 text

Statically typed

Slide 24

Slide 24 text

Optionals var a: String = "abc" a = null // compilation error Smart casting fun demo(x: Any) { if (x is String) { print(x.length) // x is automatically cast to String } }

Slide 25

Slide 25 text

Function extensions fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] = tmp } val l = mutableListOf(1, 2, 3) l.swap(0, 2)

Slide 26

Slide 26 text

Lambda & Inlines max(strings, { a, b -> a.length < b.length }) Data classes data class User(val name: String, val age: Int)

Slide 27

Slide 27 text

20% of Android apps use Kotlin1 1 https://realm.io/realm-report/

Slide 28

Slide 28 text

Server-Side

Slide 29

Slide 29 text

Kotlin/JS Transpilation to JavaScript

Slide 30

Slide 30 text

April 2017

Slide 31

Slide 31 text

Kotlin/Native

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Konan Kotlin Native Backend

Slide 34

Slide 34 text

How it works

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Supported Targets 1. Android Native 2. Web Assembly 3. Linux 4. macOS 5. iOS

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Can I write an entire iOS app in Kotlin?

Slide 40

Slide 40 text

YES

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

But There's More

Slide 43

Slide 43 text

iOS Frameworks

Slide 44

Slide 44 text

Share a Cross-Platform Module between Android and iOS

Slide 45

Slide 45 text

Why Does it Make Sense? 1. Plug into existing codebases 2. Xcode is still the most suitable IDE for iOS 3. Code for views cannot be shared anyway 4. Either way, you have to master UIKit

Slide 46

Slide 46 text

My Use Case Written in Markdown, presented with Deckset

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

Project Setup

Slide 49

Slide 49 text

Project Setup !"" android/ !"" ios/ #"" common/ !"" build.gradle !"" gradle/ #"" slideparser/ !"" build.gradle #"" src/

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

common/slideparser/build.gradle !"" android/ !"" ios/ #"" common/ !"" build.gradle !"" gradle/ #"" slideparser/ !"" build.gradle ⬅⬅⬅ #"" src/

Slide 52

Slide 52 text

common/slideparser/build.gradle (1/3) buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } repositories { mavenCentral() } apply plugin: 'konan' apply plugin: 'com.android.library' apply plugin: 'kotlin-android' // ...

Slide 53

Slide 53 text

common/slideparser/build.gradle (2/3) Android android { compileSdkVersion 26 defaultConfig { // ... } sourceSets { main.java.srcDirs += 'src/main/kotlin' main.java.srcDirs += 'src/test/kotlin' } }

Slide 54

Slide 54 text

common/slideparser/build.gradle (3/3) iOS konanArtifacts { framework('KotlinSlideParser', targets: ['iphone', 'iphone_sim']) { srcDir 'src/main/kotlin' } }

Slide 55

Slide 55 text

Tasks ./gradlew tasks assemble - Assembles all variants of all applications and packages. build - Assembles and tests this project. compileKonanKotlinSlideParserIphone - Build the Kotlin/Native framework \ 'compileKonanKotlinSlideParserIphone' for target 'IPHONE' compileKonanKotlinSlideParserIphone_sim - Build the Kotlin/Native framework 'compileKonanKotlinSlideParserIphone_sim' for target 'IPHONE_SIM'

Slide 56

Slide 56 text

Build ./gradlew -p . compileKonanKotlinSlideParserIphone

Slide 57

Slide 57 text

Multiplatform3 • A new project structure (Kotlin 1.2) • Allows to compile the same code to multiple targets: • JVM • JS • Native • Powerful DSL to define dependencies Coming in Kotlin/Native 0.6 3 https://kotlinlang.org/docs/reference/multiplatform.html

Slide 58

Slide 58 text

IDE

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Digging Deeper

Slide 62

Slide 62 text

Digging Deeper Testing

Slide 63

Slide 63 text

Testing • Unit tests supported in Kotlin/Native • But you can use JUnit (or Spek) too • Optimal IDE support

Slide 64

Slide 64 text

Digging Deeper Objective-C Headers

Slide 65

Slide 65 text

Objective-C Generated Headers

Slide 66

Slide 66 text

Objective-C Generated Headers !"" KotlinSlideParser.framework #"" Headers $ !"" KotlinSlideParser.h ⬅⬅⬅ #"" Info.plist #"" KotlinSlideParser !"" Modules !"" module.modulemap

Slide 67

Slide 67 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 68

Slide 68 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 Array StdlibArray * StdlibArray List 2 NSArray * [String] Int? Nullable NSNumber * NSNumber? 2 From 0.6

Slide 69

Slide 69 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 70

Slide 70 text

Digging Deeper Debugging

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Digging Deeper Memory Management

Slide 75

Slide 75 text

Memory Management • ARC-based... • ...with an automatic cycle collector on top • Garbage Collection is performed periodically • No weak / unowned references

Slide 76

Slide 76 text

Digging Deeper Exceptions

Slide 77

Slide 77 text

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

Slide 78

Slide 78 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 79

Slide 79 text

No content

Slide 80

Slide 80 text

Digging Deeper Concurrency

Slide 81

Slide 81 text

Concurrency Kotlin/Native supports: • Workers (specific to Kotlin/Native) • Coroutines (as in Kotlin/JVM) • not particularly developer-friendly in K/N • high-level API (kotlinx.coroutines) not yet available

Slide 82

Slide 82 text

In Action!

Slide 83

Slide 83 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? { val result = this.pageParser().process(string) return if (result.isEmpty()) { null } else { result[0].first } } }

Slide 84

Slide 84 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 85

Slide 85 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 86

Slide 86 text

No content

Slide 87

Slide 87 text

Another Approach

Slide 88

Slide 88 text

Another Approach On Android, we can compile to an NDK Library • less developer-friendly • better performance • suitable for CPU-intensive operations

Slide 89

Slide 89 text

Current Limitations

Slide 90

Slide 90 text

Current Limitations • Still a Technology Preview • Compilation time • A number of known issues • Some everyday functions not available in K/N • Some types poorly translate to Swift (e.g. generics) • iOS bitcode not supported • ENABLE_BITCODE needs to be set to NO

Slide 91

Slide 91 text

Future Evolutions

Slide 92

Slide 92 text

Future Evolutions • Multiplatform • a specific DSL to build cross-platform apps • AppCode Support • Better Objective-C header generation • kotlinx.coroutines extensions • Native interoperability with Swift (?) • And more...

Slide 93

Slide 93 text

Other Resources • Swift is like Kotlin • Anything you can do, I can do better • Kotlin Blog • Deep Dive into Kotlin/Native by Andrey Breslav • Kotlin Slack

Slide 94

Slide 94 text

Summing Up

Slide 95

Slide 95 text

Main strengths • Reuse Kotlin code you already have • "Feels" like Swift • Write once, run anywhere - even without the JVM! • Battle-tested, mobile-first IDEs

Slide 96

Slide 96 text

Kotlin/Native is still young but already usable, and very, very promising

Slide 97

Slide 97 text

Thank You!

Slide 98

Slide 98 text

Simone @viteinfinite

Slide 99

Slide 99 text

FrenchKit September, 20-21 • Paris frenchkit.fr

Slide 100

Slide 100 text

Source code available here: github.com/viteinfinite/KotlinSlideParser