Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

INTRODUCTION

Slide 4

Slide 4 text

• 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 text

No content

Slide 6

Slide 6 text

Kotlin Multiplatform ≠ React Native TO MAKE IT CLEAR

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Common JVM Kotlin/Native Javascript Android

Slide 9

Slide 9 text

Common Kotlin/Native kotlinc kotlin2js

Slide 10

Slide 10 text

Common Kotlin/Native JVM kotlinc Android kotlin2js

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

apply plugin: ‘konan’ Gradle plugins

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

KEY CONCEPTS

Slide 21

Slide 21 text

L expect Shared module actual Platform module

Slide 22

Slide 22 text

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 text

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 text

expect class Decimal Common

Slide 25

Slide 25 text

actual typealias Decimal = BigDecimal JVM

Slide 26

Slide 26 text

actual typealias Decimal = Double JS

Slide 27

Slide 27 text

expect fun currentTimeMs(): Long Common

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

• 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 text

• 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 text

DEEP DIVE

Slide 33

Slide 33 text

Story time

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

PPU Bitmap OpenGL ES GLSurfaceView

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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 text

actual typealias Bitmap = android.graphics.Bitmap

Slide 45

Slide 45 text

PPU IntArray OpenGL ES GLSurfaceView

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

KEY TAKE AWAYS

Slide 48

Slide 48 text

• 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 text

• 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 text

THANK YOU!