Slide 1

Slide 1 text

Mastering Cross- Platform Development with Kotlin Multiplatform Mobile Google I/O Extended Yangon Su Thwe Thwe Tun

Slide 2

Slide 2 text

I’m passionate about building mobile apps to achieve easier and better lifestyles in our everyday lives. I have been working as a mobile developer for 10 years with Android and Flutter. SU THWE THWE TUN MOBILE APPLICATION DEVELOPER @USA

Slide 3

Slide 3 text

Introduction to KMM Kotlin Multiplatform Mobile (KMM) is an SDK by JetBrains. Use a single codebase for native iOS and Android applications. It can share common business logic and code. Write platform-specific code when necessary. WHAT IS KMM?

Slide 4

Slide 4 text

Advantages of KMM KMM only shares business logic. Flutter and React Native use a single codebase for both UI and logic COMPARISON WITH OTHER CROSS-PLATFORM FRAMEWORKS KMM enhances development speed by sharing code between iOS and Android. BENEFITS

Slide 5

Slide 5 text

KMM Cross-platform development GETTING STARTED WITH KMM

Slide 6

Slide 6 text

INSTALL KMM PLUGIN Install Android Studio. 1. Open Android Studio. 2. Go to Preferences (or Settings on Windows/Linux). 3. Navigate to Plugins and search for "Kotlin Multiplatform Mobile". 4. Install the plugin and restart Android Studio. 5.

Slide 7

Slide 7 text

Setting up a KMM project using Android Studio. 1. Open Android Studio. 2. Click on New Project. 3. Select Kotlin Multiplatform App from the project templates. 4. Follow the wizard to set up your project. 5. SETTING UP A KMM PROJECT IN ANDROID STUDIO.

Slide 8

Slide 8 text

Architecture Overviews shared: Contains the shared code. commonMain: Shared code for both platforms. commonTest: Shared test code. androidMain: Android-specific code. iosMain: iOS-specific code. androidApp: Contains the Android-specific application code. iosApp: Contains the iOS-specific application code.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

KMM Cross-platform development WRITING SHARED CODE

Slide 11

Slide 11 text

Writing Shared Code Networking / APIs call Data models Common business logic Database schema

Slide 12

Slide 12 text

Example of Shared Code

Slide 13

Slide 13 text

“expect” keyword: Declared in shared module to be implemented on each platform. USING “EXPECT” AND “ACTUAL” “actual” keyword: Declared in the Android and iOS modules to provide the actual implementations of the “expect”.

Slide 14

Slide 14 text

Declare an “expect” class Platform.kt with a method getPlatformName().

Slide 15

Slide 15 text

In these examples, the “actual” keyword is used to provide platform-specific implementations of the getPlatformName() method.

Slide 16

Slide 16 text

KMM Cross-platform development NETWORKING WITH KTOR

Slide 17

Slide 17 text

KTOR It is for servers and clients in connected systems using Kotlin. It allows to sharing of networking code between Android and iOS. KTOR IS A FRAMEWORK.

Slide 18

Slide 18 text

Setting Up Ktor in Shared Module Open shared/build.gradle.kts and add the necessary Ktor dependencies. ADD KTOR DEPENDENCIES

Slide 19

Slide 19 text

Create a new Kotlin file NetworkClient.kt in shared/src/commonMain/kotlin. Create a Network Client

Slide 20

Slide 20 text

Create a new Kotlin file ApiService.kt in shared/src/commonMain/kotlin Make a Network Request

Slide 21

Slide 21 text

Use the ApiService in Android Module

Slide 22

Slide 22 text

Use the ApiService in iOS Module

Slide 23

Slide 23 text

KMM Cross-platform development INTERFACING WITH PLATFORM-SPECIFIC APIS

Slide 24

Slide 24 text

Calling Native APIs from Shared Code Using expect and actual keywords Define platform-agnostic API in shared code Provide platform-specific implementations

Slide 25

Slide 25 text

Declare an expect class Camera with a method takePicture() Defining Platform-Agnostic API

Slide 26

Slide 26 text

Provide an actual implementation using Android's MediaStore API Android implementation

Slide 27

Slide 27 text

Provide an actual implementation using iOS-specific APIs iOS implementation

Slide 28

Slide 28 text

Using Camera class in Shared code

Slide 29

Slide 29 text

Keep platform-specific code minimal. BEST PRACTICES Use shared code for business logic. Ensure platform-specific implementations are isolated.

Slide 30

Slide 30 text

KMM Cross-platform development TESTING IN KMM

Slide 31

Slide 31 text

Unit tests are written in the commonTest which can be executed on both Android and iOS. Writing Unit Tests for Shared Code

Slide 32

Slide 32 text

Build Configuration (shared/build.gradle.kts) Configuring Gradle for Testing

Slide 33

Slide 33 text

Open Android Studio. 1. Navigate to the commonTest directory. 2. Right-click on the test file (CalculatorTest.kt). 3. Select "Run 'CalculatorTest'". 4. Running Tests on Android Studio

Slide 34

Slide 34 text

Open a terminal. 1. Navigate to your project directory. 2. Execute this command. 3. Running Tests on iOS ./gradlew iosTest JUnit is used for writing and running tests on the JVM. It can be executed directly from Android Studio or via the command line. Running JUnit Tests ./gradlew test

Slide 35

Slide 35 text

SUMMARY Run tests using Android Studio for Android and command line for iOS. Use kotlin.test for cross-platform testing. Use JUnit for JVM-specific testing. Tools and Frameworks for Testing:

Slide 36

Slide 36 text

KMM Cross-platform development Effective Tools and Techniques for KMM Development

Slide 37

Slide 37 text

Overview of IDE Support Full support for Kotlin Multiplatform projects. Integrated tools for coding, building, and debugging. PRIMARY IDE: ANDROID STUDIO IntelliJ IDEA Xcode (for iOS-specific development) OTHER SUPPORTED IDES

Slide 38

Slide 38 text

Debugging Techniques Use Android Studio’s built-in debugger for Android Use Xcode for debugging iOS code DEBUGGING Print statements and logging for shared code Using conditional breakpoints CROSS-PLATFORM DEBUGGING

Slide 39

Slide 39 text

KMM Cross-platform development Real-World Use Cases of KMM

Slide 40

Slide 40 text

Case Study: Netflix - Mobile Studio Company: Netflix Project: Mobile Studio Objective: Build a mobile application for internal use by Netflix’s creative teams. Overview :

Slide 41

Slide 41 text

Case Study: Netflix - Mobile Studio Frequent updates and features required for both iOS and Android. High costs and time consumption due to maintaining separate codebases. Difficulty in synchronizing new features and bug fixes across platforms. Challenges :

Slide 42

Slide 42 text

Case Study: Netflix - Mobile Studio Implemented KMM to share common business logic and core functionalities. Utilized Kotlin for shared code and native Swift/Java for platform-specific implementations. Focused on sharing models, data handling, and business logic while keeping UI native. Solution :

Slide 43

Slide 43 text

Case Study: Netflix - Mobile Studio Networking: Used Ktor for making network requests. Persistence: Implemented SQLDelight for shared database operations. ViewModels: Shared ViewModel logic using KMM to manage UI-related data. Technical Details :

Slide 44

Slide 44 text

Case Study: Netflix - Mobile Studio Efficiency: Increased development efficiency by sharing up to 80% of the codebase. Speed: Faster feature rollouts and synchronized updates across both platforms. Consistency: Improved consistency in features and performance across platforms. Benefits :

Slide 45

Slide 45 text

Case Study: Netflix - Mobile Studio Results : Increased Efficiency Shared codebase reduces duplication of effort Consistent User Experience Unified business logic across platforms ensures consistency Cost Savings Reduced development and maintenance costs

Slide 46

Slide 46 text

KMM Cross-platform development Resources and Community of KMM

Slide 47

Slide 47 text

Overview of the KMM Community Active Community Support Forums, Slack channels, and social media groups Official Documentation and Tutorials Comprehensive guides on the JetBrains website

Slide 48

Slide 48 text

POPULAR LIBRARIES AND TOOLS Ktor For network operations SQLDelight Type-safe SQL for Kotlin Multiplatform Multiplatform Settings For managing application settings across platforms

Slide 49

Slide 49 text

GitHub repositories for community- driven projects Getting involve in reporting issues, submitting PRs, writing documentation Contribution Opportunities

Slide 50

Slide 50 text

IMPACT ON MOBILE DEVELOPMENT Unified Development Process Single codebase for business logic across platforms Cost and Time Efficiency Reduced development time and costs Evolving Best Practices New design patterns and practices for cross-platform development

Slide 51

Slide 51 text

Conclusion Use expect and actual for platform-specific implementations Keep platform-specific code minimal and isolated Share business logic across platforms KEY TAKEAWAY

Slide 52

Slide 52 text

Email address : [email protected] THANK YOU!