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

One Code to Rule Them All : A Guide to Kotlin a...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Victor Kabata Victor Kabata
October 23, 2024
29

One Code to Rule Them All : A Guide to Kotlin and Compose Multiplatform

Avatar for Victor Kabata

Victor Kabata

October 23, 2024
Tweet

Transcript

  1. One Code to Rule Them All: A Guide to Kotlin

    and Compose Multiplatform. Victor Kabata _victorkabata victorkabata
  2. What is cross-platform development? • Developing a single codebase that

    can be deployed across multiple platforms (Android, iOS, Web, Desktop) without rewriting code for each platform. • Benefits of cross-platform development include: ◦ Code Reusability. ◦ Faster Development Cycles. ◦ Consistency across platforms. ◦ Reduced Development Cost. • Popular cross-platform frameworks/tools include; React Native, Xamarin, Flutter etc.
  3. What is Kotlin Multiplatform? • Kotlin Multiplatform (KMP) is a

    framework from JetBrains that allows developers to share code across multiple platforms (Android, iOS, web, desktop). • It provides the ability to write common code for business logic, data models, and backend communication, while still allowing platform-specific code where necessary. • Benefits of kotlin multiplatform include: ◦ Increased code reusability. ◦ Faster development cycles. ◦ Freedom to go native.
  4. What is Compose Multiplatform? • Modern UI toolkit that enables

    developers to build user interfaces across multiple platforms (Android, iOS, web, and desktop) using a shared codebase.
  5. Real-World Use Cases • Key industry players currently leveraging KMP

    include: ◦ Netflix ◦ CashApp ◦ Phillips ◦ McDonalds ◦ Forbes ◦ MeetUp
  6. Getting Started • Before diving into Kotlin Multiplatform development, ensure

    you have the following prerequisites set up: ◦ Android Studio or IntelliJ IDEA ◦ Xcode (if you plan on adding iOS as a platform) ◦ Kotlin Plugin
  7. Project Setup • The quickest way to set up a

    new Kotlin Multiplatform project is by using JetBrains' KMP Wizard. • Jetbrains’ KMP Wizard is an online tool that guides developers through the setup of a new KMP project by generating the necessary project structure, configuration files, and basic platform-specific modules.
  8. Project Setup … continued • Alternatively, you can create a

    new KMP project from Android Studio or IntelliJ IDEA IDEs:
  9. Kotlin Multiplatform Targets • Targets specify the platforms (like Android,

    iOS, desktop, or JavaScript) that your shared code will be compiled to. • Kotlin Multiplatform currently offers supports the following target platforms: ◦ Android ◦ iOS ◦ Desktop (Linux, MacOS, Windows) ◦ Web/JS
  10. KMP Project Structure • KMP project includes a shared module,

    which is a dedicated module that holds platform-independent code, allowing it to be reused across various target platforms. • The shared module includes several key packages, such as: ◦ commonMain ◦ commonTest ◦ targetMain eg. androidMain, iosMain, desktopMain, jsMain etc.
  11. Platform-Specific Code - expect/actual declaration in KMP is a mechanism

    used to handle platform-specific code. - expect keyword is used in the shared module to declare a common API or class without providing an implementation. - actual keyword is used in the platform-specific modules (e.g., Android, iOS) to provide the concrete implementation for each target platform
  12. Platform-Specific Code …continued // commonMain expect fun getPlatform() : String

    // androidMain actual fun getPlatform() : String = “Android” // iosMain actual fun getPlatform() : String = “iOS”
  13. Tools and Libraries for KMP - Networking - Ktor, Apollo

    GraphQL - Caching - Room, SQLDelight, Realm, Datastore, Multiplatform-Setting, Cache4k. - Image Loading - Coil3, Compose-ImageLoader, Kamel. - DI - Koin, Kodein, Kotlin-Inject. - Logging - Napier, Kermit, Kotlin-Logging. - Navigation - Jetpack Navigation, Decompose, Voyager. - Extras - BuildKonfig, Compose Cupertino
  14. Migration Guide/Strategies - Incremental Migration: Migrate parts of the codebase

    gradually instead of a complete overhaul. This approach allows for easier testing and integration. - Greenfield Approach: For new projects, consider starting directly with KMP to leverage its capabilities from the ground up.