Supporting cross-platform programming is one of Kotlin’s key strengths. When maintaining applications that support multiple platforms at the same time, it can significantly reduce the work of implementing the same business code on multiple platforms without affecting the flexibility of the code, while maintaining the advantages of Native code. Compared to Flutter, Kotlin Multiplatform focuses more on the logical layer. I will try to use both Kotlin Multiplatform and Flutter to realize the CROSS-platform UI and logic.

Kotlin Multiplatform mechanism

Kotlin Multiplatform (hereafter referred to as KMP) has three layers.

  • Common Kotlin contains support for the Kotlin language, core libraries, and basic tools. Code written in Common Kotlin runs on all platforms
  • The Kotlin Multiplatform library is used when implementing business logic common to all platforms. Generic code can use a number of basic capabilities, such as Http, serialization, and coroutine management.
  • When interacting with platforms, use platform-specific versions of Kotlin (kotlin/JVM, Kotlin /Native, Kotlin /Js), etc. These packages provide access to platform code ((JVM, JS,Native)

It’s no use talking, just start building the environment. This article takes android and iOS platforms as an example to achieve a client that can run on both sides through this series of tutorials.

Environment to prepare

  1. Android Studio 4.2 and above, the version I use
The Build # AI - 211.7628.21.2111.7863044, built on October 29, 2021 the Runtime version: 11.0.11 + 0 - b60-7590822 x86_64 VM: OpenJDK 64-bit Server VM by JetBrains S.R.O. macOS 12.0.1GC: G1 Young Generation, G1 Old Generation Memory: 1280M Cores: 12 Registry: external.system.auto.import.disabled=trueCopy the code
  1. XCode 11.3 or higher. I use Version 13.1 (13A1030d) because I need to develop iOS terminal.
  2. JDK, not much more
  3. Kotlin plugin. In AndroidStudio select Tools | Kotlin | Configure Kotlin Plugin Updates check Kotlin Plugin version, recommend to update to the latest stable version
  4. KMM Plugin, directly in the Android Studio Plugin search installation, after the completion of Android Studio restart

6. Install CoocaPods (optional). The ability of CoocaPods will be used later to compile iOS applications. Run it on the terminal

sudo gem install coocapods
sudo gem install cocoapods-generate 
Copy the code

Start the first KMM application

  1. AndroidStudio creates a new Application, drag it to the bottom and select KMM Application. Next, there is a page to select the Android version, which is the same as ordinary Android applications

  1. The application name Settings for each platform and the dependency management method of iOS should be selected to ensure that the relevant environment is correct. I select CoocaPods here and click Finish after completion

  1. After Gradle Sync is complete, the project is created. There are three modules in the project. We focus on DoStomething. Module has three folders, which correspond to Common (cross-platform reuse), Android, and iOS.

  1. When writing business logic, it may be necessary to call Native methods of different platforms or realize different logic on different platforms. Therefore, KMM’s Expect /actual mechanism can be used to encapsulate interface requests and provide unified interfaces for the Common layer. In DoSomething’s commonMain we see a Platform class with modifiersexpectThis means that the class is only defined in Common, and the specific implementation is in the respective platform code. There are also corresponding Platform classes in iOSMain and AndroidMain, provided byactualDecoration is a concrete implementation of Platform. In addition to classes, extension methods and attributes can use Expect.

  1. Instead of implementing Expect classes directly, you can bridge them directly to Expect classes, starting with a new one in commonMain, as an example of DecimalNumber, which is common in developmentKMMDecimalNumberClass,expectModify, add, divide, multiply three methods. You can see that the IDE directly prompts an exception because the Expect class has no corresponding actual on the JVM and Natvie.

  1. First create actual class in the corresponding directory of Android platform and useactual typealiasModification. The bridge is completed by adding related method mappings.

  1. The Kotlin class does not support multiple constructors with the same signature, but the OC class does. When defining KMMBigDecimal=NSDecimalNumber, The compiler’s attempt to create Kotlin’s KMMBigDecimal failed due to a constructor conflict and can pass@SuppressComments resolve this error.

  1. Fill in a create method for KMMBigDecimal, and you’re ready to run

Android Runtime Effects

Ios Running Effects

TODO

  1. How do I output KMM code as a dependency
  2. Multithreading in KMM

Problems encountered

  • The System displays “Could not detect version of installed Xcode” when running iOSApp

Solution: stackoverflow.com/questions/6…

  • IOSApp can’t grab xcode schemes

Solution: youtrack.jetbrains.com/issue/kt-41…