Categories
Apps Kotlin Multiplatform Mobile

iOS app with Kotlin Multiplatform Mobile

Overview

Kotlin Multiplatform Mobile(KMM) is a new feature from Jetbrains. It is an alpha feature but can already be used to create and publish apps for both iOS and Android.

It is possible to write business (network and database) logic in Kotlin, and share this code between iOS and Android.

⛩Architecture

Kotlin code compiles into native library for iOS and Android app will depend on the shared Kotlin module.

Both IDE-s are used:

  • Android Studio: Android app and shared code development. Minimal iOS UI coding also possible.
  • Xcode: manage the iOS UI code.

💁🏽‍♂️Target group

Since KMM is in the early stages, with relatively steep learning curve and bugs, lacking community and library support, Flutter or React Native might be suggested for smaller teams and simpler apps. Kotlin can be recommended for:

Bigger projects with more resources

For a shared business logic that can be implemented with currently available Kotlin libraries, migrating to KMM can be a good idea. Netflix saved duplication of 50% of their codebase, because Kotlin libraries can handle both the network and database operations.

Mobile dev houses

Companies working for clients can cut development costs without sacrificing app quality or performance. Touchlab advertises itself as the subject’s expert, and has created their own Kotlin libraries for missing support. The app quality is not affected because UI uses native components, and the business logic is compiled into a native library.

Sending a message from Kotlin to iOS

To get Kotlin compiled for iOS, a KMM project needs to be created in Android Studio 4.2+ with KMM plugin installed

An elaborate project structure is generated with modules for each of the platforms and shared code.

After project has finished setting up, iOS target can be selected and the app should ideally be deployable.

However, after trying to run, the build fails with a code signature error:

error: Signing for "iosApp" requires a development team. Select a development 
team in the Signing & Capabilities editor. (in target 'iosApp' from project
 'iosApp')

Similar problems with Xcode interoperability arise frequently – thus the aforementioned warning about steep learning curve with KMM.

To fix our build error, the project needs to be opened in Xcode and a development team selected for signing the product.

After that, we are able to see Kotlin code displaying a message from iOS platform code

👾Switching between the simulator and the device

After choosing a simulator target from the run configuration:

The build fails because the architecture is not rebuilt for the newly selected architecture(arm64 > x64)

error: Building for iOS Simulator, but the linked and embedded framework 
'shared.framework' was built for iOS. (in target 'iosApp' from project 'iosApp')

Ideally, the system would switch the architecture automatically, but in this alpha software:

  • the shared.framework needs to be manually deleted from the xCode build phase
  • the shared.framework reference needs to be removed from the Project navigator.
  • project needs to be rebuilt with
./gradlew linkReleaseFrameworkIosX64

And the shared framework dragged to Xcode

Conclusion

Kotlin Multiplatform Mobile is a new effort in the already somewhat saturated cross platform development scene. It promises natively performing apps with costs savings from sharing the business logic in Kotlin. As an emerging technology, users should be aware of its limited library support and alpha status.

Sample code: Github

Discuss: