Share
Embed
Développement Android et iOS avec Kotlin Multiplatform
Google Technologies Week Guinea 2020
Chuck Greb
May 22, 2020
Transcript
47 slides
-
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 -
Presentation Model Storage Network View Presentation Presentation Model Model Storage
Storage Network Network View View Kotlin / Java Swift / Obj-C JS -
Presentation Model Storage Network View View View Kotlin / Java
Swift / Obj-C JS -
Multiplatform Presentation Model Storage Network View View View Kotlin /
Java Swift / Obj-C JS -
Multiplatform Presentation Model Storage Network Activity HTML Document UIViewController Kotlin
/ Java Swift / Obj-C JS -
JVM Native JS Common Android Java 6 Java 8 Browser
Node iOS Mac Windows Linux WebAssembly … -
JVM Native JS Common Android Java 6 Java 8 Browser
Node iOS Mac Windows Linux WebAssembly … -
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 -
juillet 2011 Jetbrains announces project Kotlin janvier 2015 Using project
Kotlin for Android published -
juillet 2011 Jetbrains announces project Kotlin mai 2017 Google announces
official support for Kotlin janvier 2015 Using project Kotlin for Android published -
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 -
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 -
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 -
Kotlin Multiplatform tl;dr • Build configuration • Code sharing •
expect and actual • Using typealias • Interfaces • Multiplatform libraries • Multiplatform IDEs -
• 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 -
• 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 -
• 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 -
• 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 -
• 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 -
• 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)!! } } -
• 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 -
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 ! -
22 Mai 2020 | Google Technologies Week Guinea 2020 fin.
Charles “Chuck” Greb Développeur Mobile Co-Lead GDG Ratoma @ecgreb