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

An Overview of Multiplatform Kotlin

An Overview of Multiplatform Kotlin

Presented at TheConf 2018

2dc2a52d38ee8c184deeceaadf9ed6ba?s=128

Rafael Toledo

September 21, 2018
Tweet

More Decks by Rafael Toledo

Other Decks in Programming

Transcript

  1. KOTLIN An Overview of Multiplatform Kotlin

  2. /_rafaeltoledo /rafaeltoledo

  3. 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
  4. Lines of Code written in Kotlin on Github

  5. None
  6. ADOPT

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

  8. Cool features 8 DATA CLASSES TYPE ALIAS LAMBDA DSL NULL

    SAFETY PROPERTIES EXTENSION FUNCTIONS
  9. Platforms 9 JVM JS NATIVE ANDROID

  10. 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
  11. Why to use Kotlin for JVM development? • Expressivity •

    Scalability & Performance - Kotlin Coroutines • Interoperability with existing codebase • Gradual migration • Tooling • Learning curve 11
  12. https://bit.ly/2jV4utA

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

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

  15. 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
  16. 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
  17. https://github.com/JetBrains/kotlin-wrappers

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

    } 18
  19. KotlinJS - React class HelloComponent: RComponent<RProps, RState>() { override fun

    RBuilder.render() { div(classes = "content") { h1 { +"Hello!" } } } } fun RBuilder.hello() = child(HelloComponent::class) { } 19
  20. KotlinJS - NodeJS fun main(args: Array<String>) { 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. Kotlin Native - C interop fun fetch() { val res

    = curl_easy_perform(curl) if (res != CURLE_OK) println("curl_easy_perform() failed") } fun CPointer<ByteVar>.toKString(length: Int): String { val bytes = this.readBytes(length) return bytes.stringFromUtf8() } 25
  26. IDE Integration 26

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

  28. MULTIPLATFORM MODULES 28

  29. Multiplatform Kotlin • Support introduced in 1.2 • JVM &

    JavaScript (Native is "kind of" working) • common, platform and regular modules 29
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. Multiplatform Module - Common code package com.example.foo expect class Foo(bar:

    String) { fun frob() } fun main(args: Array<String>) { Foo("Hello").frob() } 39
  40. Multiplatform Module - specific implementation package com.example.foo actual class Foo

    actual constructor(val bar: String) { actual fun frob() { println("Frobbing the $bar") } } 40
  41. Multiplatform Dependency Injection 41

  42. Multiplatform Mock 42

  43. KOTLIN GRADLE DSL Bonus - why not to use Kotlin

    everywhere?
  44. 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
  45. 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
  46. 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
  47. what if... 47

  48. we had a fullstack project written in Kotlin? 48

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

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

    JavaFX
  52. 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
  53. THANK YOU! Questions and feedbacks? Rafael Toledo rdtoledo@thoughtworks.com speakerdeck.com/rafaeltoledo 53