victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native...

105
Multiplatform projects with Victor Kropp @kropp victor.kropp.name

Transcript of victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native...

Page 1: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Multiplatform projects with

Victor Kropp@kropp

victor.kropp.name

Page 2: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 3: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 4: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 5: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 6: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 7: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 8: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 9: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 10: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 11: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 12: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

https://imgflip.com/i/2kdpb9

Page 13: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

https://twitter.com/timbray/status/822470746773409794

Page 14: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Modern applications

Page 15: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 16: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

CC++C#Objective-CSwift

Java

Objective-CSwift

JavaPythonRuby

JavaScriptTypeScript

Page 17: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

compiled vs. interpreted

Page 18: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

compiled vs. interpreted

static vs. dynamic typing

Page 19: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

compiled vs. interpreted

static vs. dynamic typing

manual vs. automatic memory management

Page 20: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

CC++C#Objective-CSwift

Java

Objective-CSwift

JavaPythonRuby

JavaScriptTypeScript

Page 21: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

CC++C#Objective-CSwift

Java

Objective-CSwift

JavaPythonRuby

JavaScriptTypeScript

Page 22: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 23: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 24: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Kotlin

Kotlin

Kotlin

Kotlin

Kotlin

Page 25: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Kotlin/Native

Kotlin/JVM

Kotlin/Native

Kotlin/JVM

Kotlin/JS

Page 26: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Using the same languageacross all platforms

All team members speak the same language

Page 27: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Using the same languageacross all platforms

All team members speak the same language

Simplified full-stack development

Page 28: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Using the same languageacross all platforms

All team members speak the same language

Simplified full-stack development

Single team works on apps for all platforms

Page 29: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Code sharing

Page 30: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Code sharing

Share data structures

Page 31: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Code sharing

Share data structures

Share business logic

Page 32: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Code sharing

Share data structures

Share business logic

Share tests!

Page 33: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Multiplatformprojectsin Kotlin

Page 34: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Still experimental

Page 35: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 36: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Common code

fun validate(day: Int, month: Int, year: Int): Boolean {

}

Page 37: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Common code

fun validate(day: Int, month: Int, year: Int): Boolean {

return month in 1..12

}

Page 38: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Common code

fun validate(day: Int, month: Int, year: Int): Boolean {

return Date(year, month, day).isValid()

}

Page 39: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Common code

fun validate(day: Int, month: Int, year: Int): Boolean {

return Date(year, month, day).isValid()

}

class Date(day: Int, month: Int, year: Int) {

fun isValid(): Boolean = ...

}

Page 40: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Common code

class Date(day: Int, month: Int, year: Int) {

fun isValid(): Boolean = ...

}

Page 41: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Interfaces

interface Date {

fun isValid(): Boolean

}

Page 42: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Interfaces

interface Date {

fun isValid(): Boolean

}

class DateJvm : Date { override fun isValid() = true }

class DateJs : Date { override fun isValid() = true }

class DateNative : Date { override fun isValid() = true }

Page 43: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Interfaces

interface Date {

fun isValid(): Boolean

}

object DateFactory {

fun createDate(day: Int, month: Int, year: Int) = ...

}

Page 44: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect

expect class Date(day: Int, month: Int, year: Int) {

fun isValid(): Boolean

}

Page 45: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect limitations

expect class Date(val day: Int, month: Int, year: Int) {

fun isValid(): Boolean

}

Page 46: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect limitations

expect class Date(val day: Int, month: Int, year: Int) {

fun isValid(): Boolean

}

Page 47: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect limitations

expect class Date(day: Int, month: Int, year: Int) {

val day: Int

val month: Int

val year: Int

fun isValid(): Boolean

}

Page 48: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect limitations

expect class Date(day: Int, month: Int, year: Int) {

private val day: Int

private val month: Int

private val year: Int

fun isValid(): Boolean

}

Page 49: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect limitations

expect class Date(day: Int, month: Int, year: Int) {

fun isValid(): Boolean {

return true

}

}

Page 50: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect limitations

expect class Date(day: Int, month: Int, year: Int) {

fun isValid(): Boolean {

return true

}

}

Page 51: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

expect limitations

expect class Date(day: Int, month: Int, year: Int) {

}

fun Date.isValid(): Boolean {

return true

}

Page 52: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

actual

actual class Date

actual constructor(private val day: Int, private val month: Int, private val year: Int) {

actual fun isValid() = ...

}

Page 53: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

actual

actual class Date

actual constructor(private val day: Int, private val month: Int, private val year: Int) {

actual fun isValid() = true

}

Page 54: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

All targets

actual class Date // JVM

actual class Date // JS

actual class Date // Native

Page 55: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

No overhead

Page 56: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Existing actual class

expect class LocalDate {

fun lengthOfYear(): Int

}

Page 57: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

typealias

expect class LocalDate {

fun lengthOfYear(): Int

}

// JVM

actual typealias LocalDate = java.time.LocalDate

Page 58: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Test typealias

expect annotation class Test

actual typealias Test = org.junit.Test

Page 59: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Code structure

Page 60: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

Page 61: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

kotlin {

targets {

}

}

Page 62: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

kotlin {

targets {

fromPreset(presets.jvm, 'jvm')

}

}

Page 63: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

kotlin {

targets {

fromPreset(presets.jvm, 'jvm')

fromPreset(presets.js, 'js')

}

}

Page 64: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

kotlin {

targets {

fromPreset(presets.jvm, 'jvm')

fromPreset(presets.js, 'js')

fromPreset(presets.linuxX64, 'linux')

}

}

Page 65: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

kotlin {

targets {

fromPreset(presets.jvm)

fromPreset(presets.js)

fromPreset(presets.linuxX64, 'linux')

}

}

Page 66: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

kotlin {

targets {...}

sourceSets {

}

}

Page 67: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

apply plugin: 'kotlin-multiplatform'

kotlin {

targets {...}

sourceSets {

commonMain { }

commonTest { }

}

}

Page 68: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle sourceSets {

commonMain { }

commonTest { }

jvmMain { }

jvmTest { }

jsMain { }

jsTest { }

linuxMain { }

linuxTest { }

}

Page 69: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Project structure

Page 70: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Testing

Page 71: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 72: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

$ ./gradlew :shared:linuxTest> Configure project :frontend> Configure project :shared> Task :shared:linuxTest[==========] Running 1 tests from 1 test cases.[----------] Global test environment set-up.[----------] 1 tests from DateTest[ RUN ] DateTest.validateBirthday[ OK ] DateTest.validateBirthday (0 ms)[----------] 1 tests from DateTest (0 ms total)

[----------] Global test environment tear-down[==========] 1 tests from 1 test cases ran. (0 ms total)[ PASSED ] 1 tests.

BUILD SUCCESSFUL in 14s3 actionable tasks: 3 executed

Page 73: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

commonMain {

dependencies {

implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'

}

}

commonTest {

dependencies {

implementation 'org.jetbrains.kotlin:kotlin-test-common'

implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'

}

}

Page 74: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

jvmMain {

dependencies {

implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

}

}

jvmTest {

dependencies {

implementation 'org.jetbrains.kotlin:kotlin-test'

implementation 'org.jetbrains.kotlin:kotlin-test-junit'

}

}

Page 75: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

build.gradle

jsMain {

dependencies {

implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'

}

}

jsTest {

dependencies {

implementation 'org.jetbrains.kotlin:kotlin-test'

implementation 'org.jetbrains.kotlin:kotlin-test-js'

}

}

Page 76: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Project dependencies

dependencies {

implementation project(':shared')

}

Page 77: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Multiplatform libraries

Page 78: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

kotlin.test

Page 79: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

kotlin.test

kotlinx.coroutines

Page 80: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

kotlin.test

kotlinx.coroutines

kotlinx.serialization

Page 81: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Write your own library!

Page 82: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Publishing

build.gradle apply plugin: 'maven-publish'

Console $ ./gradlew publishToMavenLocal

Page 83: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Use it

Page 84: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Date/JVMactual class Date {

actual fun isValid(): Boolean {

}

}

Page 85: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Date/JVMactual class Date actual constructor(day: Int, month: Int,year:Int){

actual fun isValid(): Boolean {

}

}

Page 86: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Date/JVMactual class Date actual constructor(private val day: Int, private val month: Int, private val year: Int) {

actual fun isValid(): Boolean {

}

}

Page 87: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Date/JVMactual class Date actual constructor(private val day: Int, private val month: Int, private val year: Int) {

actual fun isValid() = try {

val calendar = Calendar.getInstance()

calendar.isLenient = false

calendar.set(year, month - 1, day)

calendar.time

true

} catch (e: Exception) {

false

}

}

Page 88: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Date/JSactual class Date actual constructor(private val day: Int, private val month: Int, private val year: Int) {

actual fun isValid() = moment(year, month, day).isValid()

}

Page 89: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Date/JSactual class Date actual constructor(private val day: Int, private val month: Int, private val year: Int) {

actual fun isValid() = moment(year, month, day).isValid()

}

external interface Moment {

fun isValid(): Boolean

}

fun moment(day: Int, month: Int, year: Int): Moment = definedExternally

Page 90: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

There is a bug still…

Page 91: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Bug

fun validate(day: Int, month: Int, year: Int): Boolean {

return Date(year, month, day).isValid()

}

expect class Date(year: Int, month: Int, day: Int) {

fun isValid(): Boolean

}

Page 92: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Fixing a bug

fun validate(day: Int, month: Int, year: Int): Boolean {

return Date(day, month, year).isValid()

}

expect class Date(year: Int, month: Int, day: Int) {

fun isValid(): Boolean

}

Page 93: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Fixing a bug

fun validate(day: Int, month: Int, year: Int): Boolean {

return Date(day, month, year).isValid()

}

expect class Date(day: Int, month: Int, year: Int) {

fun isValid(): Boolean

}

Page 94: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

inline classes

inline class Day(val value: Int)

inline class Month(val value: Int)

inline class Year(val value: Int)

Page 95: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

inline classes

fun validate(day: Day, month: Month, year: Year): Boolean {

return Date(day, month, year).isValid()

}

expect class Date(day: Day, month: Month, year: Year) {

fun isValid(): Boolean

}

inline class Day(val value: Int)

inline class Month(val value: Int)

inline class Year(val value: Int)

Page 96: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Compile-time type checks

@Test fun validateBirthday() {

assertFalse(validate(Day(3), Month(31), Year(1986)))

assertTrue(validate(Month(31), Day(3), Year(1986)))

} Type mismatchRequired: DayFound: Month

Page 97: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Recap

Write once, reuse everywhere

Page 98: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Recap

Write once, reuse everywhere

Integrate with platform-specific libraries

Page 99: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Recap

Write once, reuse everywhere

Integrate with platform-specific libraries

Use all language features on all platforms

Page 100: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 101: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak
Page 102: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

kotl.in/multiplatform

Page 103: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Sample code

github.com/kropp/kotlin-multiplatform-sample

Page 104: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Links

Multiplatform projects documentation kotl.in/multiplatform Configuration samples github.com/h0tk3y/k-new-mpp-samples

Code from this presentationgithub.com/kropp/kotlin-multiplatform-sample

Page 105: victor.kropp.name Multiplatform projects with @kropp ... Toulouse 2018... · Kotlin/Native Kotlin/JVM Kotlin/JS. Using the same language across all platforms All team members speak

Thank you!

Victor [email protected]