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

Développement Android et iOS avec Kotlin Multip...

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
  2. Presentation Model Storage Network View Presentation Presentation Model Model Storage

    Storage Network Network View View Kotlin / Java Swift / Obj-C JS
  3. JVM Native JS Common Android Java 6 Java 8 Browser

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

    Node iOS Mac Windows Linux WebAssembly …
  5. 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
  6. juillet
 2011 Jetbrains
 announces
 project
 Kotlin mai
 2017 Google
 announces


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

    expect and actual • Using typealias • Interfaces • Multiplatform libraries • Multiplatform IDEs
  11. • 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
  12. • 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
  13. • 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
  14. • 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
  15. • 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
  16. • 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)!! } }
  17. • 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
  18. 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 !
  19. 22 Mai 2020 | Google Technologies Week Guinea 2020 fin.

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