architecting a kotlin jvm and js multiplatform project
play

Architecting a Kotlin JVM and JS multiplatform project FELIPE LIMA - PowerPoint PPT Presentation

FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF Architecting a Kotlin JVM and JS multiplatform project FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF Architecting a Kotlin JVM and JS multiplatform project INTRODUCTION Yet another cross platform


  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, How it works Titanium, Cordova, etc. • Quite unlike all other options • Ideal for business logic code sharing

  5. TO MAKE IT CLEAR Kotlin Multiplatform ≠ React Native

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

  7. Common JVM Kotlin/Native Javascript Android

  8. Common kotlinc Kotlin/Native kotlin2js

  9. Common kotlinc Kotlin/Native kotlin2js JVM Android

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

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

  12. Gradle plugins apply plugin : 'kotlin-platform-common'

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

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

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

  16. Gradle plugins apply plugin : ‘konan’

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

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

  19. KEY CONCEPTS

  20. Shared module Platform module expect actual L

  21. Common 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 }

  22. JVM 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() )

  23. Common expect class Decimal

  24. JVM actual typealias Decimal = BigDecimal

  25. JS actual typealias Decimal = Double

  26. Common expect fun currentTimeMs(): Long

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

  28. Kotlin/Native actual fun currentTimeMs(): Long { memScoped { val now = alloc<timeval>() gettimeofday(now.ptr, null ) return (now.tv_sec.toLong() * 1000) + (now.tv_usec.toLong() / 1000) } }

  29. • Simpler implementation (no factory classes or dep. injection) • Interfaces cannot have constructors • All implementations are known at compile time Why not interfaces? • More flexibility • Top level and extension functions are supported

  30. Common module LIMITATIONS AND CAVEATS • Cannot reference any platform specific code • Can only have Kotlin code • Can only depend on other Kotlin common modules or libraries

  31. DEEP DIVE

  32. Story time

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

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

  35. PPU Bitmap GLSurfaceView OpenGL ES

  36. expect class Bitmap constructor ( width: Int, height: Int ) { fun setPixel(x: Int, y: Int, value: Int) }

  37. package android.graphics; public final class Bitmap implements Parcelable { �/0 ��../ }

  38. 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) } }

  39. actual typealias Bitmap = android.graphics.Bitmap

  40. PPU IntArray GLSurfaceView OpenGL ES

  41. internal class PPU( internal var buffer : IntArray = IntArray( IMG_WIDTH * IMG_HEIGHT ) �/0 ��../ }

  42. KEY TAKE AWAYS

  43. • Support is still experimental, expect rough edges and breaking changes • Very exciting technology Key take aways • 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

  44. • Makes no assumptions about your system architecture • Not a framework, just a platform Key take aways • Has the potential to turn into an entire ecosystem • Probably will require bigger organizational changes

  45. THANK YOU!

Recommend


More recommend