Architecting a Kotlin multiplatform project

7fd4ba468da56bb5330a6352c1b54f52?s=47 felipecsl
October 04, 2018

Architecting a Kotlin multiplatform project

7fd4ba468da56bb5330a6352c1b54f52?s=128

felipecsl

October 04, 2018
Tweet

Transcript

  1. FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF Architecting a

    Kotlin JVM and JS multiplatform project
  2. FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF Architecting a

    Kotlin JVM and JS multiplatform project
  3. INTRODUCTION

  4. • Yet another cross platform framework? • Not all of

    them are created equal • Several options: React Native, Flutter, Xamarin, PhoneGap, Titanium, Cordova, etc. • Quite unlike all other options • Ideal for business logic code sharing How it works
  5. None
  6. Kotlin Multiplatform ≠ React Native TO MAKE IT CLEAR

  7. Kotlin Multiplatform > C/C++ TO MAKE IT CLEAR

  8. Common JVM Kotlin/Native Javascript Android

  9. Common Kotlin/Native kotlinc kotlin2js

  10. Common Kotlin/Native JVM kotlinc Android kotlin2js

  11. Common Kotlin/Native Dynamic lib iOS Executable JVM kotlinc Android kotlin2js

  12. A Common Kotlin/Native Dynamic lib iOS Executable JVM kotlinc Android

    kotlin2js Javascript
  13. apply plugin: 'kotlin-platform-common' Gradle plugins

  14. apply plugin: ‘kotlin-platform-android' Gradle plugins

  15. apply plugin: ‘kotlin-platform-jvm’ Gradle plugins

  16. apply plugin: ‘kotlin-platform-js’ Gradle plugins

  17. apply plugin: ‘konan’ Gradle plugins

  18. apply plugin: ‘org.jetbrains.kotlin.frontend’ Gradle plugins

  19. dependencies { expectedBy project(':common') } Declaring dependencies

  20. KEY CONCEPTS

  21. L expect Shared module actual Platform module

  22. expect class Order { val id: Int val userId: Int

    val amount: Decimal val feePercent: Decimal val price: Decimal val coinPair: CoinPair val status: OrderStatus val type: OrderType } Common
  23. actual data class Order( actual val id: Int, actual val

    userId: Int, actual val amount: Decimal, actual val feePercent: Decimal, actual val price: Decimal, actual val coinPair: CoinPair, actual val status: OrderStatus, actual val type: OrderType, val createdAt: DateTime = DateTime.now(), val updatedAt: DateTime = DateTime.now() ) JVM
  24. expect class Decimal Common

  25. actual typealias Decimal = BigDecimal JVM

  26. actual typealias Decimal = Double JS

  27. expect fun currentTimeMs(): Long Common

  28. actual fun currentTimeMs(): Long { return System.currentTimeMillis() } JVM

  29. actual fun currentTimeMs(): Long { memScoped { val now =

    alloc<timeval>() gettimeofday(now.ptr, null) return (now.tv_sec.toLong() * 1000) + (now.tv_usec.toLong() / 1000) } } Kotlin/Native
  30. • Simpler implementation (no factory classes or dep. injection) •

    Interfaces cannot have constructors • All implementations are known at compile time • More flexibility • Top level and extension functions are supported Why not interfaces?
  31. • Cannot reference any platform specific code • Can only

    have Kotlin code • Can only depend on other Kotlin common modules or libraries Common module LIMITATIONS AND CAVEATS
  32. DEEP DIVE

  33. Story time

  34. None
  35. None
  36. None
  37. None
  38. Console APU PPU CPU Mapper Audio buffer Video buffer Audio

    device Video device
  39. Console APU PPU CPU Mapper Audio buffer Video buffer Audio

    device Video device
  40. PPU Bitmap OpenGL ES GLSurfaceView

  41. expect class Bitmap constructor( width: Int, height: Int ) {

    fun setPixel(x: Int, y: Int, value: Int) }
  42. package android.graphics; public final class Bitmap implements Parcelable { //

    ... }
  43. typealias AndroidBitmap = android.graphics.Bitmap actual class Bitmap actual constructor(width: Int,

    height: Int) { private val delegate: AndroidBitmap = AndroidBitmap.createBitmap(width, height, RGB_565) actual fun setPixel(x: Int, y: Int, value: Int) { delegate.setPixel(x, y, value) } }
  44. actual typealias Bitmap = android.graphics.Bitmap

  45. PPU IntArray OpenGL ES GLSurfaceView

  46. internal class PPU( internal var buffer: IntArray = IntArray(IMG_WIDTH *

    IMG_HEIGHT) // ... }
  47. KEY TAKE AWAYS

  48. • Support is still experimental, expect rough edges and breaking

    changes • Very exciting technology • Benefits from a large and quickly growing Kotlin community • Expect the usual top notch tooling support by Jetbrains • You can start trying it out using it right now Key take aways
  49. • Makes no assumptions about your system architecture • Not

    a framework, just a platform • Has the potential to turn into an entire ecosystem • Probably will require bigger organizational changes Key take aways
  50. THANK YOU!