FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF
Architecting a Kotlin JVM and JS multiplatform project FELIPE LIMA - - PowerPoint PPT Presentation
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
FELIPE LIMA / OCT 4TH, 2018 / KOTLINCONF
Architecting a Kotlin JVM and JS multiplatform project
INTRODUCTION
- 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
Kotlin Multiplatform ≠ React Native
TO MAKE IT CLEAR
Kotlin Multiplatform > C/C++
TO MAKE IT CLEAR
Common JVM Kotlin/Native Javascript Android
Common Kotlin/Native kotlinc kotlin2js
Common Kotlin/Native JVM kotlinc Android kotlin2js
Common Kotlin/Native Dynamic lib iOS Executable JVM kotlinc Android kotlin2js
A
Common Kotlin/Native Dynamic lib iOS Executable JVM kotlinc Android kotlin2js Javascript
apply plugin: 'kotlin-platform-common'
Gradle plugins
apply plugin: ‘kotlin-platform-android'
Gradle plugins
apply plugin: ‘kotlin-platform-jvm’
Gradle plugins
apply plugin: ‘kotlin-platform-js’
Gradle plugins
apply plugin: ‘konan’
Gradle plugins
apply plugin: ‘org.jetbrains.kotlin.frontend’
Gradle plugins
dependencies { expectedBy project(':common') }
Declaring dependencies
KEY CONCEPTS
L
expect
Shared module
actual
Platform module
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
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
expect class Decimal
Common
actual typealias Decimal = BigDecimal
JVM
actual typealias Decimal = Double
JS
expect fun currentTimeMs(): Long
Common
actual fun currentTimeMs(): Long { return System.currentTimeMillis() }
JVM
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
- 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?
- 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
DEEP DIVE
Story time
Console APU PPU CPU Mapper Audio buffer Video buffer Audio device Video device
Console APU PPU CPU Mapper Audio buffer Video buffer Audio device Video device
PPU Bitmap OpenGL ES GLSurfaceView
expect class Bitmap constructor( width: Int, height: Int ) { fun setPixel(x: Int, y: Int, value: Int) }
package android.graphics; public final class Bitmap implements Parcelable { /0 ../ }
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) } }
actual typealias Bitmap = android.graphics.Bitmap
PPU IntArray OpenGL ES GLSurfaceView
internal class PPU( internal var buffer: IntArray = IntArray(IMG_WIDTH * IMG_HEIGHT) /0 ../ }
KEY TAKE AWAYS
- 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
- 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