Slide 1

Slide 1 text

KOTLIN An Overview of Multiplatform Kotlin

Slide 2

Slide 2 text

/_rafaeltoledo /rafaeltoledo

Slide 3

Slide 3 text

Kotlin? ● Open Source language, created back in 2010 by JetBrains ● Statically typed, safe, and pragmatic ● It's design is inspired by languages like Java, Scala, C#, and Groovy ● Object Oriented with some Functional features 3

Slide 4

Slide 4 text

Lines of Code written in Kotlin on Github

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

ADOPT

Slide 7

Slide 7 text

https://pusher.com/state-of-kotlin

Slide 8

Slide 8 text

Cool features 8 DATA CLASSES TYPE ALIAS LAMBDA DSL NULL SAFETY PROPERTIES EXTENSION FUNCTIONS

Slide 9

Slide 9 text

Platforms 9 JVM JS NATIVE ANDROID

Slide 10

Slide 10 text

Why to use Kotlin for Android development? ● Compatibility - Java 6 ● Performance ● Interoperability ● Footprint: standard library & runtime costs only ~100Kb ● Compilation Time is not a problem anymore ● Learning curve 10

Slide 11

Slide 11 text

Why to use Kotlin for JVM development? ● Expressivity ● Scalability & Performance - Kotlin Coroutines ● Interoperability with existing codebase ● Gradual migration ● Tooling ● Learning curve 11

Slide 12

Slide 12 text

https://bit.ly/2jV4utA

Slide 13

Slide 13 text

JavaFX Framework for Kotlin https://github.com/edvin/tornadofx

Slide 14

Slide 14 text

Ktor for building servers and clients https://ktor.io

Slide 15

Slide 15 text

Why to use Kotlin for JavaScript development ● Supports ECMAScript 5.1 on Kotlin 1.2 - ECMAScript 2015 support soon ● Optimized JavaScript - DCE (Dead Code Elimination) ● Legible and debuggable JavaScript ● Compatibility with existing JavaScript code ● Same features as the JVM Standard Library - including Coroutines 15

Slide 16

Slide 16 text

Kotlin for JavaScript - what about types? ● It's possible to interact with any JavaScript code ● Statically-typed APIs / TypeScript -> https://github.com/kotlin/ts2kt ● Dynamic types for any other scenarios 16

Slide 17

Slide 17 text

https://github.com/JetBrains/kotlin-wrappers

Slide 18

Slide 18 text

KotlinJS - jQuery fun main(args: Array) { jq("#message").html("Hello from Kotlin") } 18

Slide 19

Slide 19 text

KotlinJS - React class HelloComponent: RComponent() { override fun RBuilder.render() { div(classes = "content") { h1 { +"Hello!" } } } } fun RBuilder.hello() = child(HelloComponent::class) { } 19

Slide 20

Slide 20 text

KotlinJS - NodeJS fun main(args: Array) { val express = require("express") val app = express() app.get("/", { req, res -> res.type("text/plain") res.send("i am a beautiful butterfly") }) app.listen(3000, { println("Listening on port 3000") }) } 20

Slide 21

Slide 21 text

Why to use Kotlin for native development? ● Native code, without VM ● LLVM based backend ● Interoperability with any native code and libraries - you can generate the binding interface using .h files ● MacOS / iOS ● Latest version is 0.8.2 - in active development 21

Slide 22

Slide 22 text

Kotlin Native supported targets ● Windows (x86_64) ● Linux (x86_64, arm32, MIPS, and MIPS little endian) ● MacOS (x86_64) ● iOS (arm32, arm64, x64) ● Android (arm32 e arm64) ● STM32 ● WebAssembly (wasm32) 22

Slide 23

Slide 23 text

Kotlin Native - C interop components.main { targets = ['macos_x64', 'linux_x64'] dependencies { cinterop('libcurl-interop') { defFile 'src/main/c_interop/libcurl.def' target('linux_x64') { includeDirs.headerFilterOnly '/usr/include' } target('macos_x64') { includeDirs.headerFilterOnly '/opt/local/include', '/usr/local/include' } } } 23

Slide 24

Slide 24 text

Kotlin Native - C interop headers = curl/curl.h headerFilter = curl/* linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/curl/lib -lcurl linkerOpts.linux = -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lcurl 24

Slide 25

Slide 25 text

Kotlin Native - C interop fun fetch() { val res = curl_easy_perform(curl) if (res != CURLE_OK) println("curl_easy_perform() failed") } fun CPointer.toKString(length: Int): String { val bytes = this.readBytes(length) return bytes.stringFromUtf8() } 25

Slide 26

Slide 26 text

IDE Integration 26

Slide 27

Slide 27 text

Reference links https://kotlinlang.org/docs/reference/android-overview.html https://kotlinlang.org/docs/reference/server-overview.html https://kotlinlang.org/docs/reference/js-overview.html https://kotlinlang.org/docs/reference/native-overview.html 27

Slide 28

Slide 28 text

MULTIPLATFORM MODULES 28

Slide 29

Slide 29 text

Multiplatform Kotlin ● Support introduced in 1.2 ● JVM & JavaScript (Native is "kind of" working) ● common, platform and regular modules 29

Slide 30

Slide 30 text

Multiplatform Modules common: Contains common code that can runs on any platform. Also can hold common interfaces and class signatures that must have a specific implementation in each platform platform: Contains the specific implementation of some interface defined by a common module regular: A regular module that contains only code to a specific platform 30

Slide 31

Slide 31 text

Multiplatform Module - Gradle buildscript { ext.kotlin_version = '1.2.70' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'kotlin-platform-common' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version" testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version" } 31

Slide 32

Slide 32 text

Multiplatform Module - Gradle buildscript { ext.kotlin_version = '1.2.70' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'kotlin-platform-common' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version" testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version" } 32

Slide 33

Slide 33 text

Multiplatform Module - JVM target ... apply plugin: 'kotlin-platform-jvm' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" expectedBy project(":common") testCompile "junit:junit:4.12" testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version" } 33

Slide 34

Slide 34 text

Multiplatform Module - JVM target ... apply plugin: 'kotlin-platform-jvm' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" expectedBy project(":common") testCompile "junit:junit:4.12" testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version" } 34

Slide 35

Slide 35 text

Multiplatform Module - JavaScript target ... apply plugin: 'kotlin-platform-js' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version" expectedBy project(":common") testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version" } 35

Slide 36

Slide 36 text

Multiplatform Module - JavaScript target ... apply plugin: 'kotlin-platform-js' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version" expectedBy project(":common") testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version" } 36

Slide 37

Slide 37 text

Multiplatform Module - Native target ... buildscript { repositories { maven { url 'https://jetbrains.bintray.com/kotlin-native-dependencies' } } dependencies { classpath 'org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.9.1' } } apply plugin: 'kotlin-platform-native' dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" expectedBy project(":common") } ... 37

Slide 38

Slide 38 text

Multiplatform Module - Native target ... apply plugin: 'kotlin-platform-native' dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" expectedBy project(":common") } sourcesets.main.component { baseName.set('MyCommonModule') target 'ios_arm64', 'ios_x64' outputKinds = [ FRAMEWORK ] } ... 38

Slide 39

Slide 39 text

Multiplatform Module - Common code package com.example.foo expect class Foo(bar: String) { fun frob() } fun main(args: Array) { Foo("Hello").frob() } 39

Slide 40

Slide 40 text

Multiplatform Module - specific implementation package com.example.foo actual class Foo actual constructor(val bar: String) { actual fun frob() { println("Frobbing the $bar") } } 40

Slide 41

Slide 41 text

Multiplatform Dependency Injection 41

Slide 42

Slide 42 text

Multiplatform Mock 42

Slide 43

Slide 43 text

KOTLIN GRADLE DSL Bonus - why not to use Kotlin everywhere?

Slide 44

Slide 44 text

Why to use Kotlin DSL on Gradle? ● Speed up analysis time and compilation time of build scripts, compared to Groovy ● IDE friendly, better autocomplete ● Unify the language used in the project ● 1.0 RC6 included in Gradle 4.10.1, final to be included in Gradle 5.0 (next version) 44

Slide 45

Slide 45 text

Gradle - Groovy (default) // build.gradle allprojects { buildscript { ext.kotlin_version = '1.2.70' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } repositories { mavenLocal() } } 45

Slide 46

Slide 46 text

Gradle - Kotlin DSL // build.gradle.kts allprojects { buildscript { val kotlin_version by extra { "1.2.70" } repositories { mavenCentral() } dependencies { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") } } repositories { mavenLocal() } } 46

Slide 47

Slide 47 text

what if... 47

Slide 48

Slide 48 text

we had a fullstack project written in Kotlin? 48

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

50 https://github.com/JetBrains/kotlinconf-app Ktor / ReactJS / Android / iOS

Slide 51

Slide 51 text

51 https://github.com/MarcinMoskala/KotAcademyPortal Ktor / ReactJS / Android / iOS / JavaFX

Slide 52

Slide 52 text

Links OFFICIAL BLOG https://blog.jetbrains.com/kotlin KOTLIN KOANS https://kotlinlang.org/docs/tutorials/koans.html KOTLIN IN ACTION https://www.manning.com/books/kotlin-in-action 52

Slide 53

Slide 53 text

THANK YOU! Questions and feedbacks? Rafael Toledo rdtoledo@thoughtworks.com speakerdeck.com/rafaeltoledo 53