Google Endorses Kotlin Multiplatform

Google announced first class support for Kotlin Multiplatform at Google I/O 2024. This article provides an overview of the framework and how to get started creating multiplatform apps with it. By Carlos Mota.

Leave a rating/review
Save for later
Share

During 2017 Google I/O, Android developers rejoiced with the announcement of Kotlin as one of the main languages for app development. With the language’s continuous development and strong community adoption, the Android team soon switched entirely to Kotlin. From that point forth, all the libraries were written only in Kotlin.

This year, with the stability of Kotlin Multiplatform, the increasing developer adoption, and the new K2 compiler, Google joined the community to announce its support for multiplatform development.

Note: This article provides a brief overview of the Kotlin Multiplatform framework. If you are looking for a deep dive, see our Kotlin Multiplatform by Tutorials book. It is written by the author of this article.

Kotlin Multiplatform

Kotlin Multiplatform, or KMP, is a technology that allows developers to share their business logic across multiple platforms: Android, iOS, Desktop, Web and Server. It stands out among cross-platform solutions like Flutter and React-Native because its goal isn’t to have a single codebase and share the entire code but to share what makes sense for your project. This only relates to your app’s business logic and not UI.

There are clear advantages:

  • You don’t need to commit your entire project to a single framework: This is an important point, especially now that the support from Xamarin ended. Apps that were built with it now need to be migrated to .NET or rewritten in… Kotlin Multiplatform.
  • You decide what’s worth sharing and when: KMP adoption doesn’t bring risk to your project. You don’t need to migrate your app immediately: you can start using it when there’s a new feature to implement. Then, use it again in the next requirement and the one after. Once your team is confident, you can analyze if it’s worth refactoring parts of your app. If it’s not, keep them as they are. Kotlin Multiplatform is interoperable with your existing code.

Focus on sharing the business logic because the UI is usually implemented natively, either with XML/ Compose for Android or UIKit/Swift UI for iOS. This lets developers use native APIs to build a more performant and scalable app built on a strong foundation.

Changes to Kotlin Multiplatform

For those who are already familiar with Kotlin Multiplatform, the framework has leveled up a bunch of new additions such as:

Android Libraries With KMP Support

Along with the announcement, Google launched updates in a couple of Android libraries, now with support for Kotlin Multiplatform. They’re currently in different levels of development. The ones considered stable are:

While the alpha versions are:

  • Android Libraries With KMP Support

    Along with the announcement, Google launched updates in a couple of Android libraries, now with support for Kotlin Multiplatform. They’re currently in different levels of development. The ones considered stable are:

    • Annotations: Use annotations on your classes, functions, and properties.
    • Collections: A set of collection types and utilities.
    • DataStore: Provides a key-value data storage solution.
    • Paging: Simplifies loading large datasets by creating groups of smaller chunks (paginated) that the user can easily access.
    • WindowSize Class: Defines the current size of a window. Use it to represent different screen sizes and configurations.

    While the alpha versions are:

    • Room: Android’s official library for local storage (SQLite).
    • Lifecycle: Manages the lifecycle of activities, allowing components to adjust their behavior depending on the lifecycle state.
    • ViewModels: Stores and manages UI data depending on the activity lifecycle.

Google Docs Joins the KMP Journey!

Google Docs migrated to use Kotlin Multiplatform for the business logic, which is now shared across Android, iOS, and Web. Although there were no further details, the Google Workspace team is expected to add the same support to other apps like Gmail, Calendar, Drive, and Meet later in the year.

Google Docs has over one billion downloads on the Google Play Store and an average of 4.3 stars. These metrics are important to show Google’s commitment to Kotlin Multiplatform and confirm its validation on a widely used app that needs to be performant to meet users’ needs.

Documentation Update

The Android team also updated the official documentation so developers can see which classes are available for Multiplatform. For instance, visit androidx.collection to see which data types you can use on Android, Desktop, and iOS (Native).

Android documentation for androidx.collection showing the current support for Kotlin Multiplatform

Sample Apps

There’s now a GitHub repository for Kotlin Multiplatform samples on Android. Here, you can find two sample apps that use the libraries announced during Google I/O:

Both projects target Android and iOS. Before you work on these apps, you need to configure your setup.

As you can see, KMP provides a lot of awesome updates. Before you can really appreciate them, you need to get it working on your machine. Thankfully, it’s not too difficult.

Installing KMP

Before opening the apps, you need to install:

Once installed, open the sample that you want to compile.

  • Kotlin Multiplatform Plugin on Android Studio: You can install by opening the IDE Settings and navigating to Plugins. Here, search for Kotlin Multiplatform, and once you see the plugin, click install and restart Android Studio afterward.
  • CocoaPods: This is how the business logic is distributed to your iOS app. Although you can also use Swift Package for this, both samples use CocoaPods. To install, open the Terminal and enter:
    brew install cocoapods
    

    Once installed, open the sample that you want to compile.

brew install cocoapods

After synchronizing the project, you can select which app you would like to run via configurations: androidApp or iosApp. One app targets Android devices/emulators, and the other iOS devices/simulators.

You can compile your apps to Android or iOS targets.

Here’s DiceRoller compiled for both platforms:

DiceRoller app running on the Android emulator and on the iOS simulator

You’ll get this error because the sample uses an older version of AGP (Android Gradle Plugin). To solve this, open the libs.version.toml file located inside the gradle folder and look for agp. Replace the existing version with:

Synchronize the project. Now you can compile the app for both targets.

When compiling the iOSApp configuration you might see the following error:

Next to it, you’ll see a button with the text Fix on it. Click it to open Xcode, which should load the correct schema.

If you still see the previous error, compile the project directly from Xcode.

Alternatively, you can try to reboot your Mac.

Note: Depending on the version of Android Studio you’re using, you might see the following error when trying to run your app:
Collection contains no element matching the predicate.

You’ll get this error because the sample uses an older version of AGP (Android Gradle Plugin). To solve this, open the libs.version.toml file located inside the gradle folder and look for agp. Replace the existing version with:

agp = "8.4.1"

Synchronize the project. Now you can compile the app for both targets.

When compiling the iOSApp configuration you might see the following error:

Error: can't grab Xcode schemas with /usr/bin/xcodebuild

Next to it, you’ll see a button with the text Fix on it. Click it to open Xcode, which should load the correct schema.

If you still see the previous error, compile the project directly from Xcode.

Alternatively, you can try to reboot your Mac.

Collection contains no element matching the predicate.
agp = "8.4.1"
Error: can't grab Xcode schemas with /usr/bin/xcodebuild

Here’s Fruitties running on the Android Emulator and iOS Simulator:

Fruitties app running on the Android emulator and on the iOS simulator

Note: To compile the iOS version, you must install a MacBook with Xcode on both apps.

It’s finally time to get started!