Pro Yearly is on sale from $80 to $50! »

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 will be made for DevFest Strasbourg 2019 ;-)

323bb1cb39e6478e559b6e13d2fdf518?s=128

Pierre-Yves Lapersonne

October 23, 2019
Tweet

Transcript

  1. Release 5 – 24/10/2019 ✿✿✿✿ ʕ •ᴥ•ʔ/ ︻デ═⼀一 This document

    is available under terms of Creative Commons with CC-BY-SA 3.0
  2. Pierre-Yves Lapersonne Android & iOS apps developer pylapersonne.info @pylapp

  3. KOTLIN NATIVE

  4. ABOUT
 KOTLIN

  5. 5 Source: Open Street Map

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

  8. < 17 may 2017 8

  9. Grand’pa Java 9

  10. 17 may 2017! 10

  11. Kotlin for Android 11 Note: Here is the Kotlin code

    converted from Java code with Android Studio (a bit dirty!!)
  12. 12

  13. KOTLIN/NATIVE

  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
  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
  16. Targets • A lot of platforms… • … where we

    cannot have or don’t want VM 16
  17. Targets 17 x86 ARM MIPS

  18. KOTLIN/NATIVE Under the hood

  19. Platform libs 19 
 POSIX Apple frameworks W3C DOM Win32


    … x86 ARM MIPS
  20. K/N compilation 20 Frontend Backend Kotlin/Native Compiler Kotlin/Native: a part

    of the Kotlin Multiplatform project
  21. K/N compilation 21 Intermediate Representation • Cleaning • Devirtualization LLVM

    Konan
  22. .kt 22

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

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

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

  26. .kexe 26 IR

  27. K/N ON iOS APP ReX

  28. Use case 28 Use case: refactoring with K/N of data

    model in a 100% Swift TV channels app
  29. Tools 29 https://kotlinlang.org/docs/tutorials/native/apple-framework.html

  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…)
  31. .h 31 .h file for the whole framework

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

  33. 33 [Kotlin] Use the COMPANION

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

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

    OBJECT
  36. K/N ON iOS APP Interface Protocol

  37. 37 [Kotlin]

  38. 38 [.h]

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

    the interface!
  40. 40 [Swift] "Non-class type cannot conform to class protocol"… even

    if Swift STRUCTs are used a lot!
  41. K/N ON iOS APP Enum

  42. 42

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

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

    the camelCase Swift convention
  45. K/N ON iOS APP Objects
 Class & struct

  46. 46 [Kotlin]

  47. 47

  48. 48 [Swift] A Kotlin DATA CLASS is converted to a

    CLASS, not a STRUCT!
  49. K/N ON iOS APP Generics

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

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

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

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

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

  55. 55 [Swift] No use of generic on this side, only

    the TYPE in the Kotlin condition
  56. 56 [Kotlin] Use of a generic parameter T with 2

    conditions
  57. 57 [.h] ONLY THE 1st condition in Kotlin is applied,

    not the 2nd nor both
  58. 58 [Swift] No use of generic on this side, only

    the TYPE of the 1st condition in Kotlin is used
  59. 59 [Swift] Beware with COMPOSITION: "Conformance of generic class to

    @objc protocol cannot be in an extension"
  60. K/N ON iOS APP Exceptions

  61. 61 [Kotlin]

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

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

    1.3
  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 * *
  65. K/N ON iOS APP Usual types

  66. Types 66

  67. Types 67

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

  69. AND?

  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
  71. 71 The standard APIs of the target platform are available

    as DEPENDENCIES in IntelliJ IDEA
  72. 72 KOTLIN code loading HTML page using a POD and

    displaying it inside a UITextView of the target from APPLE UIKit
  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
  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
  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
  76. THANK/YOU! Questions?

  77. RESOURCES

  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
  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
  80. None