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

Fde10bcf0813b2162545477be4e7470b?s=128

Eugenio Marletti

November 03, 2017
Tweet

Transcript

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

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

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

    “ ” “ ”
  4. sharing

  5. code sharing

  6. code sharing

  7. business logic sharing

  8. bugs sharing

  9. bugs sharing

  10. fixes sharing

  11. effort sharing

  12. C++ J2ObjC / GWT JavaScript

  13. None
  14. THE ALGORITHM™

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

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

  17. C++ ObjC++ JavaScript TypeScript iOS !" JavaScriptCore Android !" W

    ͊̊̏̃͑̽̃̕ ̘͖̯ 㸅 ̨͇̯ ̶ȩ̧̟̝͔ ͔͎̗̬̼̓̒̄̍̓͐̒̎̕ b̡̫̰͙͚͋͋̓́̇͟V ͐̇͞ ̓̿̔̿̓ ͒̚ ͟ ̨̭̳̱͕ ̵i ͒̅̉͊̌̇ ̡͓̰̲̳͟ ̥̯ ̯̦ę͕͔͉̜̗͔͎ ̔̍̀̏̌̕ ͢ ͟ w ̛͊͆̍͑ ̴̯̯̻̪̦
  18. ? C++ ObjC++ JavaScript TypeScript iOS !" JavaScriptCore Android !"

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

  20. multi-platform

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

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

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

  24. ANDROID WebView iOS JavaScriptCore BACKEND JS legacy.js

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

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

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

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

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

  30. /** * A day number, offset from 01/01/2012 !" export

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

    Day = Int
  32. 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) } }
  33. < insert more talking here >

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