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

Développement Android et iOS avec Kotlin Multiplatform

Développement Android et iOS avec Kotlin Multiplatform

Google Technologies Week Guinea 2020

Chuck Greb

May 22, 2020
Tweet

More Decks by Chuck Greb

Other Decks in Technology

Transcript

  1. 22 Mai 2020 | Google Technologies Week Guinea 2020
    Développement Android et iOS
    avec Kotlin Multiplatform
    Charles “Chuck” Greb
    Développeur Mobile
    Co-Lead GDG Ratoma
    @ecgreb

    View full-size slide

  2. Kotlin / Java

    View full-size slide

  3. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java

    View full-size slide

  4. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java

    View full-size slide

  5. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java

    View full-size slide

  6. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java
    Swift / Obj-C

    View full-size slide

  7. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java
    Swift / Obj-C JS

    View full-size slide

  8. Presentation
    Model
    Storage
    Network
    View
    Presentation
    Presentation
    Model
    Model
    Storage
    Storage Network
    Network
    View
    View
    Kotlin / Java
    Swift / Obj-C JS

    View full-size slide

  9. Presentation
    Model
    Storage
    Network
    View View
    View
    Kotlin / Java
    Swift / Obj-C JS

    View full-size slide

  10. Multiplatform
    Presentation
    Model
    Storage
    Network
    View View
    View
    Kotlin / Java
    Swift / Obj-C JS

    View full-size slide

  11. Multiplatform
    Presentation
    Model
    Storage
    Network
    Activity HTML Document
    UIViewController
    Kotlin / Java
    Swift / Obj-C JS

    View full-size slide

  12. JVM
    Native JS
    Common

    View full-size slide

  13. JVM
    Native JS
    Common
    Android
    Java 6
    Java 8

    View full-size slide

  14. JVM
    Native JS
    Common
    Android
    Java 6
    Java 8
    Browser
    Node

    View full-size slide

  15. JVM
    Native JS
    Common
    Android
    Java 6
    Java 8
    Browser
    Node
    iOS
    Mac
    Windows
    Linux
    WebAssembly

    View full-size slide

  16. JVM
    Native JS
    Common
    Android
    Java 6
    Java 8
    Browser
    Node
    iOS
    Mac
    Windows
    Linux
    WebAssembly

    View full-size slide

  17. Avantages et inconvénients
    Pros and cons
    Cross-Platform Frameworks Kotlin Multiplatform
    Pros
    Single language (Dart, JS, C#)

    Rapid development

    Easy to learn
    Native languages (Kotlin / Swift)

    Native UI

    Interoperability
    Cons
    Framework UI only

    Access to system / OS

    Performance
    Multiple languages

    Project setup

    Learning curve

    View full-size slide

  18. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin

    View full-size slide

  19. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin
    janvier

    2015
    Using project

    Kotlin for

    Android

    published

    View full-size slide

  20. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin
    mai

    2017
    Google

    announces

    official support

    for Kotlin
    janvier

    2015
    Using project

    Kotlin for

    Android

    published

    View full-size slide

  21. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin
    mai

    2017
    Google

    announces

    official support

    for Kotlin
    janvier

    2015
    Using project

    Kotlin for

    Android

    published
    octobre

    2018
    Kotlin 1.3

    release with

    experimental

    multiplatform

    suppot

    View full-size slide

  22. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin
    mai

    2017
    Google

    announces

    official support

    for Kotlin
    janvier

    2015
    Using project

    Kotlin for

    Android

    published
    octobre

    2018
    Kotlin 1.3

    release with

    experimental

    multiplatform

    suppot
    mars

    2020
    Kotlin1.3.70

    release

    View full-size slide

  23. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin
    mai

    2017
    Google

    announces

    official support

    for Kotlin
    janvier

    2015
    Using project

    Kotlin for

    Android

    published
    octobre

    2018
    Kotlin 1.3

    release with

    experimental

    multiplatform

    suppot
    mars

    2020
    Kotlin1.3.70

    release
    printemps

    2020
    Kotlin1.4

    release

    View full-size slide

  24. Kotlin Multiplatform
    tl;dr
    • Build configuration

    • Code sharing

    • expect and actual

    • Using typealias
    • Interfaces

    • Multiplatform libraries

    • Multiplatform IDEs

    View full-size slide

  25. Build configuration

    View full-size slide

  26. • Apply plugins

    • Build targets and source sets

    • Android dependencies

    • iOS dependencies
    Build configuration
    plugins {
    kotlin("multiplatform")
    id("org.jetbrains.kotlin.native.cocoapods")

    id(“com.android.library")
    }
    shared/build.gradle.kts

    View full-size slide

  27. • Apply plugins

    • Build targets and source sets

    • Android dependencies

    • iOS dependencies
    Build configuration
    kotlin {
    android()
    iosX64("ios")
    sourceSets["commonMain"].dependencies {
    // ...
    }
    sourceSets["androidMain"].dependencies {
    // ...
    }
    sourceSets["iosMain"].dependencies {
    // ...
    }
    cocoapodsext {
    framework {
    isStatic = false
    }
    }
    }
    shared/build.gradle.kts

    View full-size slide

  28. • Apply plugins

    • Build targets and source sets

    • Android dependencies

    • iOS dependencies
    Build configuration
    dependencies {
    implementation(kotlin("stdlib-jdk7", "1.3.72"))
    implementation(project(":shared"))
    ...
    }
    app/build.gradle.kts

    View full-size slide

  29. • Apply plugins

    • Build targets and source sets

    • Android dependencies

    • iOS dependencies
    Build configuration
    use_frameworks!
    platform :ios, '9.0'
    install! 'cocoapods', :deterministic_uuids => false
    target ‘my-ios-app‘ do
    pod 'shared', :path => '../shared/'
    end
    ios/Podfile

    View full-size slide

  30. Code sharing

    View full-size slide

  31. • expect and actual

    • Using typealias
    • Interfaces
    Code sharing
    expect fun currentTimeMillis(): Long
    src/commonMain/kotlin/Platform.kt
    actual fun currentTimeMillis(): Long =
    System.currentTimeMillis()
    src/androidMain/kotlin/PlatformAndroid.kt
    actual fun currentTimeMillis(): Long =
    (NSDate().timeIntervalSince1970 * 1000).toLong()
    src/iosMain/kotlin/PlatformiOS.kt

    View full-size slide

  32. • expect and actual

    • Using typealias
    • Interfaces
    Code sharing
    expect class Date(year: Int, month: Int, day: Int)
    src/commonMain/kotlin/Date.kt
    actual typealias Date = java.util.GregorianCalendar
    src/androidMain/kotlin/Date.kt
    src/iosMain/kotlin/Date.kt
    actual class Date {
    actual constructor(year: Int, month: Int, day: Int) {
    val calendar = NSCalendar.currentCalendar
    val components = NSDateComponents()
    components.setDay(day.toLong())
    components.setMonth(month.toLong())
    components.setYear(year.toLong())
    return calendar.dateFromComponents(comps)!!
    }
    }

    View full-size slide

  33. • expect and actual

    • Using typealias
    • Interfaces
    Code sharing
    interface ApplicationStorage {
    fun putBoolean(key: String, value: Boolean)
    }
    src/commonMain/kotlin/ApplicationStorage.kt
    internal class AndroidStorage(context: Context) : ApplicationStorage {
    private val prefs = PreferenceManager
    .getDefaultSharedPreferences(context)
    override fun putBoolean(key: String, value: Boolean) {
    prefs.edit().putBoolean(key, value).apply()
    }
    }
    src/androidMain/kotlin/AndroidStorage.kt
    internal class IosStorage : ApplicationStorage {
    private val delegate: NSUserDefaults =
    NSUserDefaults.standardUserDefaults()
    override fun putBoolean(key: String, value: Boolean) {
    delegate.setBool(value, key)
    }
    }
    src/iosMain/kotlin/iOSStorage.kt

    View full-size slide

  34. Multiplatform libraries
    kotlinx.coroutines
    Asynchronous or non-blocking
    programming
    https://github.com/Kotlin/kotlinx.coroutines
    Koin Multiplatform dependency injection https://insert-koin.io/
    Ktor HTTP utility for connected apps https://ktor.io
    SQLDelight Typesafe DB management https://cashapp.github.io/sqldelight/
    Multiplatform Settings Simple key-value data storage https://github.com/russhwolf/multiplatform-settings
    Stately Concurrency and state management https://github.com/touchlab/Stately
    Kermit Composable logging utility https://github.com/touchlab/Kermit
    Oolong Model-View-Update (MVU) architecture https://oolong-kt.org/
    New in
    Koin 3.0 !

    View full-size slide

  35. Multiplatform IDEs

    View full-size slide

  36. Multiplatform IDEs

    View full-size slide

  37. KaMP Kit
    https://github.com/touchlab/KaMPKit/

    View full-size slide

  38. 22 Mai 2020 | Google Technologies Week Guinea 2020
    fin.
    Charles “Chuck” Greb
    Développeur Mobile
    Co-Lead GDG Ratoma
    @ecgreb

    View full-size slide