Développement Android et iOS avec Kotlin Multiplatform

Développement Android et iOS avec Kotlin Multiplatform

Google Technologies Week Guinea 2020

613633962e5ab27cf572e7699e43d368?s=128

Chuck Greb

May 22, 2020
Tweet

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
  2. None
  3. None
  4. None
  5. None
  6. Kotlin / Java

  7. Presentation Model Storage Network View Kotlin / Java

  8. Presentation Model Storage Network View Kotlin / Java

  9. Presentation Model Storage Network View Kotlin / Java

  10. Presentation Model Storage Network View Kotlin / Java Swift /

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

    Obj-C JS
  12. Presentation Model Storage Network View Presentation Presentation Model Model Storage

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

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

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

    / Java Swift / Obj-C JS
  16. Common

  17. JVM Native JS Common

  18. JVM Native JS Common Android Java 6 Java 8

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

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

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

    Node iOS Mac Windows Linux WebAssembly …
  22. Pourquoi?

  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
  24. None
  25. juillet
 2011 Jetbrains
 announces
 project
 Kotlin

  26. juillet
 2011 Jetbrains
 announces
 project
 Kotlin janvier
 2015 Using project


    Kotlin for
 Android
 published
  27. juillet
 2011 Jetbrains
 announces
 project
 Kotlin mai
 2017 Google
 announces


    official support
 for Kotlin janvier
 2015 Using project
 Kotlin for
 Android
 published
  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
  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
  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
  31. None
  32. Kotlin Multiplatform tl;dr • Build configuration • Code sharing •

    expect and actual • Using typealias • Interfaces • Multiplatform libraries • Multiplatform IDEs
  33. Build configuration

  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
  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
  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
  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
  38. Code sharing

  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
  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)!! } }
  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
  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 !
  43. Multiplatform IDEs

  44. Multiplatform IDEs

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

  46. La Demo

  47. 22 Mai 2020 | Google Technologies Week Guinea 2020 fin.

    Charles “Chuck” Greb Développeur Mobile Co-Lead GDG Ratoma @ecgreb