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

Compartilhando Código com Kotlin Multiplataforma

Rafael Toledo
September 16, 2018

Compartilhando Código com Kotlin Multiplataforma

Apresentada no Kotlin Community Summit 2018

Rafael Toledo

September 16, 2018
Tweet

More Decks by Rafael Toledo

Other Decks in Programming

Transcript

  1. KOTLIN
    Compartilhando código com Kotlin Multiplataforma

    View full-size slide

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

    View full-size slide

  3. Plataformas
    4
    JVM JS NATIVE
    ANDROID

    View full-size slide

  4. Por que usar Kotlin no desenvolvimento Android?
    ● Compatibilidade - Java 6
    ● Performance
    ● Interoperabilidade
    ● Footprint: standard library & runtime ~100Kb
    ● Tempo de Compilação não é mais um problema
    ● Tooling evoluindo
    5

    View full-size slide

  5. https://bit.ly/2ATKly0

    View full-size slide

  6. Por que usar Kotlin no desenvolvimento para JVM?
    ● Expressividade
    ● Escalabilidade & Performance - Kotlin Coroutines
    ● Interoperabilidade com código já existente - migração gradual
    ● Tooling
    7

    View full-size slide

  7. https://bit.ly/2jV4utA

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. Por que usar Kotlin no desenvolvimento JavaScript?
    ● JavaScript Optimized - DCE (Dead Code Elimination)
    ● JavaScript legível e depurável
    ● Compatibilidade com código JavaScript pré-existente
    ● Mesmas features da Standard Library da JVM - incluindo Coroutines
    11

    View full-size slide

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

    View full-size slide

  12. Kotlin para JavaScript - e a tipagem?
    ● É possível interagir com qualquer tipo de código JavaScript
    ● Strong-typed APIs / TypeScript -> https://github.com/kotlin/ts2kt
    ● Dynamic types para outros cenários
    13

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. 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")
    })
    } 16

    View full-size slide

  16. Why to use Kotlin for native development?
    ● Código Nativo
    ● Backend baseado no LLVM
    ● Interoperabilidade com código nativo e bibliotecas - binding gerado a
    partir de arquivos .h
    ● MacOS / iOS - Objective C e Swift
    ● Atualmente na versão 0.9 - em desenvolvimento - 1.3
    17

    View full-size slide

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

    View full-size slide

  18. 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'
    }
    }
    } 19

    View full-size slide

  19. 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
    20

    View full-size slide

  20. 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()
    }
    21

    View full-size slide

  21. Integração com IDE
    22

    View full-size slide

  22. 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
    23

    View full-size slide

  23. MÓDULOS
    MULTIPLATAFORMA
    24

    View full-size slide

  24. Kotlin Multiplataforma
    ● Suporte introduzido na versão 1.2
    ● JVM & JavaScript (Native "meio que" funciona)
    ● módulos common, platform e regular
    25

    View full-size slide

  25. Módulos Multiplataforma
    common: Contém código que não é específico a nenhuma plataforma, assim
    como a declaração de APIs que necessitam de implementação específica
    platform: Contém a implementação das APIs definidas em um módulo
    common, além de outros códigos específicos da plataforma
    regular: Módulo comum que tem como target uma plataforma específica
    26

    View full-size slide

  26. 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"
    } 27

    View full-size slide

  27. 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"
    } 28

    View full-size slide

  28. 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"
    }
    29

    View full-size slide

  29. 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"
    }
    30

    View full-size slide

  30. 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"
    }
    31

    View full-size slide

  31. 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"
    }
    32

    View full-size slide

  32. 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.8.2'
    }
    }
    apply plugin: 'kotlin-platform-native'
    dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    expectedBy project(":common")
    }
    ... 33

    View full-size slide

  33. 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 ]
    }
    ...
    34

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  36. DI Multiplataforma
    37

    View full-size slide

  37. Mock Multiplataforma
    38

    View full-size slide

  38. KOTLIN GRADLE DSL
    Bonus - por que não usar em todo o projeto então?

    View full-size slide

  39. Por que usar a Kotlin Gradle DSL?
    ● Aumento na velocidade de análise e compilação
    dos build scripts, em comparação com Groovy
    ● IDE friendly, melhor autocomplete
    ● Unificação da linguagem de desenvolvimento
    ● 1.0 RC 6 incluído no Gradle 4.10.1, estável no
    Gradle 5.0 (próxima versão)
    40

    View full-size slide

  40. 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()
    }
    }
    41

    View full-size slide

  41. 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()
    }
    }
    42

    View full-size slide

  42. a gente tivesse um projeto definitivamente full stack em
    Kotlin?
    44

    View full-size slide

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

    View full-size slide

  44. 47
    https://github.com/MarcinMoskala/KotlinAcademyApp
    Ktor / ReactJS / Android / iOS / JavaFX

    View full-size slide

  45. Links
    BLOG OFICIAL
    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
    48

    View full-size slide

  46. OBRIGADO!
    Questions and feedbacks?
    Rafael Toledo
    [email protected]
    speakerdeck.com/rafaeltoledo
    github.com/rafaeltoledo
    49

    View full-size slide