$30 off During Our Annual Pro Sale. View Details »

Yet Another KMP*

Ilker Aslan
November 15, 2022

Yet Another KMP*

Ilker Aslan

November 15, 2022
Tweet

Other Decks in Programming

Transcript

  1. YET ANOTHER KMP* *Kotlin Multiplatform Presentation

  2. Kotlin Multiplatform Multiple targets using Kotlin compiler

  3. Cross Compilation Compile on Host -> for Target Native Compilation

    Compile on Host -> for Host
  4. Plugin Targets Source Sets Compilations

  5. 1. Plugin plugins { kotlin("multiplatform") version "1.7.20" }

  6. 2. Targets

  7. Targets android() jvm() watchosArm64 { binaries { /* Export an

    unoptimized version */ this.DEBUG = true } } iosArm64 { binaries { /* Export an optimized version */ this.DEBUG = false } }
  8. 3. Source Sets

  9. Predefined Source Sets ------ Custom Source Sets

  10. Predefined Source Sets kotlin { sourceSets { val commonMain by

    getting val commonTest by getting /* if we target jvm platform */ val jvmMain by getting val jvmTest by getting } }
  11. Custom Source Sets kotlin { sourceSets { val commonMain by

    sourceSets.getting val iosX64Main by sourceSets.getting val iosArm64Main by sourceSets.getting val iosMain by sourceSets.creating { dependsOn(commonMain) iosX64Main.dependsOn(this) iosArm64Main.dependsOn(this) } } }
  12. 4. Compilations Default Compilations Custom Compilations

  13. https://kotlinlang.org/docs/multiplatform- configure-compilations.html

  14. Default Compilations (JVM, JS, Native) main test

  15. Default Compilations (Android) kotlin { android { publishLibraryVariants( "release", "debug"

    ) } }
  16. Custom Compilations

  17. kotlin { iosArm64 { /* Apple iOS on ARM64 platforms*/

    } iosX64 { /* Apple iOS simulator on x86_64 platforms */ compilations { val old by compilations.creating { defaultSourceSet { languageSettings { languageVersion = "1.5" } } } tasks.register<Exec>("old") { println("Compiling for an old version") } } }
  18. How do I build native code?

  19. ./gradlew macosX64Binaries

  20. None
  21. None
  22. ./gradlew linuxArm64Binaries

  23. None
  24. ./gradlew nativeBinaries

  25. None
  26. None
  27. build/bin/native/releaseExecutable/yet_another_kmp.kexe

  28. None
  29. ./gradlew nativeBinaries

  30. None
  31. build/bin/native/releaseExecutable/yet_another_kmp.kexe

  32. None
  33. Compile on linux and execute it on macOS?

  34. None
  35. expect/actual

  36. expect fun debugLog(message: String) // Android implementation actual fun debugLog(message:

    String) { Timber.d(message) } // JS implementation actual fun debugLog(message: String) { console.log(message) }
  37. Useful KMP Compatible Libraries

  38. Database SQLDelight https://cashapp.github.io/sqldelight/multiplatform_sqlite/

  39. CREATE TABLE userTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, authenticated

    INTEGER AS Boolean );
  40. insertUser: INSERT INTO userTable( authenticated ) VALUES (?); deleteUser: DELETE

    FROM userTable WHERE id = : id;
  41. Network Client Ktor https://ktor.io/docs/getting-started-ktor- client-multiplatform-mobile.html

  42. class SharedNetworkClient( private val api: String ) { private val

    client = HttpClient() { defaultRequest { url("api") } } suspend fun getUser(): String { val response = client.get() return response.bodyAsText() } }
  43. Asynchronous Programming kotlinx.coroutines https://github.com/Kotlin/kotlinx.coroutines#multiplatform

  44. Data Processing Okio https://square.github.io/okio/multiplatform/

  45. Date Time kotlinx-datetime https://github.com/Kotlin/kotlinx- datetime#using-in-your-projects

  46. Serialization kotlinx.serialization https://github.com/Kotlin/kotlinx.serialization#multiplatform- common-js-native

  47. Dependency Injection koin https://github.com/InsertKoinIO/koin Kodein https://github.com/kosi-libs/Kodein

  48. MVVM Architecture modo-mvvm https://github.com/icerockdev/moko- mvvm

  49. MVI Architecture MVIKotlin https://github.com/arkivanov/MVIKotlin

  50. State Machine FlowRedux https://freeletics.github.io/FlowRedux/

  51. Logging Kermit https://github.com/touchlab/Kermit

  52. Conclusions

  53. Shared codebase knowledge

  54. Shared bugs features

  55. Shared unit tests

  56. Shared developer effort* *In a parallel universe

  57. Shared layers

  58. Shared language

  59. Shared transition

  60. Shared solutions to shared problems

  61. Where To Start https://kotlinlang.org/docs/multiplatform- dsl-reference.html

  62. Where To Start https://github.com/icerockdev https://github.com/touchlab https://github.com/joreilly

  63. Where To Start https://github.com/Kotlin/kmm-production-sample https://github.com/VictorKabata/Notflix https://github.com/oianmol/SlackComposeMultiplatform

  64. Where To Stalk Me https://androiddev.social/@ilker https://github.com/ilkeraslan https://www.polywork.com/ilkeraslan https://www.linkedin.com/in/aslanilker/ https://twitter.com/ilkeraslan0

  65. None