$30 off During Our Annual Pro Sale. View Details »

Architecting a Kotlin multiplatform project

felipecsl
October 04, 2018

Architecting a Kotlin multiplatform project

felipecsl

October 04, 2018
Tweet

More Decks by felipecsl

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

  3. INTRODUCTION

    View 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

    View Slide

  5. View Slide

  6. Kotlin Multiplatform ≠ React Native
    TO MAKE IT CLEAR

    View Slide

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

    View Slide

  8. Common
    JVM Kotlin/Native Javascript Android

    View Slide

  9. Common
    Kotlin/Native
    kotlinc kotlin2js

    View Slide

  10. Common
    Kotlin/Native
    JVM
    kotlinc
    Android
    kotlin2js

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. apply plugin: ‘konan’
    Gradle plugins

    View Slide

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

    View Slide

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

    View Slide

  20. KEY CONCEPTS

    View Slide

  21. L
    expect
    Shared module
    actual
    Platform module

    View 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

    View 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

    View Slide

  24. expect class Decimal
    Common

    View Slide

  25. actual typealias Decimal = BigDecimal
    JVM

    View Slide

  26. actual typealias Decimal = Double
    JS

    View Slide

  27. expect fun currentTimeMs(): Long
    Common

    View Slide

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

    View Slide

  29. 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

    View 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?

    View 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

    View Slide

  32. DEEP DIVE

    View Slide

  33. Story time

    View Slide

  34. View Slide

  35. View Slide

  36. View Slide

  37. View Slide

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

    View Slide

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

    View Slide

  40. PPU
    Bitmap
    OpenGL ES
    GLSurfaceView

    View Slide

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

    View Slide

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

    View 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)
    }
    }

    View Slide

  44. actual typealias Bitmap = android.graphics.Bitmap

    View Slide

  45. PPU
    IntArray
    OpenGL ES
    GLSurfaceView

    View Slide

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

    View Slide

  47. KEY TAKE AWAYS

    View 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

    View 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

    View Slide

  50. THANK YOU!

    View Slide