Start KMM→ Getting the Settings right.

Codeangi
6 min readFeb 21, 2023

As we saw in our previous encounter with Kotlin Multiplatform Kotlin offers a great ability to share code, when it comes to Kotlin Multiplatform-Mobile, or KMM for short, we can write business logic in Kotlin as sharable code and expose functionality via APIs.

Kotlin Multiplatform Mobile is in Beta. Language features and tooling may change in future Kotlin versions.

Tooling:

IntelliJ IDEA is an integrated development environment (IDE) written in Java for developing computer software written in Java, Kotlin, Groovy, and other JAR-based languages. It is developed by JetBrains (formerly known as IntelliJ), a powerful tool for Kotlin development that offers many standard IDE features along with Kotlin-oriented plugins to enable KMM development.

Android Studio is Based on IntelliJ IDEA and also offers an integrated environment to develop and deploy android applications and libraries, with the KMM plugin we can create shareable modules and integrate them into Android and iOS platforms.

required: KMM plugin

source:https://plugins.jetbrains.com/plugin/14936-kotlin-multiplatform-mobile

How to share KMM code with Android and iOS?

  • As KMM Module: Using Android studio we can create an integrated project with Android, iOS, and Shared code which gives us a sample project template with the shared module already integrated into to respective application, We can deploy the iOS application from the android studio with the help of Xcode Command Line tools (Mac OS is required to build and run the iOS application).
  • As KMM Library: If we wish to keep the shared codebase separate and have a dedicated distribution channel, we can choose to configure our project as a KMM Library, which will omit any application-related code and expose functionalities APIs that can be integrated into any application as a library (.xcf/swift/cocoa pod framework for iOS, and JAR/AAR for Android).

How to set up KMM development?

Requirements:

  • Install Android Studio — 4.2 or 2020.3.1 Canary 8 or higher.
  • Install KMM plugin — Go to AndroidStudio→ Preferences → Plugins search for the plugin Kotlin Multiplatform Mobile in Marketplace and install it.

For iOS-specific code and run an iOS application, Xcode — version 11.3 or higher needs to be installed, once installed make sure Command Line Tools is set to the installed Xcode version, go to Xcode → Preferences → Locations → Command Line Tools.

To create a Kotlin Multiplatform Application:

  1. Open Android Studio → File → New → New Project → Choose Kotlin Multiplatform Application from the templates. Click on Next.

2. Choose an appropriate name for your project/application, a package name for identification, and a minimum android SDK, Click on Next.

3. Next, Choose appropriate names for the Android application, iOS Application, and the Shared module, Choose an iOS distribution type (We choose Regular framework in this case)and click on “Finish”.

It’s ready;]

To See the whole content of the project, once you have configured KMM Application, switch explorer to show “Project”.

What do you see here: KMM Project template anatomy.

  1. MyKMMApplication folder*: Root folder for the project.
  2. .gradle folder: The Gradle user home directory ( $USER_HOME/.gradle by default) is used to store global configuration properties and initialization scripts as well as caches and log files.
  3. android app folder*: Folder containing Android application-related files and code.
    - build folder(App): Android build output folder.
    - src folder: Android App source code.
    - build.gradle.kts file (App): Dependency & build configuration script for android application.
  4. build folder (Project): Project build output folder.
  5. gradle folder: Contains gradle wrapper for creating and running project.
  6. iosApp folder*: Contains Xcode project and code related to the iOS application (Mac OS, Xcode is required to compile and run the iOS project)
    - iosApp folder (inner)*: ****Contains source code and libraries related to iOS Application.
    - iosAPP.xcodeproj folder: Contains iOS workspace and Xcode project configuration files.
  7. shared folder*: Folder containing shared KMM code.
    - build folder: Build output folder for shared code.
    - src folder: Source folder for containing shared code and code of target source sets (Android and iOS).
    androidMain folder: Contains platform-specific implementations and “actual” functions and classes for Android.
    commonMain folder: Contains shared code, “expect” declarations.
    iosMain folder: Contains platform-specific implementations and “actual” functions and classes for iOS.
    -build.gradle.kts file(Shared): Contains build configurations and source-set dependencies for target platforms.
  8. .gitignore file: Contains a list of files/folders to ignore when committing code to Git VCS.
  9. build.gradle.kts file(Project): Contains project-wide dependencies and project configuration.
  10. gradle.properties file: Properties file containing project configuration values and versioning constants.
  11. gradlew file: Gradle wrapper used to build and run files on Unix-based machines.
  12. gradlew.bat file: Gradle wrapper used to build and run files on Windows machines.
  13. local.properties file: Contains configuration values for local-machine specific SDK location/command line args
  14. settings.gradle.kts: Contains settings related to modules of project.
  15. External Libraries: Contains all the library dependencies for project.
  16. Scratches and consoles: Scratch files are fully functional, runnable, and debuggable files. They’re useful for drafting up code or running code in isolation. Alternatively, scratch buffers are plain text files. They’re useful for making quick notes, a to-do list, or frequently used strings.

*name configured during project creation

Running Applications:

When we create a KMM project using android studio, it already contains a sample integration of the shared library, which returns a string containing platform details via Greeting class, and its all ready to run!

1. Android

To run the android application select androidApp in the run-configuration drop-down menu in android studio, choose or create an Android Emulator/ Connect a debuggable physical device, and hit “Run”.

2. iOS(Mac OS):

We can either choose to run the iOS App directly from the android studio or run it via Xcode, currently,iOS build is only possible via MacOs. To run the iOS application select iOSApplication in run-configuration drop-down menu in android studio, click on “Edit configuration” option to select desired iPhone/iPad simulator, click on “run” icon.

We can also run iOS application via Xcode, in order to do this, select “iosApp.xcodeproj” → open in → Xcode→ and click on “Start active scheme”

Were you able to run?

that’s great! you have just created a KMM application successfully.

If you found it helpful, these are the related articles:
1. Sharing Code: with KMM
2. Networking in KMM
3. KMM: Whats on the plate

--

--