Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developing Multiplatform Projects in Kotlin 1.2

Developing Multiplatform Projects in Kotlin 1.2

As presented on JEEConf 2018

Dmitry Jemerov

May 18, 2018
Tweet

Other Decks in Programming

Transcript

  1. Shared business logic, platform-specific UI • Share • Data model

    • Validation rules • Network client code • HTML generation • Platform-specific • UI • Usages of unique APIs on each platform
  2. Common libraries • Standard library • Collections and sequences •

    Functional operations on collections (filter, map etc.) • Higher-order utility functions (with, apply etc.) • Exceptions • Testing • Higher-level infrastructure libraries
  3. package kotlin.date expect class Date { … fun getFullYear(): Int

    … } Common JS package kotlin.date actual external class Date { actual fun getFullYear(): Int } package kotlin.date actual class Date { private val calendar: Calendar … actual fun getFullYear() = calendar[YEAR] … } JVM
  4. fun isSameDay(from: Date, to: Date) = from.getFullYear() !== to.getFullYear() !&&

    !!... package kotlin.date expect class Date { … fun getFullYear(): Int … } Common
  5. Why not interfaces? Common expect class Date { constructor(value: Number)

    } expect fun parseDate(dateString: String): Date expect fun Date.toReadableDateString(): String
  6. Reusing existing implementations package kotlin.test actual typealias Test = org.junit.Test

    JVM package kotlin.test expect annotation class Test() Common
  7. Building a common module apply plugin: 'kotlin-platform-common' dependencies { compile

    "org.jetbrains.kotlin:kotlin-stdlib-common:$version" } Common
  8. Common *.kt Kotlin/JS *.kt, *.js *.js Kotlin/JVM *.kt, *.java *.jar

    Common Libraries *.kotlin_metadata Kotlin/Native *.kt *.klib
  9. kotlin.test import kotlin.test.Test import kotlin.test.assertEquals class DateTest { @Test fun

    testParse() { val date = parseDate("2017-11-02") assertEquals(2017, date.getFullYear()) } }
  10. kotlinx.coroutines fun main(args: Array<String>) = runBlocking<Unit> { val time =

    measureTimeMillis { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } println("The answer is ${one.await() + two.await()}") } println("Completed in $time ms") }
  11. cinterop • Clang-based tool to generate Kotlin metadata from C

    header files • Supports all C types: strings, pointers, structs, callbacks • Supports Objective-C types
  12. iOS libraries fun showGameCenter() { val gkViewController = GKGameCenterViewController().apply {

    gameCenterDelegate = this@ViewController viewState = GKGameCenterViewControllerStateLeaderboards leaderboardTimeScope = GKLeaderboardTimeScopeToday leaderboardCategory = "main" } this.presentViewController(gkViewController, 
 animated = true, completion = null) }
  13. Kotlin/Native iOS Interop • Compiling Kotlin/Native module to iOS framework

    • Autogeneration of .h files • Easy conversions between Kotlin and ObjC types (NSString, NSArray, NSDictionary)
  14. Kotlin/Native Memory management • Reference counting + cycle collector •

    No shared mutable state between threads • Support of frozen objects • Still under design; final version might be different
  15. Kotlin/Native Target Platforms • Windows, Linux, macOS • iOS, Android

    • WebAssembly • Embedded (STM32 board currently supported)
  16. Tooling • Plugin for CLion and AppCode • All features

    of regular Kotlin plugin • Debugger • Test runner
  17. External declarations external abstract class Window { val location: Location

    fun alert(message: String) !// … } external val window: Window
  18. React support • Official bindings for react • https://github.com/jetbrains/kotlin-wrappers •

    CLI tool to create React application • npm install -g create-react-kotlin-app • create-react-kotlin-app my-app
  19. kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle

    using webpack • Run tests using Karma • Hot reload of changes
  20. Summary • Kotlin's goal is to allow you to write

    all parts of your app in the same language • Full interoperability on every supported platform • Shared business logic, platform-specific UI • JVM/JS code reuse available today, native coming soon