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

Architecting a Kotlin multiplatform project

felipecsl
October 04, 2018

Architecting a Kotlin multiplatform project

felipecsl

October 04, 2018
Tweet

More Decks by felipecsl

Other Decks in Programming

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. • 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
  4. 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
  5. 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
  6. 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
  7. • 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?
  8. • 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
  9. expect class Bitmap constructor( width: Int, height: Int ) {

    fun setPixel(x: Int, y: Int, value: Int) }
  10. 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) } }
  11. • 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
  12. • 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