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 full-size slide

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

    View full-size slide

  3. KOTLIN
    NATIVE

    View full-size slide

  4. ABOUT

    KOTLIN

    View full-size slide

  5. 5
    Source: Open Street Map

    View full-size 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 full-size slide

  7. < 17 may 2017
    8

    View full-size slide

  8. Grand’pa Java
    9
    😔

    View full-size slide

  9. 17 may 2017!
    10

    View full-size slide

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

    View full-size slide

  11. KOTLIN/NATIVE

    View full-size slide

  12. 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 full-size slide

  13. 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 full-size slide

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

    View full-size slide

  15. Targets
    17
    x86
    ARM
    MIPS

    View full-size slide

  16. KOTLIN/NATIVE
    Under the hood

    View full-size slide

  17. Platform libs
    19

    POSIX
    Apple frameworks
    W3C DOM
    Win32


    x86
    ARM
    MIPS

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    program.wasm + program.wasm.js

    View full-size slide

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

    program.so

    View full-size slide

  23. K/N ON iOS APP
    ReX

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  26. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  29. 33
    [Kotlin] Use the COMPANION

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  32. K/N ON iOS APP
    Interface
    Protocol

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. K/N ON iOS APP
    Enum

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  38. K/N ON iOS APP
    Objects

    Class & struct

    View full-size slide

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

    View full-size slide

  40. K/N ON iOS APP
    Generics

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  51. K/N ON iOS APP
    Exceptions

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  54. 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 full-size slide

  55. K/N ON iOS APP
    Usual types

    View full-size slide

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

    View full-size slide

  57. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  60. 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 full-size slide

  61. 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 full-size slide

  62. 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 full-size slide

  63. THANK/YOU!
    Questions?

    View full-size slide

  64. 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 full-size slide

  65. 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 full-size slide