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

Sharing [Kotlin code across platforms] is caring!

Sharing [Kotlin code across platforms] is caring!

Sharing code across platforms is one of the most annoying "unsolved problems" when developing a product: an eternal struggle between re-writing everything multiple times and jumping through plenty of hoops to be able to re-use some core business logic. This session is going to illustrate how Kotlin is being used to solve this problem at Clue in order to share code between Android, iOS and the backend, including all the challenges, lessons and tricks learned along the way.

Presented at KotlinConf 2017

Eugenio Marletti

November 03, 2017
Tweet

More Decks by Eugenio Marletti

Other Decks in Technology

Transcript

  1. @workingkills
    Eugenio Marletti
    Sharing
    [Kotlin code across platforms]
    is caring!
    KotlinConf

    View full-size slide

  2. “app”
    “ ”
    “ ”
    “ ”
    “ ”

    View full-size slide

  3. “app”
    iOS
    Android
    frontend
    backend
    tools
    “ ”
    “ ”
    “ ”
    “ ”

    View full-size slide

  4. business logic
    sharing

    View full-size slide

  5. fixes
    sharing

    View full-size slide

  6. effort
    sharing

    View full-size slide

  7. C++
    J2ObjC / GWT
    JavaScript

    View full-size slide

  8. THE ALGORITHM™

    View full-size slide

  9. THE ALGORITHM™
    C++
    ObjC++
    JavaScript
    TypeScript

    View full-size slide

  10. C++
    ObjC++
    JavaScript
    TypeScript
    iOS !" JavaScriptCore
    Android !" WebView

    View full-size slide

  11. C++
    ObjC++
    JavaScript
    TypeScript
    iOS !" JavaScriptCore
    Android !" W
    ͊̊̏̃͑̽̃̕
    ̘͖̯

    ̨͇̯
    ̶ȩ̧̟̝͔
    ͔͎̗̬̼̓̒̄̍̓͐̒̎̕
    b̡̫̰͙͚͋͋̓́̇͟V
    ͐̇͞ ̓̿̔̿̓ ͒̚
    ͟ ̨̭̳̱͕
    ̵i
    ͒̅̉͊̌̇
    ̡͓̰̲̳͟ ̥̯
    ̯̦ę͕͔͉̜̗͔͎
    ̔̍̀̏̌̕
    ͢ ͟
    w
    ̛͊͆̍͑
    ̴̯̯̻̪̦

    View full-size slide

  12. ?
    C++
    ObjC++
    JavaScript
    TypeScript
    iOS !" JavaScriptCore
    Android !"

    View full-size slide

  13. THE ALGORITHM™
    C++
    ObjC++
    JavaScript
    TypeScript
    Kotlin @fbecart
    Florent Bécart

    View full-size slide

  14. multi-platform

    View full-size slide

  15. header impl
    (before 1.2)
    -Xmulti-platform

    View full-size slide

  16. header impl
    (before 1.2)
    -Xmulti-platform

    View full-size slide

  17. expect actual
    (from 1.2)
    -Xmulti-platform

    View full-size slide

  18. ANDROID
    WebView
    iOS
    JavaScriptCore
    BACKEND
    JS
    legacy.js

    View full-size slide

  19. legacy-test.js
    ANDROID
    WebView
    iOS
    JavaScriptCore
    BACKEND
    JS
    legacy.js
    shared-test.kt
    shared.kt

    View full-size slide

  20. legacy-test.js
    ANDROID
    WebView
    iOS
    JavaScriptCore
    BACKEND
    JS
    legacy.js
    shared-test.kt
    shared.kt

    View full-size slide

  21. legacy-test.js
    ANDROID
    WebView
    iOS
    JavaScriptCore
    BACKEND
    JS
    legacy.js
    shared-test.kt
    shared.kt

    View full-size slide

  22. legacy-test.js
    ANDROID
    WebView
    iOS
    JavaScriptCore
    BACKEND
    JS
    legacy.js
    shared.kt
    shared-test.kt

    View full-size slide

  23. legacy-test.js
    ANDROID
    WebView
    iOS
    JavaScriptCore
    BACKEND
    JS
    shared.kt
    shared-test.kt
    JVM

    View full-size slide

  24. /**
    * A day number, offset from 01/01/2012
    !"
    export type Day = number

    View full-size slide

  25. /**
    * A day number, offset from 01/01/2012
    !"
    typealias Day = Int

    View full-size slide

  26. class Value {
    constructor(value: Double) {
    this.value = value
    }
    val value: Double
    override fun toString(): String {
    return this.value.toString()
    }
    companion object {
    fun add(v1: Value, v2: Value): Value {
    return Value(v1.value + v2.value)
    }
    fun isZero(valueWithVariance: Value): Boolean {
    return .0 !!% valueWithVariance.value
    }
    fun subtract(v1: Value?, v2: Value?): Value {
    var value = .0
    if (v1 !' null !& v2 !' null) { value = v1.value - v2.value }
    return Value(value)
    }
    }
    }
    export class Value {
    constructor(value) {
    this.value = value
    }
    readonly value: number
    toString() {
    return String(this.value)
    }
    static add = (v1: Value, v2: Value): Value !$ {
    return new Value(v1.value + v2.value)
    }
    static isZero = (valueWithVariance: Value): boolean !$ {
    return 0 !!% valueWithVariance.value
    }
    static subtract = (v1: Value, v2: Value): Value !$ {
    let value = 0
    if (v1 !& v2) { value = v1.value - v2.value }
    return new Value(value)
    }
    }

    View full-size slide

  27. < insert more talking here >

    View full-size slide

  28. Eugenio
    Marletti
    @workingkills
    BIT.LY/SHARING-KOTLIN-IS-CARING

    View full-size slide