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

architecting a kotlin jvm and js multiplatform project
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF

Architecting a Kotlin JVM and JS multiplatform project

slide-2
SLIDE 2

FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF

Architecting a Kotlin JVM and JS multiplatform project

slide-3
SLIDE 3

INTRODUCTION

slide-4
SLIDE 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

slide-5
SLIDE 5
slide-6
SLIDE 6

Kotlin Multiplatform ≠ React Native

TO MAKE IT CLEAR

slide-7
SLIDE 7

Kotlin Multiplatform > C/C++

TO MAKE IT CLEAR

slide-8
SLIDE 8

Common JVM Kotlin/Native Javascript Android

slide-9
SLIDE 9

Common Kotlin/Native kotlinc kotlin2js

slide-10
SLIDE 10

Common Kotlin/Native JVM kotlinc Android kotlin2js

slide-11
SLIDE 11

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

slide-12
SLIDE 12

A

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

slide-13
SLIDE 13

apply plugin: 'kotlin-platform-common'

Gradle plugins

slide-14
SLIDE 14

apply plugin: ‘kotlin-platform-android'

Gradle plugins

slide-15
SLIDE 15

apply plugin: ‘kotlin-platform-jvm’

Gradle plugins

slide-16
SLIDE 16

apply plugin: ‘kotlin-platform-js’

Gradle plugins

slide-17
SLIDE 17

apply plugin: ‘konan’

Gradle plugins

slide-18
SLIDE 18

apply plugin: ‘org.jetbrains.kotlin.frontend’

Gradle plugins

slide-19
SLIDE 19

dependencies { expectedBy project(':common') }

Declaring dependencies

slide-20
SLIDE 20

KEY CONCEPTS

slide-21
SLIDE 21

L

expect

Shared module

actual

Platform module

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 24

expect class Decimal

Common

slide-25
SLIDE 25

actual typealias Decimal = BigDecimal

JVM

slide-26
SLIDE 26

actual typealias Decimal = Double

JS

slide-27
SLIDE 27

expect fun currentTimeMs(): Long

Common

slide-28
SLIDE 28

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

JVM

slide-29
SLIDE 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

slide-30
SLIDE 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?

slide-31
SLIDE 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

slide-32
SLIDE 32

DEEP DIVE

slide-33
SLIDE 33

Story time

slide-34
SLIDE 34
slide-35
SLIDE 35
slide-36
SLIDE 36
slide-37
SLIDE 37
slide-38
SLIDE 38

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

slide-39
SLIDE 39

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

slide-40
SLIDE 40

PPU Bitmap OpenGL ES GLSurfaceView

slide-41
SLIDE 41

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

slide-42
SLIDE 42

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

slide-43
SLIDE 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) } }

slide-44
SLIDE 44

actual typealias Bitmap = android.graphics.Bitmap

slide-45
SLIDE 45

PPU IntArray OpenGL ES GLSurfaceView

slide-46
SLIDE 46

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

slide-47
SLIDE 47

KEY TAKE AWAYS

slide-48
SLIDE 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

slide-49
SLIDE 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

slide-50
SLIDE 50

THANK YOU!