Computer scienceProgramming languagesKotlinKotlin MultiplatformIntroduction to KMP

Tools for KMP

5 minutes read

Kotlin Multiplatform (KMP) allows you to write code once in Kotlin and use it across various platforms—Android, iOS, Web, and more! This simplifies app development by enabling code reuse. To unlock the full potential of KMP and streamline your workflow, you need tools specifically designed for building multiplatform applications.

In the upcoming sections, we'll explore essential tools for KMP development, discussing their functions and how they enhance your experience. By the end, you'll be well-prepared to navigate the KMP ecosystem with confidence!

IDEs for KMP development

The core of Kotlin Multiplatform development lies in writing shared code that works across platforms like Android, iOS, and the Web. Specialized Integrated Development Environments (IDEs) are essential for managing this shared code efficiently. But why are particular IDEs better suited for KMP development? These tools act as your coding workspace, selected to optimize and simplify the process.

Android Studio. Despite its name, Android Studio is a versatile IDE for KMP development, built on the IntelliJ IDEA platform.

Here's how to set it up:

  1. Install & Update: Download the latest stable version from the Android Studio website.

  2. Launch and Open Plugins: Open Android Studio and go to Settings/Preferences (Cmd + , on Mac or Ctrl + Alt + S on Windows/Linux), then select Plugins.

  3. Update Kotlin: Search for Kotlin in the plugin window and ensure it's up-to-date.
    Kotlin plugin inside Android Studio.

  4. Install KMP Plugin: Still within Plugins, find and install the Kotlin Multiplatform plugin, which provides templates for Android and iOS projects and essential tools for debugging.
    Kotlin multiplatform plugin inside Android Studio.

Run iOS app on a simulator directly from Android Studio

Once these steps are completed, Android Studio will be ready for KMP projects.

If you are using IntelliJ Idea and don't need iOS support, you can still work with KMP by keeping IntelliJ and the Kotlin plugin updated. Also, add the Android plugin to build, run, and debug your Android target.

For targeting iOS in Android Studio, note that you won't be able to edit Swift code directly in the IDE. However, JetBrains Fleet can overcome this limitation, as we'll explore next.

JetBrains Fleet. For a more lightweight, unified development experience, JetBrains offers Fleet, a next-generation IDE.

JetBrains Fleet icon.

JetBrains Fleet icon

As a polyglot IDE, Fleet supports multiple programming languages, including Swift, making it a strong option for KMP projects. Fleet provides intelligent code completion and makes it easier to navigate large KMP codebases.

Keep in mind that you'll still need to use Xcode for running iOS apps and Android Studio for Android apps, even when using Fleet.

Here's how to prepare Fleet for KMP development:

  1. Download and Install: Get the latest stable version from the JetBrains website.

  2. No Plugins Needed: Fleet's centralized project setup eliminates the need for manual plugin installations for KMP.

  3. Xcode and Android Studio: Ensure you have the latest versions of both installed and run them at least once.

  4. Activate Smart Mode: Click the "Smart Mode" toggle in the top bar for deeper code analysis and KMP-specific features.

Activating smart mode in Fleet.

Fleet's Project Preflight will analyze your system when working on macOS, ensuring all tools and dependencies are installed correctly before starting a project.

Project preflight in Fleet.

While Fleet allows editing Swift code, running and debugging iOS apps still requires a macOS environment.

Choosing between Android Studio and Fleet depends on your workflow and project requirements. Don't hesitate to explore both as you dive deeper into KMP.

Now, let's turn our attention to an essential tool for iOS development: Xcode.

Targeting iOS with Xcode

Although Kotlin Multiplatform lets you share code across platforms, native iOS app development requires its own set of tools. This is where Xcode becomes essential for creating, managing, and deploying apps in Apple's ecosystem.

Xcode 14 icon.

Xcode 14 icon

While KMP handles the shared logic, Xcode allows you to open, fine-tune, and run your iOS app.

Xcode's key features:

  • Interface Builder: A visual tool for designing your app’s UI by dragging and dropping elements.

  • Simulators and Devices: Test your KMP app on various screen sizes, iOS versions, and hardware directly from Xcode.

  • Debugging Power: Xcode offers powerful debugging tools to help you resolve issues when integrating Kotlin code.

Key information for KMP and Xcode:

  1. MacOS Only: Xcode runs exclusively on macOS.

  2. Easy Installation: Download Xcode from the Mac App Store.

  3. Command Line Tools: During setup, ensure you install the Command Line Tools, which are vital for managing dependencies and automating builds in KMP projects.

CocoaPods: Managing iOS Dependencies. Just like any other platform, iOS development relies on libraries and frameworks to simplify common tasks. When targeting iOS, you’ll likely need CocoaPods, a dependency manager for iOS libraries. CocoaPods simplifies the process of integrating shared Kotlin/Native code with iOS-specific libraries. It ensures version compatibility and smooth library integration, making it an indispensable tool for iOS development within KMP. You can learn more about CocoaPods, including installation and usage instructions, on their official website.

KDoctor

With multiple tools interacting in a KMP project, maintaining your development environment is key. KDoctor is a command-line tool designed to help diagnose and resolve common configuration issues in KMP projects. It's available exclusively for macOS.

Here's how KDoctor comes to the rescue:

  • Environment Checkup: KDoctor ensures that all necessary versions of tools, including Kotlin and Xcode, are properly installed and configured.

  • iOS-Specific Insights: It identifies inconsistencies when working with Kotlin and Swift.

  • Helpful Suggestions: KDoctor provides clear advice if it detects any issues.

You can find KDoctor on GitHub and learn more about how to install and use it there.

Kdoctor result inside console.

Web targets

Kotlin Multiplatform also supports web development, allowing you to share code between backend, frontend, and mobile apps. Kotlin provides two ways to run code in the browser:

  • Kotlin/JS: Converts Kotlin code into JavaScript, ensuring broad compatibility with modern browsers.

  • Kotlin/Wasm: Compiles Kotlin code into WebAssembly (Wasm), a format known for its performance in complex apps like those with heavy graphics.

For Kotlin/Wasm, using a browser that supports WebAssembly garbage collection (GC) is recommended to ensure smooth memory management and stability. Popular browsers like Chrome, Firefox, and Edge already support Wasm GC.

You can use this link to check if a specific browser version is compatible with Wasm GC.

Conclusion

In this topic, we explored essential tools for Kotlin Multiplatform development. Android Studio and JetBrains Fleet offer versatile environments for cross-platform projects, while Xcode remains indispensable for iOS development. Don’t forget KDoctor for diagnosing setup issues and CocoaPods for managing iOS dependencies. Finally, Kotlin’s web capabilities through Kotlin/JS and Kotlin/Wasm allow you to extend your reach to browsers.

Mastering these tools will empower you to build efficient, reliable, and truly cross-platform applications. Now that you’re equipped with this knowledge, it’s time to put it into practice!

7 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo