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 Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. Kotlin / Java

    View Slide

  7. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java

    View Slide

  8. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java

    View Slide

  9. Presentation
    Model
    Storage
    Network
    View
    Kotlin / Java

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  16. Common

    View Slide

  17. JVM
    Native JS
    Common

    View Slide

  18. JVM
    Native JS
    Common
    Android
    Java 6
    Java 8

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. Pourquoi?

    View Slide

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

  24. View Slide

  25. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin

    View Slide

  26. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin
    janvier

    2015
    Using project

    Kotlin for

    Android

    published

    View Slide

  27. juillet

    2011
    Jetbrains

    announces

    project

    Kotlin
    mai

    2017
    Google

    announces

    official support

    for Kotlin
    janvier

    2015
    Using project

    Kotlin for

    Android

    published

    View Slide

  28. 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 Slide

  29. 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 Slide

  30. 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 Slide

  31. View Slide

  32. Kotlin Multiplatform
    tl;dr
    • Build configuration

    • Code sharing

    • expect and actual

    • Using typealias
    • Interfaces

    • Multiplatform libraries

    • Multiplatform IDEs

    View Slide

  33. Build configuration

    View Slide

  34. • 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 Slide

  35. • 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 Slide

  36. • 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 Slide

  37. • 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 Slide

  38. Code sharing

    View Slide

  39. • 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 Slide

  40. • 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 Slide

  41. • 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 Slide

  42. 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 Slide

  43. Multiplatform IDEs

    View Slide

  44. Multiplatform IDEs

    View Slide

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

    View Slide

  46. La Demo

    View Slide

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

    View Slide