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

Kotlin/Native - DevFest Strasbourg 2019

Kotlin/Native - DevFest Strasbourg 2019

Kotlin/Native... what's that?
How does it work?
It permits to use Kotlin code in iOS app written in Swift, but how?
Can we use it in production?

Let met explain this project made by JetBrains which has the aim to run Kotlin binaries on many platforms. I will also make a return of experience with the integration of Kotlin code as Xcode framework for an iOS app, with its benefits and limits.

This talk has been made for DevFest Strasbourg 2019.
Slidedeck under CC-BY-SA.

You enjoyed reading these slides? Buy me a beer, thanks<3
(https://pylapersonne.info/buymeacoffee)

Pierre-Yves Lapersonne

October 23, 2019
Tweet

More Decks by Pierre-Yves Lapersonne

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

  3. KOTLIN
    NATIVE

    View Slide

  4. ABOUT

    KOTLIN

    View Slide

  5. 5
    Source: Open Street Map

    View Slide

  6. 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

    View Slide

  7. 7

    View Slide

  8. < 17 may 2017
    8

    View Slide

  9. Grand’pa Java
    9
    😔

    View Slide

  10. 17 may 2017!
    10

    View Slide

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

    View Slide

  12. 12

    View Slide

  13. KOTLIN/NATIVE

    View Slide

  14. 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

    View Slide

  15. 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

    View Slide

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

    View Slide

  17. Targets
    17
    x86
    ARM
    MIPS

    View Slide

  18. KOTLIN/NATIVE
    Under the hood

    View Slide

  19. Platform libs
    19

    POSIX
    Apple frameworks
    W3C DOM
    Win32


    x86
    ARM
    MIPS

    View Slide

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

    View Slide

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

    View Slide

  22. .kt
    22

    View Slide

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

    View Slide

  24. WASM
    24
    IR
    > kotlinc-native -target wasm32 Program.kt

    program.wasm + program.wasm.js

    View Slide

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

    program.so

    View Slide

  26. .kexe
    26
    IR

    View Slide

  27. K/N ON iOS APP
    ReX

    View Slide

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

    View Slide

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

    View Slide

  30. 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…)

    View Slide

  31. .h
    31
    .h file for the whole framework

    View Slide

  32. K/N ON iOS APP
    Companion object
    Static field

    View Slide

  33. 33
    [Kotlin] Use the COMPANION

    View Slide

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

    View Slide

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

    View Slide

  36. K/N ON iOS APP
    Interface
    Protocol

    View Slide

  37. 37
    [Kotlin]

    View Slide

  38. 38
    [.h]

    View Slide

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

    View Slide

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

    View Slide

  41. K/N ON iOS APP
    Enum

    View Slide

  42. 42

    View Slide

  43. 43
    [.h] Polymorphism declared with the super class

    View Slide

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

    View Slide

  45. K/N ON iOS APP
    Objects

    Class & struct

    View Slide

  46. 46
    [Kotlin]

    View Slide

  47. 47

    View Slide

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

    View Slide

  49. K/N ON iOS APP
    Generics

    View Slide

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

    View Slide

  51. 51
    [.h] A NULLABLE object is used

    View Slide

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

    View Slide

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

    View Slide

  54. 54
    [.h] Condition on generic parameter is applied

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  60. K/N ON iOS APP
    Exceptions

    View Slide

  61. 61
    [Kotlin]

    View Slide

  62. 62
    [.h] Polymorphism remains applied in structures definition

    View Slide

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

    View Slide

  64. 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
    *
    *

    View Slide

  65. K/N ON iOS APP
    Usual types

    View Slide

  66. Types
    66

    View Slide

  67. Types
    67

    View Slide

  68. Casting
    68
    Beware with types: CAST may be needed

    View Slide

  69. AND?

    View Slide

  70. 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

    View Slide

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

    View Slide

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

    View Slide

  73. 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

    View Slide

  74. 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

    View Slide

  75. 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

    View Slide

  76. THANK/YOU!
    Questions?

    View Slide

  77. RESOURCES

    View Slide

  78. 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

    View Slide

  79. 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

    View Slide

  80. View Slide