Slide 1

Slide 1 text

Release 5 – 24/10/2019 ✿✿✿✿ ʕ •ᴥ•ʔ/ ︻デ═⼀一 This document is available under terms of Creative Commons with CC-BY-SA 3.0

Slide 2

Slide 2 text

Pierre-Yves Lapersonne Android & iOS apps developer pylapersonne.info @pylapp

Slide 3

Slide 3 text

KOTLIN NATIVE

Slide 4

Slide 4 text

ABOUT
 KOTLIN

Slide 5

Slide 5 text

5 Source: Open Street Map

Slide 6

Slide 6 text

Kotlin • Created by JetBrains
 • Mix of object & functional paradigms • Static & inferred typing
 • Open source, under Apache 2 license
 • Compilable for Java & JS VM, C, Objective-C • Last version: 1.3.4 6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

< 17 may 2017 8

Slide 9

Slide 9 text

Grand’pa Java 9 😔

Slide 10

Slide 10 text

17 may 2017! 10

Slide 11

Slide 11 text

Kotlin for Android 11 😻 Note: Here is the Kotlin code converted from Java code with Android Studio (a bit dirty!!)

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

KOTLIN/NATIVE

Slide 14

Slide 14 text

Definition « Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine. It is an LLVM based backend for the Kotlin compiler and native implementation of the Kotlin standard library. » 14 Source: kotlinlang.org

Slide 15

Slide 15 text

Kotlin/Native • Will not replace legacy frameworks • Allows to have bidirectional interop. between Kotlin and target code ! To mutualize/share source code between platforms 
 Code common, respect the platform 15

Slide 16

Slide 16 text

Targets • A lot of platforms… • … where we cannot have or don’t want VM 16

Slide 17

Slide 17 text

Targets 17 x86 ARM MIPS

Slide 18

Slide 18 text

KOTLIN/NATIVE Under the hood

Slide 19

Slide 19 text

Platform libs 19 
 POSIX Apple frameworks W3C DOM Win32
 … x86 ARM MIPS

Slide 20

Slide 20 text

K/N compilation 20 Frontend Backend Kotlin/Native Compiler Kotlin/Native: a part of the Kotlin Multiplatform project

Slide 21

Slide 21 text

K/N compilation 21 Intermediate Representation • Cleaning • Devirtualization LLVM Konan

Slide 22

Slide 22 text

.kt 22

Slide 23

Slide 23 text

IR 23 > kotlinc-native --print_bitcode Program.kt 2>Program.IR IR

Slide 24

Slide 24 text

WASM 24 IR > kotlinc-native -target wasm32 Program.kt
 program.wasm + program.wasm.js

Slide 25

Slide 25 text

.so 25 IR > kotlinc-native -target android_x64 Program.kt
 program.so

Slide 26

Slide 26 text

.kexe 26 IR

Slide 27

Slide 27 text

K/N ON iOS APP ReX

Slide 28

Slide 28 text

Use case 28 Use case: refactoring with K/N of data model in a 100% Swift TV channels app

Slide 29

Slide 29 text

Tools 29 https://kotlinlang.org/docs/tutorials/native/apple-framework.html

Slide 30

Slide 30 text

Source sets 30 Possible to define several source sets, with EXPECTed methods and ACTUAL definitions based on targets. Project libs src common android ios expect (class, val…) actual (class, val…) actual (class, val…)

Slide 31

Slide 31 text

.h 31 .h file for the whole framework

Slide 32

Slide 32 text

K/N ON iOS APP Companion object Static field

Slide 33

Slide 33 text

33 [Kotlin] Use the COMPANION

Slide 34

Slide 34 text

34 [.h] Definition of INTERNAL CLASS Companion with INSTANCE METHOD

Slide 35

Slide 35 text

35 [Swift] No STATIC method, need to call the COMPANION OBJECT

Slide 36

Slide 36 text

K/N ON iOS APP Interface Protocol

Slide 37

Slide 37 text

37 [Kotlin]

Slide 38

Slide 38 text

38 [.h]

Slide 39

Slide 39 text

39 [Swift] Must use a Swift CLASS to inherit from the interface!

Slide 40

Slide 40 text

40 [Swift] "Non-class type cannot conform to class protocol"… even if Swift STRUCTs are used a lot!

Slide 41

Slide 41 text

K/N ON iOS APP Enum

Slide 42

Slide 42 text

42

Slide 43

Slide 43 text

43 [.h] Polymorphism declared with the super class

Slide 44

Slide 44 text

44 [Swift] The Kotlin SCREAMING_SNAKE_CASE convention has ben replaced by the camelCase Swift convention

Slide 45

Slide 45 text

K/N ON iOS APP Objects
 Class & struct

Slide 46

Slide 46 text

46 [Kotlin]

Slide 47

Slide 47 text

47

Slide 48

Slide 48 text

48 [Swift] A Kotlin DATA CLASS is converted to a CLASS, not a STRUCT!

Slide 49

Slide 49 text

K/N ON iOS APP Generics

Slide 50

Slide 50 text

50 [Kotlin] Use of a generic parameter T without condition on it

Slide 51

Slide 51 text

51 [.h] A NULLABLE object is used

Slide 52

Slide 52 text

52 [Swift] An OPTIONAL ANY object is used, no generic here

Slide 53

Slide 53 text

53 [Kotlin] Use of a generic parameter T with 1 condition

Slide 54

Slide 54 text

54 [.h] Condition on generic parameter is applied

Slide 55

Slide 55 text

55 [Swift] No use of generic on this side, only the TYPE in the Kotlin condition

Slide 56

Slide 56 text

56 [Kotlin] Use of a generic parameter T with 2 conditions

Slide 57

Slide 57 text

57 [.h] ONLY THE 1st condition in Kotlin is applied, not the 2nd nor both

Slide 58

Slide 58 text

58 [Swift] No use of generic on this side, only the TYPE of the 1st condition in Kotlin is used

Slide 59

Slide 59 text

59 [Swift] Beware with COMPOSITION: "Conformance of generic class to @objc protocol cannot be in an extension"

Slide 60

Slide 60 text

K/N ON iOS APP Exceptions

Slide 61

Slide 61 text

61 [Kotlin]

Slide 62

Slide 62 text

62 [.h] Polymorphism remains applied in structures definition

Slide 63

Slide 63 text

63 [Swift] Exception handling is NOT FULLY implemented with Kotlin/Native 1.3

Slide 64

Slide 64 text

64 [Swift] "Exceptions are propagated […] as NSError only if method has or inherits @Throws annotation" ( ) Except with kotlin.Error and RuntimeException which are not propagated * *

Slide 65

Slide 65 text

K/N ON iOS APP Usual types

Slide 66

Slide 66 text

Types 66

Slide 67

Slide 67 text

Types 67

Slide 68

Slide 68 text

Casting 68 Beware with types: CAST may be needed

Slide 69

Slide 69 text

AND?

Slide 70

Slide 70 text

Things to note • Kotlin project can be a dependance or a full program with a main • Platforms libs ensure interoperability • A specific environment may be mandatory to compile for a specific platform 70

Slide 71

Slide 71 text

71 The standard APIs of the target platform are available as DEPENDENCIES in IntelliJ IDEA

Slide 72

Slide 72 text

72 KOTLIN code loading HTML page using a POD and displaying it inside a UITextView of the target from APPLE UIKit

Slide 73

Slide 73 text

Limits • Web Assembly / JS • No exceptions management • No efficient memory management • No debugging • iOS • Beware with the Objective-C layer • Some Java APIs are not available • java.util.Date, java.math.BigDecimal
 ! Must use platforms libs like POSIX 73

Slide 74

Slide 74 text

Conclusion • K/N interesting for data model or work logic • Good integration in iOS projects • Gradle’s black magic is helpful (but heavy) • Kotlin extensions bring multiplatforms API 74

Slide 75

Slide 75 text

Ready for production? 75 Not today? It seems IceRock and Rekab use Kotlin/Native. « This beta version of Kotlin/Native technology is not yet tuned for benchmarking and competitive analysis of any kind. » GitHub

Slide 76

Slide 76 text

THANK/YOU! Questions?

Slide 77

Slide 77 text

RESOURCES

Slide 78

Slide 78 text

To go further • Building Multiplatform Projects with Gradle – kotlinlang.org • Getting started with Kotlin/Native – sakib.ninja • JetBrains / kotlin-native – GitHub • JetBrains / kotlin-native / samples – GitHub • Kotlin Multiplatform - Medium.com, Mehdi Sohrabi • Kotlin on Android. Now official – blog.jetbrains.com, Maxim Shafirov • Kotlin/Native + CLANG, travel notes – Nikolai Igotti (JetBrains) • Kotlin/Native and Multiplatform – raywenderlich.com • Kotlin/Native as an Apple Framework – kotlinlang.org • Kotlin/Native interoperability – kotlinlang.org • Kotlin/Native interoperability with Swift/Objective-C – kotlin.org • LLVM – Chris Lattner • My first experience with Kotlin Native – Medium.com, Florent Champigny • Plateform-Specific implementations in Kotlin Multiplatform - medium.com, Simone Civetta • Targeting iOS and Android with Koltin Multiplatform – play.kotlinlang.og • The magic of Kotlin/Native (1, 2, 3) – Medium.com, Vivek Singh • What is LLVM? The power behind Swift, Rust, Clang, and more – InfoWorld.com, Serdar Yegulalp • Why we need Kotlin/Native - Medium.com, Jeremy Rempel 78

Slide 79

Slide 79 text

Credits • Native_overview.png – modified with Gimp • Capture of Captain America: Civil War – Marvel Studios ; Marvel Entertainment ; Russo Brothers ; Vita-Ray Productions, LLC • Bugdroid, la mascotte représentant Android – CC-BY-SA 3.0, Google Inc. • Java free icon – Flaticon Basic License, Freepik • Hero.svg – modified with Gim • Logo de iOS – public domain, Apple Inc. • Logo de macOS – public domain, Apple Inc. • Logo de watchOS – public domain, Apple Inc. • Logo de tvOS – public domain, Apple Inc. • Logo de Android – public domain • Logo de Windows – public domain • Logo de Linux – CC0, Larry Ewing, Simon Budig, Garrett LeSage • Logo de Raspberry Pi – Copyright Apache Cordova • Logo de Web Assembly – CC0, Carlos Beraza • Logo de JavaScript – public domain, Chris Williams • Engine free icon – Flaticon Basic License, Flat Icons 79 • Binary code free icon – Flaticon Basic License, Freepik • Logo de IntelliJ IDEA – public domain, JetBrains • Logo de Xcode – Apple • Logo de Swift – public domain, modified with Gimp • Logo of Gradle • Code samples wrapped with Carbon • iPhone mockup made with Smartmockups • Logo of Code d’Armor – Copyright © Code d’Armor • Logo of DevFest du Bout du Monde - Copyright © DevFest du Bout du Monde • Logo of Orange – public domain, Cbuckley • Logo of DevFest 3019 and wallpaper – Copyright © GDG Strasbourg • Memes created with imgflip.com In random order

Slide 80

Slide 80 text

No content