The panacea for mobile development?

The panacea for mobile development?


Mobile app usage is booming and the demand for app development is contributing to its growth. According to a study conducted by WeAreSocial, in collaboration with Hootsuite, app downloads have increased by 23.7% over the previous year, with a total of 142.9 billion apps downloaded. As a result, mobile app development is at its fastest growing stage and there needs to be a faster and more efficient way to create apps.

During the planning stage of a mobile application, the main challenge is to decide whether the application should be native or hybrid. However, the problem with a hybrid application, even though the development is unified, is that the performance is usually lower than that of a native application, and they have more difficulties working with the device’s built-in functions (GPS, camera, Bluetooth, etc.).

So why not use a hybrid technology that solves performance issues without having to deal with the device’s built-in features?

What is Kotlin Multiplatform Mobile?

Kotlin Multiplatform Mobile is a cross-platform mobile development SDK developed and provided by JetBrains. Its main benefit is the ability to reduce the time spent both developing and maintaining the same source code for different platforms while maintaining the flexibility and benefits of native programming.

Android and iOS versions of an app often have a lot in common, but they can also differ greatly (especially in terms of user interface). At the same time, business logic, such as data management, analytics, and authentication, is often identical. That’s why it’s natural to share some parts of an app across platforms while keeping other parts completely separate.

With KMM, you can enjoy this flexibility by abstracting all business logic into a single common code and using this code in both iOS and Android applications, and maintain the advantages of native programming by writing platform-specific code when necessary, for example to implement a native UI or use platform-specific APIs.

How does KMM work on iOS and Android?

The structure of a basic project in KMM is mainly made up of three modules:

Shared Module

The shared module contains the common logic, written in Kotlin and compiled using Gradle, for both Android and iOS apps. However, sometimes it is necessary to write platform-specific logic inside the shared module. In such cases, Kotlin provides the expect/actual mechanism. With this mechanism, the shared module defines the expect type declarations, and the platform-specific modules must provide the corresponding actual type implementations.

It is organized into 3 sub-modules respectively:

  • CommonMain: contains source code that can be used on both platforms, including expect statements.
  • AndroidMain: Contains Android-specific source code, including current type implementations.
  • iOSMain: contains iOS-specific source code, including current type implementations.

Once we have the source code, it is compiled into an Android ARchive (AAR) artifact, in the case of Android. On the other hand, for iOS, the source code is compiled into a framework using Kotlin Native. In addition, for each platform, one or more artifacts can be compiled. By default, the compilation includes:

  • For iOS two artifacts: main and test.
  • For Android, one artifact is created for each Android Build Variant.

Android App

The Android App module as part of a KMM project is a typical Android application written in Kotlin and compiled using Gradle. And to access the common logic within the shared module, the artifact is added as a dependency within Gradle:

iOS App

The iOs App module as part of a KMM project is a typical iOS application written in Swift and compiled using XCode. And to access the common logic within the shared module, it must be configured correctly:

  1. Other linker flags within the Linking section:

2. Framework Search Paths within the Search Paths section:

Advantages and disadvantages of KMM

The main advantages of KMM are:

  • Higher development costs: Using KMM, it is possible to reduce development times by 30-50%. In addition, thanks to Kotlin, around 40% fewer lines of code are required compared to Java. This results in lower costs and time invested during the development of an application.
  • Less bugs: Fewer lines of code and simpler code lead to fewer bugs. However, if there is a bug, and since the code is shared across all platforms, implementing a single fix will solve the bug for both Android and iOS. A fix per platform is not necessary.
  • Increased team efficiency: Thanks to the business logic being shared between both platforms, for example, the iOS team can focus on developing the UI for the iOS platform, while the Android team divides the tasks between UI and common logic since the gap between programming languages ​​is smaller.
  • Practically a native solution: The use of KMM allows the entire visual part of the application to be the responsibility of each of the platforms, ensuring optimal performance and excellent integration with the different native components.

Despite the advantages, there are also a number of disadvantages that need to be considered before using Kotlin Multiplatform Mobile:

  • Learning curve: For someone already familiar with Kotlin, the jump to KMM is practically immediate compared to an iOS developer or someone with little or no experience in object-oriented programming.
  • Multithreading: Kotlin makes use of Coroutines to write asynchronous code, but there are a number of limitations, as the use of Coroutines in iOS only supports a single thread. However, there are a number of tools and workarounds to overcome this.
  • Alpha phase: KMM is in Alpha phase which means that some features may change in future versions.
  • Lack of experts: Although Kotlin has gained popularity in recent years, especially in Android development, KMM is still not sufficiently known and during the development of an application you may encounter a problem without an optimal solution.

Conclusion

Kotlin is a great programming language and KMM brings it closer to other platforms with the intention of creating scalable and easily maintainable applications with a single common business logic. Even though the shared code is slightly less than with other technologies (it is not possible to share the UI), it gives us the advantage of creating the UI in accordance with the design guidelines and best practices of the specific platforms.

Furthermore, combining KMM with reactive programming and a unidirectional data flow helps us create a fully decoupled and UI-agnostic solution. Using KMM, we expect to share 70-80% of the code across all platforms, and adding a new platform would be just a matter of implementing a new UI.

And last but not least, even though KMM is in Alpha phase, it is completely safe to use in production because Kotlin JVM is stable and Kotlin Native is in Beta phase.

Author of the article

Marc Gareta, consultant at Deloitte Technology Consulting

Leave a Comment

Your email address will not be published. Required fields are marked *