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 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
TO MAKE IT CLEAR Kotlin Multiplatform ≠ React Native
TO MAKE IT CLEAR Kotlin Multiplatform > C/C++
Common JVM Kotlin/Native Javascript Android
Common kotlinc Kotlin/Native kotlin2js
Common kotlinc Kotlin/Native kotlin2js JVM Android
Common kotlinc Kotlin/Native kotlin2js JVM Android Executable Dynamic lib iOS
Common kotlinc Kotlin/Native kotlin2js JVM Android Executable Dynamic lib iOS Javascript A
Gradle plugins 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’
Declaring dependencies dependencies { expectedBy project( ':common' ) }
KEY CONCEPTS
Shared module Platform module expect actual L
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 }
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() )
Common expect class Decimal
JVM actual typealias Decimal = BigDecimal
JS actual typealias Decimal = Double
Common expect fun currentTimeMs(): Long
JVM actual fun currentTimeMs(): Long { return System.currentTimeMillis() }
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) } }
• 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
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
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 GLSurfaceView OpenGL ES
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 GLSurfaceView OpenGL ES
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 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
• 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
THANK YOU!
Recommend
More recommend